add Field.Coded dynamic lookups for ObjectPublisher and ObjectSnapshot

This commit is contained in:
John Estropia
2020-02-17 18:30:18 +09:00
parent 38e9878c04
commit 2d1b1e0592
3 changed files with 46 additions and 1 deletions

View File

@@ -397,6 +397,21 @@ extension ObjectPublisher where O: CoreStoreObject {
return FieldContainer<OBase>.Virtual<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>.Coded<V>>) -> V? {
guard
let object = self.object,
let rawObject = object.rawObject
else {
return nil
}
return FieldContainer<OBase>.Coded<V>.read(field: object[keyPath: member], for: rawObject) as! V?
}
/**
Returns the value for the property identified by a given key.
*/

View File

@@ -209,6 +209,23 @@ extension ObjectSnapshot where O: CoreStoreObject {
}
}
/**
Returns the value for the property identified by a given key.
*/
public subscript<OBase, V>(dynamicMember member: KeyPath<O, FieldContainer<OBase>.Coded<V>>) -> V {
get {
let key = String(keyPath: member)
return self.values[key] as! V
}
set {
let key = String(keyPath: member)
self.values[key] = newValue
}
}
/**
Returns the value for the property identified by a given key.
*/