Skip to content

Commit ecf6ebe

Browse files
authored
Merge pull request #7 from PureSwift/feature/decoder
Add `ModelDataDecoder`
2 parents 746def1 + 316f98a commit ecf6ebe

9 files changed

Lines changed: 776 additions & 30 deletions

File tree

Sources/CoreDataModel/NSAttributeType.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public extension NSAttributeType {
1414

1515
init(attributeType: AttributeType) {
1616
switch attributeType {
17-
case .boolean:
17+
case .bool:
1818
self = .booleanAttributeType
1919
case .int16:
2020
self = .integer16AttributeType
@@ -61,7 +61,7 @@ public extension AttributeType {
6161
case .stringAttributeType:
6262
self = .string
6363
case .booleanAttributeType:
64-
self = .boolean
64+
self = .bool
6565
case .dateAttributeType:
6666
self = .date
6767
case .binaryDataAttributeType:

Sources/CoreModel/AttributeType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
public enum AttributeType: String, Codable, CaseIterable, Sendable {
1212

1313
/// Boolean number type.
14-
case boolean
14+
case bool
1515

1616
/// 16 bit Integer number type.
1717
case int16

Sources/CoreModel/Decodable.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,46 @@ public extension ModelData {
6969
}
7070
}
7171

72-
// MARK: - Default Codable Implementation
72+
// MARK: - AttributeDecodable
7373

74-
extension Entity where Self: Decodable, Self.ID: Decodable {
74+
public protocol AttributeDecodable {
7575

76-
// TODO: Default implementation for Decodable
76+
init?(attributeValue: AttributeValue)
7777
}
7878

79-
// MARK: - AttributeDecodable
79+
extension Optional: AttributeDecodable where Wrapped: AttributeDecodable {
80+
81+
public init?(attributeValue: AttributeValue) {
82+
switch attributeValue {
83+
case .null:
84+
self = .none
85+
default:
86+
guard let value = Wrapped.init(attributeValue: attributeValue) else {
87+
return nil
88+
}
89+
self = .some(value)
90+
}
91+
}
92+
}
8093

81-
public protocol AttributeDecodable {
94+
extension AttributeDecodable where Self: RawRepresentable, RawValue: AttributeDecodable {
8295

83-
init?(attributeValue: AttributeValue)
96+
public init?(attributeValue: AttributeValue) {
97+
guard let rawValue = RawValue.init(attributeValue: attributeValue) else {
98+
return nil
99+
}
100+
self.init(rawValue: rawValue)
101+
}
102+
}
103+
104+
extension Bool: AttributeDecodable {
105+
106+
public init?(attributeValue: AttributeValue) {
107+
guard case let .bool(value) = attributeValue else {
108+
return nil
109+
}
110+
self = value
111+
}
84112
}
85113

86114
extension String: AttributeDecodable {

0 commit comments

Comments
 (0)