Skip to content

Commit 85923fb

Browse files
authored
Merge pull request #9 from PureSwift/feature/decimal
Add `Decimal` support #8
2 parents ecf6ebe + b85eabf commit 85923fb

8 files changed

Lines changed: 37 additions & 18 deletions

File tree

Sources/CoreDataModel/NSAttributeType.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public extension NSAttributeType {
3636
self = .UUIDAttributeType
3737
case .url:
3838
self = .URIAttributeType
39+
case .decimal:
40+
self = .decimalAttributeType
3941
}
4042
}
4143
}
@@ -53,7 +55,7 @@ public extension AttributeType {
5355
case .integer64AttributeType:
5456
self = .int64
5557
case .decimalAttributeType:
56-
return nil
58+
self = .decimal
5759
case .doubleAttributeType:
5860
self = .double
5961
case .floatAttributeType:

Sources/CoreDataModel/NSManagedObject.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ internal extension NSManagedObject {
4747
return .float(value)
4848
} else if let value = objectValue as? Double {
4949
return .double(value)
50+
} else if let value = objectValue as? NSDecimalNumber {
51+
return .decimal(value as Decimal)
5052
} else {
5153
assertionFailure("Invalid CoreData attribute value \(objectValue)")
5254
throw CocoaError(.coreData)
@@ -82,6 +84,8 @@ internal extension NSManagedObject {
8284
objectValue = value as NSNumber
8385
case let .double(value):
8486
objectValue = value as NSNumber
87+
case let .decimal(value):
88+
objectValue = value as NSDecimalNumber
8589
}
8690

8791
self.setValue(objectValue, forKey: key.rawValue)

Sources/CoreModel/AttributeType.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ public enum AttributeType: String, Codable, CaseIterable, Sendable {
4242

4343
/// URL
4444
case url
45+
46+
/// Decimal
47+
case decimal
4548
}

Sources/CoreModel/Decodable.swift

Lines changed: 20 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) {
@@ -192,6 +202,7 @@ extension Int: AttributeDecodable {
192202
.data,
193203
.date,
194204
.bool,
205+
.decimal,
195206
.float,
196207
.double:
197208
return nil
@@ -216,6 +227,7 @@ extension Int8: AttributeDecodable {
216227
.data,
217228
.date,
218229
.bool,
230+
.decimal,
219231
.float,
220232
.double:
221233
return nil
@@ -240,6 +252,7 @@ extension Int16: AttributeDecodable {
240252
.data,
241253
.date,
242254
.bool,
255+
.decimal,
243256
.float,
244257
.double:
245258
return nil
@@ -264,6 +277,7 @@ extension Int32: AttributeDecodable {
264277
.data,
265278
.date,
266279
.bool,
280+
.decimal,
267281
.float,
268282
.double:
269283
return nil
@@ -288,6 +302,7 @@ extension Int64: AttributeDecodable {
288302
.data,
289303
.date,
290304
.bool,
305+
.decimal,
291306
.float,
292307
.double:
293308
return nil
@@ -312,6 +327,7 @@ extension UInt: AttributeDecodable {
312327
.data,
313328
.date,
314329
.bool,
330+
.decimal,
315331
.float,
316332
.double:
317333
return nil
@@ -337,6 +353,7 @@ extension UInt8: AttributeDecodable {
337353
.data,
338354
.date,
339355
.bool,
356+
.decimal,
340357
.float,
341358
.double:
342359
return nil
@@ -362,6 +379,7 @@ extension UInt16: AttributeDecodable {
362379
.data,
363380
.date,
364381
.bool,
382+
.decimal,
365383
.float,
366384
.double:
367385
return nil
@@ -387,6 +405,7 @@ extension UInt32: AttributeDecodable {
387405
.data,
388406
.date,
389407
.bool,
408+
.decimal,
390409
.float,
391410
.double:
392411
return nil
@@ -411,6 +430,7 @@ extension UInt64: AttributeDecodable {
411430
.data,
412431
.date,
413432
.bool,
433+
.decimal,
414434
.float,
415435
.double:
416436
return nil

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.

Sources/CoreModel/PropertyValue.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public enum AttributeValue: Equatable, Hashable, Codable {
2222
case int64(Int64)
2323
case float(Float)
2424
case double(Double)
25+
case decimal(Decimal)
2526
}
2627

2728
/// CoreModel Relationship Value

0 commit comments

Comments
 (0)