Skip to content

Commit b85eabf

Browse files
committed
Add AttributeCodable support for Decimal
1 parent f5d9d1d commit b85eabf

4 files changed

Lines changed: 16 additions & 17 deletions

File tree

Sources/CoreModel/Decodable.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ extension Data: AttributeDecodable {
161161
}
162162
}
163163

164+
extension Decimal: AttributeDecodable {
165+
166+
public init?(attributeValue: AttributeValue) {
167+
guard case let .decimal(value) = attributeValue else {
168+
return nil
169+
}
170+
self = value
171+
}
172+
}
173+
164174
extension Float: AttributeDecodable {
165175

166176
public init?(attributeValue: AttributeValue) {

Sources/CoreModel/Decoder.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,6 @@ internal extension ModelDataDecoder {
192192
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Cannot decode \(type) from identifier \(id)"))
193193
}
194194
return value as! T
195-
} else if type == Data.self {
196-
return try decodeAttribute(Data.self, forKey: key) as! T
197-
} else if type == Date.self {
198-
return try decodeAttribute(Date.self, forKey: key) as! T
199-
} else if type == UUID.self {
200-
return try decodeAttribute(UUID.self, forKey: key) as! T
201-
} else if type == URL.self {
202-
return try decodeAttribute(URL.self, forKey: key) as! T
203195
} else if let decodableType = type as? AttributeDecodable.Type {
204196
return try decodeAttribute(decodableType, forKey: key) as! T
205197
} else {

Sources/CoreModel/Encodable.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,8 @@ extension URL: AttributeEncodable {
147147

148148
public var attributeValue: AttributeValue { .url(self) }
149149
}
150+
151+
extension Decimal: AttributeEncodable {
152+
153+
public var attributeValue: AttributeValue { .decimal(self) }
154+
}

Sources/CoreModel/Encoder.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,7 @@ internal extension ModelDataEncoder {
123123

124124
func setEncodable <T: Encodable> (_ value: T, forKey key: PropertyKey) throws {
125125

126-
if let data = value as? Data {
127-
try setAttribute(data.attributeValue, forKey: key)
128-
} else if let date = value as? Date {
129-
try setAttribute(date.attributeValue, forKey: key)
130-
} else if let uuid = value as? UUID {
131-
try setAttribute(uuid.attributeValue, forKey: key)
132-
} else if let url = value as? URL {
133-
try setAttribute(url.attributeValue, forKey: key)
134-
} else if let encodable = value as? AttributeEncodable {
126+
if let encodable = value as? AttributeEncodable {
135127
try setAttribute(encodable.attributeValue, forKey: key)
136128
} else {
137129
// encode using Encodable, container should write directly.

0 commit comments

Comments
 (0)