|
5 | 5 | // Created by Alsey Coleman Miller on 8/17/23. |
6 | 6 | // |
7 | 7 |
|
| 8 | +#if canImport(FoundationEssentials) |
| 9 | +import FoundationEssentials |
| 10 | +#elseif canImport(Foundation) |
8 | 11 | import Foundation |
| 12 | +#endif |
9 | 13 |
|
10 | 14 | // MARK: - ModelData Decoding |
11 | 15 |
|
12 | 16 | public extension ModelData { |
13 | | - |
| 17 | + |
14 | 18 | func decode<T, K>(_ type: T.Type, forKey key: K) throws -> T where T: AttributeDecodable, K: CodingKey { |
15 | | - |
| 19 | + |
16 | 20 | let property = PropertyKey(key) |
17 | 21 | guard let attribute = self.attributes[property] else { |
18 | | - throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: [], debugDescription: "Key \(key.stringValue) not found")) |
| 22 | + throw coreModelKeyNotFoundError(key) |
19 | 23 | } |
20 | 24 | // TODO: Optional values |
21 | 25 | /* |
22 | 26 | guard attribute != .null else { |
23 | 27 | return |
24 | 28 | }*/ |
25 | 29 | guard let decodable = type.init(attributeValue: attribute) else { |
26 | | - throw DecodingError.typeMismatch(type, DecodingError.Context(codingPath: [], debugDescription: "Cannot decode \(String(describing: type)) from \(attribute)")) |
| 30 | + throw coreModelTypeMismatchError(type, forKey: key, from: attribute) |
27 | 31 | } |
28 | 32 | return decodable |
29 | 33 | } |
30 | | - |
| 34 | + |
31 | 35 | func decodeRelationship<T, K>(_ type: T.Type, forKey key: K) throws -> T where T: ObjectIDConvertible, K: CodingKey { |
32 | | - |
| 36 | + |
33 | 37 | let property = PropertyKey(key) |
34 | 38 | guard let relationship = self.relationships[property] else { |
35 | | - throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: [], debugDescription: "Key \(key.stringValue) not found")) |
| 39 | + throw coreModelKeyNotFoundError(key) |
36 | 40 | } |
37 | 41 | switch relationship { |
38 | 42 | case .null: |
39 | | - throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: [], debugDescription: "Key \(key.stringValue) not found")) |
| 43 | + throw coreModelKeyNotFoundError(key) |
40 | 44 | case .toMany: |
41 | | - throw DecodingError.typeMismatch(type, DecodingError.Context(codingPath: [], debugDescription: "Cannot decode \(String(describing: type)) from \(relationship)")) |
| 45 | + throw coreModelTypeMismatchError(type, forKey: key, from: relationship) |
42 | 46 | case let .toOne(objectID): |
43 | 47 | guard let id = type.init(objectID: objectID) else { |
44 | | - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Cannot decode identifier from \(objectID)")) |
| 48 | + throw coreModelInvalidIdentifierError(objectID) |
45 | 49 | } |
46 | 50 | return id |
47 | 51 | } |
48 | 52 | } |
49 | | - |
| 53 | + |
50 | 54 | func decodeRelationship<T, K>(_ type: [T].Type, forKey key: K) throws -> [T] where T: ObjectIDConvertible, K: CodingKey { |
51 | | - |
| 55 | + |
52 | 56 | let property = PropertyKey(key) |
53 | 57 | guard let relationship = self.relationships[property] else { |
54 | | - throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: [], debugDescription: "Key \(key.stringValue) not found")) |
| 58 | + throw coreModelKeyNotFoundError(key) |
55 | 59 | } |
56 | 60 | switch relationship { |
57 | 61 | case .null: |
58 | 62 | return [] |
59 | 63 | case .toOne: |
60 | | - throw DecodingError.typeMismatch(type, DecodingError.Context(codingPath: [], debugDescription: "Cannot decode \(String(describing: type)) from \(relationship)")) |
| 64 | + throw coreModelTypeMismatchError(type, forKey: key, from: relationship) |
61 | 65 | case let .toMany(objectIDs): |
62 | 66 | return try objectIDs.map { |
63 | 67 | guard let id = T.init(objectID: $0) else { |
64 | | - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Cannot decode identifier from \($0)")) |
| 68 | + throw coreModelInvalidIdentifierError($0) |
65 | 69 | } |
66 | 70 | return id |
67 | 71 | } |
|
0 commit comments