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

@@ -37,10 +37,16 @@ public protocol FieldRelationshipType {
associatedtype SnapshotValueType
associatedtype PublishedType
static func cs_toReturnType(from value: NativeValueType?) -> Self
static func cs_toPublishedType(from value: SnapshotValueType, in context: NSManagedObjectContext) -> PublishedType
static func cs_toNativeType(from value: Self) -> NativeValueType?
static func cs_toSnapshotType(from value: PublishedType) -> SnapshotValueType
static func cs_valueForSnapshot(from value: NativeValueType?) -> SnapshotValueType
}
@@ -60,16 +66,28 @@ extension Optional: FieldRelationshipType, FieldRelationshipToOneType where Wrap
public typealias SnapshotValueType = NSManagedObjectID?
public typealias PublishedType = ObjectPublisher<DestinationObjectType>?
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
return value.map(Wrapped.cs_fromRaw(object:))
}
public static func cs_toPublishedType(from value: SnapshotValueType, in context: NSManagedObjectContext) -> PublishedType {
return value.map(context.objectPublisher(objectID:))
}
public static func cs_toNativeType(from value: Self) -> NativeValueType? {
return value?.cs_toRaw()
}
public static func cs_toSnapshotType(from value: PublishedType) -> SnapshotValueType {
return value?.objectID()
}
public static func cs_valueForSnapshot(from value: NativeValueType?) -> SnapshotValueType {
return value?.objectID
@@ -85,6 +103,8 @@ extension Array: FieldRelationshipType, FieldRelationshipToManyType, FieldRelati
public typealias SnapshotValueType = [NSManagedObjectID]
public typealias PublishedType = [ObjectPublisher<DestinationObjectType>]
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
guard let value = value else {
@@ -94,11 +114,21 @@ extension Array: FieldRelationshipType, FieldRelationshipToManyType, FieldRelati
return value.map({ Element.cs_fromRaw(object: $0 as! NSManagedObject) })
}
public static func cs_toPublishedType(from value: SnapshotValueType, in context: NSManagedObjectContext) -> PublishedType {
return value.map(context.objectPublisher(objectID:))
}
public static func cs_toNativeType(from value: Self) -> NativeValueType? {
return NSOrderedSet(array: value.map({ $0.rawObject! }))
}
public static func cs_toSnapshotType(from value: PublishedType) -> SnapshotValueType {
return value.map({ $0.objectID() })
}
public static func cs_valueForSnapshot(from value: NativeValueType?) -> SnapshotValueType {
guard let value = value else {
@@ -117,6 +147,8 @@ extension Set: FieldRelationshipType, FieldRelationshipToManyType, FieldRelation
public typealias SnapshotValueType = Set<NSManagedObjectID>
public typealias PublishedType = Set<ObjectPublisher<DestinationObjectType>>
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
guard let value = value else {
@@ -126,11 +158,21 @@ extension Set: FieldRelationshipType, FieldRelationshipToManyType, FieldRelation
return Set(value.map({ Element.cs_fromRaw(object: $0 as! NSManagedObject) }))
}
public static func cs_toPublishedType(from value: SnapshotValueType, in context: NSManagedObjectContext) -> PublishedType {
return PublishedType(value.map(context.objectPublisher(objectID:)))
}
public static func cs_toNativeType(from value: Self) -> NativeValueType? {
return NSSet(array: value.map({ $0.rawObject! }))
}
public static func cs_toSnapshotType(from value: PublishedType) -> SnapshotValueType {
return SnapshotValueType(value.map({ $0.objectID() }))
}
public static func cs_valueForSnapshot(from value: NativeValueType?) -> SnapshotValueType {
guard let value = value else {

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.
*/

View File

@@ -209,6 +209,25 @@ 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>.Relationship<V>>) -> V.PublishedType {
get {
let key = String(keyPath: member)
let context = self.context
let snapshotValue = self.values[key] as! V.SnapshotValueType
return V.cs_toPublishedType(from: snapshotValue, in: context)
}
set {
let key = String(keyPath: member)
self.values[key] = V.cs_toSnapshotType(from: newValue)
}
}
/**
Returns the value for the property identified by a given key.
*/