-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ComplexTypes.swift
299 lines (262 loc) · 12.3 KB
/
ComplexTypes.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2021 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
import Realm
import Realm.Private
extension Object: SchemaDiscoverable, _PersistableInsideOptional, _DefaultConstructible {
public typealias PersistedType = Object
public static var _rlmType: PropertyType { .object }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
if !prop.optional && !prop.collection {
throwRealmException("Object property '\(prop.name)' must be marked as optional.")
}
if prop.optional && prop.array {
throwRealmException("List<\(className())> property '\(prop.name)' must not be marked as optional.")
}
if prop.optional && prop.set {
throwRealmException("MutableSet<\(className())> property '\(prop.name)' must not be marked as optional.")
}
if !prop.optional && prop.dictionary {
throwRealmException("Map<String, \(className())> property '\(prop.name)' must be marked as optional.")
}
prop.objectClassName = className()
}
public static func _rlmGetProperty(_ obj: ObjectBase, _ key: UInt16) -> Self {
fatalError("Non-optional Object properties are not allowed.")
}
public static func _rlmGetPropertyOptional(_ obj: ObjectBase, _ key: UInt16) -> Self? {
// FIXME: gives Assertion failed: (LocalSelf && "no local self metadata"), function getLocalSelfMetadata, file /src/swift-source/swift/lib/IRGen/GenHeap.cpp, line 1686.
// return RLMGetSwiftPropertyObject(obj, key).map(dynamicBridgeCast)
if let value = RLMGetSwiftPropertyObject(obj, key) {
return (value as! Self)
}
return nil
}
public static func _rlmSetProperty(_ obj: ObjectBase, _ key: UInt16, _ value: Object) {
RLMSetSwiftPropertyObject(obj, key, value)
}
}
extension EmbeddedObject: SchemaDiscoverable, _PersistableInsideOptional, _DefaultConstructible {
public typealias PersistedType = EmbeddedObject
public static var _rlmType: PropertyType { .object }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
Object._rlmPopulateProperty(prop)
prop.objectClassName = className()
}
public static func _rlmGetProperty(_ obj: ObjectBase, _ key: UInt16) -> Self {
if let value = RLMGetSwiftPropertyObject(obj, key) {
return value as! Self
}
return Self()
}
public static func _rlmGetPropertyOptional(_ obj: ObjectBase, _ key: UInt16) -> Self? {
if let value = RLMGetSwiftPropertyObject(obj, key) {
return (value as! Self)
}
return nil
}
public static func _rlmSetProperty(_ obj: ObjectBase, _ key: UInt16, _ value: EmbeddedObject) {
RLMSetSwiftPropertyObject(obj, key, value)
}
}
extension List: _RealmSchemaDiscoverable, SchemaDiscoverable where Element: _RealmSchemaDiscoverable {
public static var _rlmType: PropertyType { Element._rlmType }
public static var _rlmOptional: Bool { Element._rlmOptional }
public static var _rlmRequireObjc: Bool { false }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
prop.array = true
prop.swiftAccessor = ListAccessor<Element>.self
Element._rlmPopulateProperty(prop)
}
}
extension List: _HasPersistedType, _Persistable, _DefaultConstructible where Element: _Persistable {
public typealias PersistedType = List
public static var _rlmRequiresCaching: Bool { true }
public static func _rlmGetProperty(_ obj: ObjectBase, _ key: UInt16) -> Self {
return Self(collection: RLMGetSwiftPropertyArray(obj, key))
}
public static func _rlmSetProperty(_ obj: ObjectBase, _ key: UInt16, _ value: List) {
let array = RLMGetSwiftPropertyArray(obj, key)
if array.isEqual(value.rlmArray) { return }
array.removeAllObjects()
array.addObjects(value.rlmArray)
}
public static func _rlmSetAccessor(_ prop: RLMProperty) {
prop.swiftAccessor = PersistedListAccessor<Element>.self
}
}
extension MutableSet: _RealmSchemaDiscoverable, SchemaDiscoverable where Element: _RealmSchemaDiscoverable {
public static var _rlmType: PropertyType { Element._rlmType }
public static var _rlmOptional: Bool { Element._rlmOptional }
public static var _rlmRequireObjc: Bool { false }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
prop.set = true
prop.swiftAccessor = SetAccessor<Element>.self
Element._rlmPopulateProperty(prop)
}
}
extension MutableSet: _HasPersistedType, _Persistable, _DefaultConstructible where Element: _Persistable {
public typealias PersistedType = MutableSet
public static var _rlmRequiresCaching: Bool { true }
public static func _rlmGetProperty(_ obj: ObjectBase, _ key: UInt16) -> Self {
return Self(collection: RLMGetSwiftPropertySet(obj, key))
}
public static func _rlmSetProperty(_ obj: ObjectBase, _ key: UInt16, _ value: MutableSet) {
let set = RLMGetSwiftPropertySet(obj, key)
if set.isEqual(value.rlmSet) { return }
set.removeAllObjects()
set.addObjects(value.rlmSet)
}
public static func _rlmSetAccessor(_ prop: RLMProperty) {
prop.swiftAccessor = PersistedSetAccessor<Element>.self
}
}
extension Map: _RealmSchemaDiscoverable, SchemaDiscoverable where Value: _RealmSchemaDiscoverable {
public static var _rlmType: PropertyType { Value._rlmType }
public static var _rlmOptional: Bool { Value._rlmOptional }
public static var _rlmRequireObjc: Bool { false }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
prop.dictionary = true
prop.swiftAccessor = MapAccessor<Key, Value>.self
prop.dictionaryKeyType = Key._rlmType
Value._rlmPopulateProperty(prop)
}
}
extension Map: _HasPersistedType, _Persistable, _DefaultConstructible where Value: _Persistable {
public typealias PersistedType = Map
public static var _rlmRequiresCaching: Bool { true }
public static func _rlmGetProperty(_ obj: ObjectBase, _ key: UInt16) -> Self {
return Self(objc: RLMGetSwiftPropertyMap(obj, key))
}
public static func _rlmSetProperty(_ obj: ObjectBase, _ key: UInt16, _ value: Map) {
let map = RLMGetSwiftPropertyMap(obj, key)
if map.isEqual(value.rlmDictionary) { return }
map.removeAllObjects()
map.addEntries(fromDictionary: value.rlmDictionary)
}
public static func _rlmSetAccessor(_ prop: RLMProperty) {
prop.swiftAccessor = PersistedMapAccessor<Key, Value>.self
}
}
extension LinkingObjects: SchemaDiscoverable {
public static var _rlmType: PropertyType { .linkingObjects }
public static var _rlmRequireObjc: Bool { false }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
prop.array = true
prop.objectClassName = Element.className()
prop.swiftAccessor = LinkingObjectsAccessor<Element>.self
if prop.linkOriginPropertyName == nil {
throwRealmException("LinkingObjects<\(prop.objectClassName!)> property '\(prop.name)' must set the origin property name with @Persisted(originProperty: \"name\").")
}
}
public func _rlmPopulateProperty(_ prop: RLMProperty) {
prop.linkOriginPropertyName = self.propertyName
}
}
@available(*, deprecated)
extension RealmOptional: SchemaDiscoverable, _RealmSchemaDiscoverable where Value: _RealmSchemaDiscoverable {
public static var _rlmType: PropertyType { Value._rlmType }
public static var _rlmOptional: Bool { true }
public static var _rlmRequireObjc: Bool { false }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
Value._rlmPopulateProperty(prop)
prop.swiftAccessor = RealmOptionalAccessor<Value>.self
}
}
extension LinkingObjects: _HasPersistedType, _Persistable where Element: _Persistable {
public typealias PersistedType = Self
public static func _rlmDefaultValue() -> Self {
fatalError("LinkingObjects properties must set the origin property name")
}
public static func _rlmGetProperty(_ obj: ObjectBase, _ key: UInt16) -> LinkingObjects {
let prop = RLMObjectBaseObjectSchema(obj)!.computedProperties[Int(key)]
return Self(propertyName: prop.name, handle: RLMLinkingObjectsHandle(object: obj, property: prop))
}
public static func _rlmSetProperty(_ obj: ObjectBase, _ key: UInt16, _ value: LinkingObjects) {
fatalError("LinkingObjects properties are read-only")
}
public static func _rlmSetAccessor(_ prop: RLMProperty) {
prop.swiftAccessor = PersistedLinkingObjectsAccessor<Element>.self
}
}
extension Optional: SchemaDiscoverable, _RealmSchemaDiscoverable where Wrapped: _RealmSchemaDiscoverable {
public static var _rlmType: PropertyType { Wrapped._rlmType }
public static var _rlmOptional: Bool { true }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
Wrapped._rlmPopulateProperty(prop)
}
}
extension Optional: _HasPersistedType where Wrapped: _HasPersistedType {
public typealias PersistedType = Wrapped.PersistedType?
}
extension Optional: _Persistable where Wrapped: _PersistableInsideOptional {
public static func _rlmDefaultValue() -> Self {
return .none
}
public static func _rlmGetProperty(_ obj: ObjectBase, _ key: UInt16) -> Wrapped? {
return Wrapped._rlmGetPropertyOptional(obj, key)
}
public static func _rlmSetProperty(_ obj: ObjectBase, _ key: UInt16, _ value: Wrapped?) {
if let value = value {
Wrapped._rlmSetProperty(obj, key, value)
} else {
RLMSetSwiftPropertyNil(obj, key)
}
}
public static func _rlmSetAccessor(_ prop: RLMProperty) {
Wrapped._rlmSetAccessor(prop)
}
}
extension Optional: _PrimaryKey where Wrapped: _Persistable, Wrapped.PersistedType: _PrimaryKey {}
extension Optional: _Indexable where Wrapped: _Persistable, Wrapped.PersistedType: _Indexable {}
extension RealmProperty: _RealmSchemaDiscoverable, SchemaDiscoverable where Value: _RealmSchemaDiscoverable {
public static var _rlmType: PropertyType { Value._rlmType }
public static var _rlmOptional: Bool { Value._rlmOptional }
public static var _rlmRequireObjc: Bool { false }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
Value._rlmPopulateProperty(prop)
prop.swiftAccessor = RealmPropertyAccessor<Value>.self
}
}
extension RawRepresentable where RawValue: _RealmSchemaDiscoverable {
public static var _rlmType: PropertyType { RawValue._rlmType }
public static var _rlmOptional: Bool { RawValue._rlmOptional }
public static var _rlmRequireObjc: Bool { false }
public func _rlmPopulateProperty(_ prop: RLMProperty) { }
public static func _rlmPopulateProperty(_ prop: RLMProperty) {
RawValue._rlmPopulateProperty(prop)
}
}
extension RawRepresentable where Self: _PersistableInsideOptional, RawValue: _PersistableInsideOptional {
public typealias PersistedType = RawValue
public static func _rlmGetProperty(_ obj: ObjectBase, _ key: PropertyKey) -> Self {
return Self(rawValue: RawValue._rlmGetProperty(obj, key))!
}
public static func _rlmGetPropertyOptional(_ obj: ObjectBase, _ key: PropertyKey) -> Self? {
return RawValue._rlmGetPropertyOptional(obj, key).flatMap(Self.init)
}
public static func _rlmSetProperty(_ obj: ObjectBase, _ key: PropertyKey, _ value: Self) {
RawValue._rlmSetProperty(obj, key, value.rawValue)
}
public static func _rlmSetAccessor(_ prop: RLMProperty) {
if prop.optional {
prop.swiftAccessor = BridgedPersistedPropertyAccessor<Optional<Self>>.self
} else {
prop.swiftAccessor = BridgedPersistedPropertyAccessor<Self>.self
}
}
}