forked from prisma-ai/Sworm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManagedObject.swift
125 lines (101 loc) · 3.85 KB
/
ManagedObject.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import CoreData
@dynamicMemberLookup
public final class ManagedObject<PlainObject: ManagedObjectConvertible> {
internal init(instance: NSManagedObject) {
self.instance = instance
}
public func decode() throws -> PlainObject {
try .init(from: self.instance)
}
public func decode<Attribute: SupportedAttributeType>(
_ keyPath: KeyPath<PlainObject, Attribute>
) throws -> Attribute {
try Attribute.decode(self.instance[
primitiveValue: PlainObject.attribute(keyPath).name
])
}
public func decode<Attribute: SupportedAttributeType>(
_ keyPath: KeyPath<PlainObject, Attribute?>
) throws -> Attribute? {
try Attribute?.decode(self.instance[
primitiveValue: PlainObject.attribute(keyPath).name
])
}
@discardableResult
public func encode(_ value: PlainObject) -> Self {
value.encodeAttributes(to: self.instance)
return self
}
@discardableResult
public func encode<Attribute: SupportedAttributeType>(
_ keyPath: KeyPath<PlainObject, Attribute>,
_ value: Attribute
) -> Self {
self.instance[primitiveValue: PlainObject.attribute(keyPath).name] = value.encodePrimitiveValue()
return self
}
@discardableResult
public func encode<Attribute: SupportedAttributeType>(
_ keyPath: KeyPath<PlainObject, Attribute?>,
_ value: Attribute?
) -> Self {
self.instance[primitiveValue: PlainObject.attribute(keyPath).name] = value?.encodePrimitiveValue()
return self
}
public subscript<Destination: ManagedObjectConvertible>(
dynamicMember keyPath: KeyPath<PlainObject.Relations, ToOneRelation<Destination>>
) -> ManagedObject<Destination>? {
get {
let destination = PlainObject.relations[keyPath: keyPath]
return (self.instance[primitiveValue: destination.name] as? NSManagedObject).flatMap {
.init(instance: $0)
}
}
set {
let destination = PlainObject.relations[keyPath: keyPath]
self.instance[primitiveValue: destination.name] = newValue?.instance
}
}
public subscript<Destination: ManagedObjectConvertible>(
dynamicMember keyPath: KeyPath<PlainObject.Relations, ToManyRelation<Destination>>
) -> ManagedObjectSet<Destination> {
let destination = PlainObject.relations[keyPath: keyPath]
return .init(name: destination.name, instance: self.instance)
}
public subscript<Destination: ManagedObjectConvertible>(
dynamicMember keyPath: KeyPath<PlainObject.Relations, ToManyOrderedRelation<Destination>>
) -> ManagedObjectOrderedSet<Destination> {
let destination = PlainObject.relations[keyPath: keyPath]
return .init(name: destination.name, instance: self.instance)
}
unowned let instance: NSManagedObject
}
public extension ManagedObject {
@discardableResult
func set<Destination: ManagedObjectConvertible>(
_ keyPath: KeyPath<PlainObject.Relations, ToOneRelation<Destination>>,
value: Destination?,
context: ManagedObjectContext
) throws -> Self {
guard let value = value else {
return self.delete(keyPath, context: context)
}
if let object = self[dynamicMember: keyPath] {
object.encode(value)
} else {
self[dynamicMember: keyPath] = try context.insert(value)
}
return self
}
@discardableResult
func delete<Destination: ManagedObjectConvertible>(
_ keyPath: KeyPath<PlainObject.Relations, ToOneRelation<Destination>>,
context: ManagedObjectContext
) -> Self {
if let object = self[dynamicMember: keyPath] {
self[dynamicMember: keyPath] = nil
context.delete(object)
}
return self
}
}