From c20fe4ac17e7cbd7f826fbb37fa78911e65d3bc3 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Wed, 5 Feb 2020 11:03:57 +0900 Subject: [PATCH] add Field.Relationship dynamicMemberLookups --- Sources/FIeldRelationshipType.swift | 42 +++++++++++++++++++++++++++++ Sources/ObjectPublisher.swift | 18 +++++++++++++ Sources/ObjectSnapshot.swift | 19 +++++++++++++ 3 files changed, 79 insertions(+) diff --git a/Sources/FIeldRelationshipType.swift b/Sources/FIeldRelationshipType.swift index f054adc..b19e751 100644 --- a/Sources/FIeldRelationshipType.swift +++ b/Sources/FIeldRelationshipType.swift @@ -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? + 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] + 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 + public typealias PublishedType = Set> + 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 { diff --git a/Sources/ObjectPublisher.swift b/Sources/ObjectPublisher.swift index 5426925..9549851 100644 --- a/Sources/ObjectPublisher.swift +++ b/Sources/ObjectPublisher.swift @@ -397,6 +397,24 @@ extension ObjectPublisher where O: CoreStoreObject { return FieldContainer.Computed.read(field: object[keyPath: member], for: rawObject) as! V? } + /** + Returns the value for the property identified by a given key. + */ + public subscript(dynamicMember member: KeyPath.Relationship>) -> V.PublishedType? { + + guard + let object = self.object, + let rawObject = object.rawObject, + let value = FieldContainer.Relationship.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. */ diff --git a/Sources/ObjectSnapshot.swift b/Sources/ObjectSnapshot.swift index 0845636..cfac82d 100644 --- a/Sources/ObjectSnapshot.swift +++ b/Sources/ObjectSnapshot.swift @@ -209,6 +209,25 @@ extension ObjectSnapshot where O: CoreStoreObject { } } + /** + Returns the value for the property identified by a given key. + */ + public subscript(dynamicMember member: KeyPath.Relationship>) -> 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. */