mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-20 16:44:08 +01:00
add Field.Relationship dynamicMemberLookups
This commit is contained in:
@@ -37,10 +37,16 @@ public protocol FieldRelationshipType {
|
|||||||
|
|
||||||
associatedtype SnapshotValueType
|
associatedtype SnapshotValueType
|
||||||
|
|
||||||
|
associatedtype PublishedType
|
||||||
|
|
||||||
static func cs_toReturnType(from value: NativeValueType?) -> Self
|
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_toNativeType(from value: Self) -> NativeValueType?
|
||||||
|
|
||||||
|
static func cs_toSnapshotType(from value: PublishedType) -> SnapshotValueType
|
||||||
|
|
||||||
static func cs_valueForSnapshot(from value: NativeValueType?) -> 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 SnapshotValueType = NSManagedObjectID?
|
||||||
|
|
||||||
|
public typealias PublishedType = ObjectPublisher<DestinationObjectType>?
|
||||||
|
|
||||||
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
|
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
|
||||||
|
|
||||||
return value.map(Wrapped.cs_fromRaw(object:))
|
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? {
|
public static func cs_toNativeType(from value: Self) -> NativeValueType? {
|
||||||
|
|
||||||
return value?.cs_toRaw()
|
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 {
|
public static func cs_valueForSnapshot(from value: NativeValueType?) -> SnapshotValueType {
|
||||||
|
|
||||||
return value?.objectID
|
return value?.objectID
|
||||||
@@ -85,6 +103,8 @@ extension Array: FieldRelationshipType, FieldRelationshipToManyType, FieldRelati
|
|||||||
|
|
||||||
public typealias SnapshotValueType = [NSManagedObjectID]
|
public typealias SnapshotValueType = [NSManagedObjectID]
|
||||||
|
|
||||||
|
public typealias PublishedType = [ObjectPublisher<DestinationObjectType>]
|
||||||
|
|
||||||
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
|
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
|
||||||
|
|
||||||
guard let value = value else {
|
guard let value = value else {
|
||||||
@@ -94,11 +114,21 @@ extension Array: FieldRelationshipType, FieldRelationshipToManyType, FieldRelati
|
|||||||
return value.map({ Element.cs_fromRaw(object: $0 as! NSManagedObject) })
|
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? {
|
public static func cs_toNativeType(from value: Self) -> NativeValueType? {
|
||||||
|
|
||||||
return NSOrderedSet(array: value.map({ $0.rawObject! }))
|
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 {
|
public static func cs_valueForSnapshot(from value: NativeValueType?) -> SnapshotValueType {
|
||||||
|
|
||||||
guard let value = value else {
|
guard let value = value else {
|
||||||
@@ -117,6 +147,8 @@ extension Set: FieldRelationshipType, FieldRelationshipToManyType, FieldRelation
|
|||||||
|
|
||||||
public typealias SnapshotValueType = Set<NSManagedObjectID>
|
public typealias SnapshotValueType = Set<NSManagedObjectID>
|
||||||
|
|
||||||
|
public typealias PublishedType = Set<ObjectPublisher<DestinationObjectType>>
|
||||||
|
|
||||||
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
|
public static func cs_toReturnType(from value: NativeValueType?) -> Self {
|
||||||
|
|
||||||
guard let value = value else {
|
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) }))
|
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? {
|
public static func cs_toNativeType(from value: Self) -> NativeValueType? {
|
||||||
|
|
||||||
return NSSet(array: value.map({ $0.rawObject! }))
|
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 {
|
public static func cs_valueForSnapshot(from value: NativeValueType?) -> SnapshotValueType {
|
||||||
|
|
||||||
guard let value = value else {
|
guard let value = value else {
|
||||||
|
|||||||
@@ -397,6 +397,24 @@ extension ObjectPublisher where O: CoreStoreObject {
|
|||||||
return FieldContainer<OBase>.Computed<V>.read(field: object[keyPath: member], for: rawObject) as! V?
|
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.
|
Returns the value for the property identified by a given key.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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.
|
Returns the value for the property identified by a given key.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user