Skip to content

Commit

Permalink
Merge pull request #9 from PureSwift/feature/decimal
Browse files Browse the repository at this point in the history
Add `Decimal` support #8
  • Loading branch information
colemancda authored Aug 19, 2023
2 parents ecf6ebe + b85eabf commit 85923fb
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
4 changes: 3 additions & 1 deletion Sources/CoreDataModel/NSAttributeType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public extension NSAttributeType {
self = .UUIDAttributeType
case .url:
self = .URIAttributeType
case .decimal:
self = .decimalAttributeType
}
}
}
Expand All @@ -53,7 +55,7 @@ public extension AttributeType {
case .integer64AttributeType:
self = .int64
case .decimalAttributeType:
return nil
self = .decimal
case .doubleAttributeType:
self = .double
case .floatAttributeType:
Expand Down
4 changes: 4 additions & 0 deletions Sources/CoreDataModel/NSManagedObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ internal extension NSManagedObject {
return .float(value)
} else if let value = objectValue as? Double {
return .double(value)
} else if let value = objectValue as? NSDecimalNumber {
return .decimal(value as Decimal)
} else {
assertionFailure("Invalid CoreData attribute value \(objectValue)")
throw CocoaError(.coreData)
Expand Down Expand Up @@ -82,6 +84,8 @@ internal extension NSManagedObject {
objectValue = value as NSNumber
case let .double(value):
objectValue = value as NSNumber
case let .decimal(value):
objectValue = value as NSDecimalNumber
}

self.setValue(objectValue, forKey: key.rawValue)
Expand Down
3 changes: 3 additions & 0 deletions Sources/CoreModel/AttributeType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ public enum AttributeType: String, Codable, CaseIterable, Sendable {

/// URL
case url

/// Decimal
case decimal
}
20 changes: 20 additions & 0 deletions Sources/CoreModel/Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ extension Data: AttributeDecodable {
}
}

extension Decimal: AttributeDecodable {

public init?(attributeValue: AttributeValue) {
guard case let .decimal(value) = attributeValue else {
return nil
}
self = value
}
}

extension Float: AttributeDecodable {

public init?(attributeValue: AttributeValue) {
Expand Down Expand Up @@ -192,6 +202,7 @@ extension Int: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -216,6 +227,7 @@ extension Int8: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -240,6 +252,7 @@ extension Int16: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -264,6 +277,7 @@ extension Int32: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -288,6 +302,7 @@ extension Int64: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -312,6 +327,7 @@ extension UInt: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -337,6 +353,7 @@ extension UInt8: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -362,6 +379,7 @@ extension UInt16: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -387,6 +405,7 @@ extension UInt32: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand All @@ -411,6 +430,7 @@ extension UInt64: AttributeDecodable {
.data,
.date,
.bool,
.decimal,
.float,
.double:
return nil
Expand Down
8 changes: 0 additions & 8 deletions Sources/CoreModel/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,6 @@ internal extension ModelDataDecoder {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Cannot decode \(type) from identifier \(id)"))
}
return value as! T
} else if type == Data.self {
return try decodeAttribute(Data.self, forKey: key) as! T
} else if type == Date.self {
return try decodeAttribute(Date.self, forKey: key) as! T
} else if type == UUID.self {
return try decodeAttribute(UUID.self, forKey: key) as! T
} else if type == URL.self {
return try decodeAttribute(URL.self, forKey: key) as! T
} else if let decodableType = type as? AttributeDecodable.Type {
return try decodeAttribute(decodableType, forKey: key) as! T
} else {
Expand Down
5 changes: 5 additions & 0 deletions Sources/CoreModel/Encodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,8 @@ extension URL: AttributeEncodable {

public var attributeValue: AttributeValue { .url(self) }
}

extension Decimal: AttributeEncodable {

public var attributeValue: AttributeValue { .decimal(self) }
}
10 changes: 1 addition & 9 deletions Sources/CoreModel/Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,7 @@ internal extension ModelDataEncoder {

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

if let data = value as? Data {
try setAttribute(data.attributeValue, forKey: key)
} else if let date = value as? Date {
try setAttribute(date.attributeValue, forKey: key)
} else if let uuid = value as? UUID {
try setAttribute(uuid.attributeValue, forKey: key)
} else if let url = value as? URL {
try setAttribute(url.attributeValue, forKey: key)
} else if let encodable = value as? AttributeEncodable {
if let encodable = value as? AttributeEncodable {
try setAttribute(encodable.attributeValue, forKey: key)
} else {
// encode using Encodable, container should write directly.
Expand Down
1 change: 1 addition & 0 deletions Sources/CoreModel/PropertyValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum AttributeValue: Equatable, Hashable, Codable {
case int64(Int64)
case float(Float)
case double(Double)
case decimal(Decimal)
}

/// CoreModel Relationship Value
Expand Down

0 comments on commit 85923fb

Please sign in to comment.