add Field.Relationship dynamicMemberLookups

This commit is contained in:
John Estropia
2020-02-05 11:03:57 +09:00
parent e9c3312612
commit c20fe4ac17
3 changed files with 79 additions and 0 deletions

View File

@@ -397,6 +397,24 @@ extension ObjectPublisher where O: CoreStoreObject {
return FieldContainer<OBase>.Computed<V>.read(field: object[keyPath: member], for: rawObject) as! V?
}
/**
Returns the value for the property identified by a given key.
*/
public subscript<OBase, V>(dynamicMember member: KeyPath<O, FieldContainer<OBase>.Relationship<V>>) -> V.PublishedType? {
guard
let object = self.object,
let rawObject = object.rawObject,
let value = FieldContainer<OBase>.Relationship<V>.read(field: object[keyPath: member], for: rawObject) as! V?
else {
return nil
}
let nativeValue = V.cs_toNativeType(from: value)
let snapshotValue = V.cs_valueForSnapshot(from: nativeValue)
return V.cs_toPublishedType(from: snapshotValue, in: self.context)
}
/**
Returns the value for the property identified by a given key.
*/