Skip to content

Commit f5d9d1d

Browse files
committed
Add Decimal support #8
1 parent ecf6ebe commit f5d9d1d

5 files changed

Lines changed: 21 additions & 1 deletion

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ extension Int: AttributeDecodable {
192192
.data,
193193
.date,
194194
.bool,
195+
.decimal,
195196
.float,
196197
.double:
197198
return nil
@@ -216,6 +217,7 @@ extension Int8: AttributeDecodable {
216217
.data,
217218
.date,
218219
.bool,
220+
.decimal,
219221
.float,
220222
.double:
221223
return nil
@@ -240,6 +242,7 @@ extension Int16: AttributeDecodable {
240242
.data,
241243
.date,
242244
.bool,
245+
.decimal,
243246
.float,
244247
.double:
245248
return nil
@@ -264,6 +267,7 @@ extension Int32: AttributeDecodable {
264267
.data,
265268
.date,
266269
.bool,
270+
.decimal,
267271
.float,
268272
.double:
269273
return nil
@@ -288,6 +292,7 @@ extension Int64: AttributeDecodable {
288292
.data,
289293
.date,
290294
.bool,
295+
.decimal,
291296
.float,
292297
.double:
293298
return nil
@@ -312,6 +317,7 @@ extension UInt: AttributeDecodable {
312317
.data,
313318
.date,
314319
.bool,
320+
.decimal,
315321
.float,
316322
.double:
317323
return nil
@@ -337,6 +343,7 @@ extension UInt8: AttributeDecodable {
337343
.data,
338344
.date,
339345
.bool,
346+
.decimal,
340347
.float,
341348
.double:
342349
return nil
@@ -362,6 +369,7 @@ extension UInt16: AttributeDecodable {
362369
.data,
363370
.date,
364371
.bool,
372+
.decimal,
365373
.float,
366374
.double:
367375
return nil
@@ -387,6 +395,7 @@ extension UInt32: AttributeDecodable {
387395
.data,
388396
.date,
389397
.bool,
398+
.decimal,
390399
.float,
391400
.double:
392401
return nil
@@ -411,6 +420,7 @@ extension UInt64: AttributeDecodable {
411420
.data,
412421
.date,
413422
.bool,
423+
.decimal,
414424
.float,
415425
.double:
416426
return nil

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)