NSManagedObject features are now fully supported for CoreStoreObject types. MacOSX 10.12 onwards now support ListMonitors and ObjectMonitors

This commit is contained in:
John Estropia
2017-04-07 20:14:13 +09:00
parent 4aa1a63f9a
commit c0ae129b22
58 changed files with 1172 additions and 803 deletions

View File

@@ -1,5 +1,5 @@
//
// ManagedObject.swift
// CoreStoreObject.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
@@ -27,9 +27,9 @@ import CoreData
import Foundation
// MARK: - ManagedObject
// MARK: - CoreStoreObject
open class ManagedObject: ManagedObjectProtocol, Hashable {
open class CoreStoreObject: DynamicObject, Hashable {
public required init(_ object: NSManagedObject) {
@@ -47,7 +47,7 @@ open class ManagedObject: ManagedObjectProtocol, Hashable {
// MARK: Equatable
public static func == (lhs: ManagedObject, rhs: ManagedObject) -> Bool {
public static func == (lhs: CoreStoreObject, rhs: CoreStoreObject) -> Bool {
guard lhs.isMeta == rhs.isMeta else {

View File

@@ -1,5 +1,5 @@
//
// ObjectModel.swift
// DynamicModel.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
@@ -27,9 +27,9 @@ import CoreGraphics
import Foundation
// MARK: - ObjectModel
// MARK: - DynamicModel
public final class ObjectModel {
public final class DynamicModel {
public convenience init(version: String, entities: [EntityProtocol]) {
@@ -74,7 +74,7 @@ public final class ObjectModel {
self.entityName = entity.entityName
}
internal init(type: ManagedObject.Type, entityName: String) {
internal init(type: CoreStoreObject.Type, entityName: String) {
self.type = type
self.entityName = entityName
@@ -99,7 +99,7 @@ public final class ObjectModel {
// MARK: EntityProtocol
internal let type: ManagedObject.Type
internal let type: CoreStoreObject.Type
internal let entityName: EntityName
}
@@ -116,12 +116,12 @@ public final class ObjectModel {
let entityDescription = ModelCache.entityDescription(
for: entity,
initializer: ObjectModel.firstPassCreateEntityDescription
initializer: DynamicModel.firstPassCreateEntityDescription
)
entityDescriptionsByEntity[entity] = entityDescription
}
ObjectModel.secondPassConnectRelationshipAttributes(for: entityDescriptionsByEntity)
ObjectModel.thirdPassConnectInheritanceTree(for: entityDescriptionsByEntity)
DynamicModel.secondPassConnectRelationshipAttributes(for: entityDescriptionsByEntity)
DynamicModel.thirdPassConnectInheritanceTree(for: entityDescriptionsByEntity)
return entityDescriptionsByEntity
}
model.entities = entityDescriptionsByEntity.values.sorted(by: { $0.name! < $1.name! })
@@ -147,7 +147,7 @@ public final class ObjectModel {
entityDescription.name = entity.entityName
entityDescription.managedObjectClassName = NSStringFromClass(NSManagedObject.self)
func createProperties(for type: ManagedObject.Type) -> [NSPropertyDescription] {
func createProperties(for type: CoreStoreObject.Type) -> [NSPropertyDescription] {
var propertyDescriptions: [NSPropertyDescription] = []
for child in Mirror(reflecting: type.meta).children {
@@ -161,9 +161,8 @@ public final class ObjectModel {
description.isOptional = attribute.isOptional
description.isIndexed = attribute.isIndexed
description.defaultValue = attribute.defaultValue
description.isTransient = false
description.isTransient = attribute.isTransient
// TODO: versionHash, renamingIdentifier, etc
// TODO: Separate attributes for Value, Transient, Relationship
propertyDescriptions.append(description)
case let relationship as RelationshipProtocol:
@@ -174,7 +173,6 @@ public final class ObjectModel {
description.isOrdered = relationship.isOrdered
description.deleteRule = relationship.deleteRule
// TODO: versionHash, renamingIdentifier, etc
// TODO: Separate attributes for Value, Transient, Relationship
propertyDescriptions.append(description)
default:
@@ -195,7 +193,7 @@ public final class ObjectModel {
relationshipsByNameByEntity[entity] = entityDescription.relationshipsByName
}
func findEntity(for type: ManagedObject.Type) -> AnyEntity {
func findEntity(for type: CoreStoreObject.Type) -> AnyEntity {
var matchedEntities: Set<AnyEntity> = []
for (entity, _) in entityDescriptionsByEntity where entity.type == type {
@@ -209,7 +207,7 @@ public final class ObjectModel {
if matchedEntities.isEmpty {
CoreStore.abort(
"No \(cs_typeName("Entity<\(type)>")) instance found in the \(cs_typeName(ObjectModel.self))."
"No \(cs_typeName("Entity<\(type)>")) instance found in the \(cs_typeName(DynamicModel.self))."
)
}
else {
@@ -244,7 +242,7 @@ public final class ObjectModel {
let description = relationshipsByName[relationship.keyPath]!
description.destinationEntity = entityDescriptionsByEntity[destinationEntity]!
if let destinationKeyPath = destinationKeyPath {
if let destinationKeyPath = destinationKeyPath() {
let inverseRelationshipDescription = findInverseRelationshipMatching(
destinationEntity: destinationEntity,
@@ -284,8 +282,8 @@ public final class ObjectModel {
func connectBaseEntity(mirror: Mirror, entityDescription: NSEntityDescription) {
guard let superclassMirror = mirror.superclassMirror,
let superType = superclassMirror.subjectType as? ManagedObject.Type,
superType != ManagedObject.self else {
let superType = superclassMirror.subjectType as? CoreStoreObject.Type,
superType != CoreStoreObject.self else {
return
}
@@ -325,7 +323,7 @@ fileprivate enum ModelCache {
return self.barrierQueue.cs_barrierSync(closure)
}
fileprivate static func entityDescription(for entity: ObjectModel.AnyEntity, initializer: (ObjectModel.AnyEntity) -> NSEntityDescription) -> NSEntityDescription {
fileprivate static func entityDescription(for entity: DynamicModel.AnyEntity, initializer: (DynamicModel.AnyEntity) -> NSEntityDescription) -> NSEntityDescription {
if let cachedEntityDescription = self.entityDescriptionsByEntity[entity] {
@@ -341,5 +339,5 @@ fileprivate enum ModelCache {
private static let barrierQueue = DispatchQueue.concurrent("com.coreStore.modelCacheBarrierQueue")
private static var entityDescriptionsByEntity: [ObjectModel.AnyEntity: NSEntityDescription] = [:]
private static var entityDescriptionsByEntity: [DynamicModel.AnyEntity: NSEntityDescription] = [:]
}

View File

@@ -1,5 +1,5 @@
//
// ManagedObjectProtocol.swift
// DynamicObject.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
@@ -26,25 +26,27 @@
import Foundation
// MARK: - ManagedObjectProtocol
// MARK: - DynamicObject
public protocol ManagedObjectProtocol: class {
public protocol DynamicObject: class {
static func cs_forceCreate(entityDescription: NSEntityDescription, into context: NSManagedObjectContext, assignTo store: NSPersistentStore) -> Self
static func cs_from(object: NSManagedObject) -> Self
static func cs_fromRaw(object: NSManagedObject) -> Self
func cs_toRaw() -> NSManagedObject
}
public extension ManagedObjectProtocol where Self: ManagedObject {
public extension DynamicObject where Self: CoreStoreObject {
@inline(__always)
public static func keyPath<O: ManagedObject, V: ImportableAttributeType>(_ attribute: (Self) -> AttributeContainer<O>.Required<V>) -> String {
public static func keyPath<O: CoreStoreObject, V: ImportableAttributeType>(_ attribute: (Self) -> ValueContainer<O>.Required<V>) -> String {
return attribute(self.meta).keyPath
}
@inline(__always)
public static func keyPath<O: ManagedObject, V: ImportableAttributeType>(_ attribute: (Self) -> AttributeContainer<O>.Optional<V>) -> String {
public static func keyPath<O: CoreStoreObject, V: ImportableAttributeType>(_ attribute: (Self) -> ValueContainer<O>.Optional<V>) -> String {
return attribute(self.meta).keyPath
}
@@ -67,19 +69,9 @@ public extension ManagedObjectProtocol where Self: ManagedObject {
// MARK: - NSManagedObject
extension NSManagedObject: ManagedObjectProtocol {
extension NSManagedObject: DynamicObject {
// MARK: ManagedObjectProtocol
public class func cs_from(object: NSManagedObject) -> Self {
@inline(__always)
func forceCast<T: NSManagedObject>(_ value: Any) -> T {
return value as! T
}
return forceCast(object)
}
// MARK: DynamicObject
public class func cs_forceCreate(entityDescription: NSEntityDescription, into context: NSManagedObjectContext, assignTo store: NSPersistentStore) -> Self {
@@ -90,19 +82,29 @@ extension NSManagedObject: ManagedObjectProtocol {
}
return object
}
public class func cs_fromRaw(object: NSManagedObject) -> Self {
@inline(__always)
func forceCast<T: NSManagedObject>(_ value: Any) -> T {
return value as! T
}
return forceCast(object)
}
public func cs_toRaw() -> NSManagedObject {
return self
}
}
// MARK: - ManagedObject
// MARK: - CoreStoreObject
extension ManagedObject {
extension CoreStoreObject {
// MARK: ManagedObjectProtocol
public class func cs_from(object: NSManagedObject) -> Self {
return self.init(object)
}
// MARK: DynamicObject
public class func cs_forceCreate(entityDescription: NSEntityDescription, into context: NSManagedObjectContext, assignTo store: NSPersistentStore) -> Self {
@@ -114,4 +116,14 @@ extension ManagedObject {
}
return self.init(object)
}
public class func cs_fromRaw(object: NSManagedObject) -> Self {
return self.init(object)
}
public func cs_toRaw() -> NSManagedObject {
return self.rawObject!
}
}

View File

@@ -32,14 +32,14 @@ import ObjectiveC
public protocol EntityProtocol {
var type: ManagedObject.Type { get }
var type: CoreStoreObject.Type { get }
var entityName: EntityName { get }
}
// MARK: Entity
public struct Entity<O: ManagedObject>: EntityProtocol {
public struct Entity<O: CoreStoreObject>: EntityProtocol {
public init(_ entityName: String) {
@@ -49,7 +49,7 @@ public struct Entity<O: ManagedObject>: EntityProtocol {
// MARK: EntityProtocol
public let type: ManagedObject.Type
public let type: CoreStoreObject.Type
public let entityName: EntityName
}
@@ -78,24 +78,24 @@ internal struct EntityIdentifier: Hashable {
self.interfacedClassName = String(reflecting: type)
}
internal init(_ type: ManagedObject.Type) {
internal init(_ type: CoreStoreObject.Type) {
self.category = .coreStore
self.interfacedClassName = String(reflecting: type)
}
internal init(_ type: ManagedObjectProtocol.Type) {
internal init(_ type: DynamicObject.Type) {
switch type {
case let type as NSManagedObject.Type:
self.init(type)
case let type as ManagedObject.Type:
case let type as CoreStoreObject.Type:
self.init(type)
default:
CoreStore.abort("\(cs_typeName(ManagedObjectProtocol.self)) is not meant to be implemented by external types.")
CoreStore.abort("\(cs_typeName(DynamicObject.self)) is not meant to be implemented by external types.")
}
}
@@ -138,7 +138,7 @@ internal struct EntityIdentifier: Hashable {
internal extension NSEntityDescription {
@nonobjc
internal var anyEntity: ObjectModel.AnyEntity? {
internal var anyEntity: DynamicModel.AnyEntity? {
get {
@@ -148,8 +148,8 @@ internal extension NSEntityDescription {
return nil
}
return ObjectModel.AnyEntity(
type: NSClassFromString(typeName) as! ManagedObject.Type,
return DynamicModel.AnyEntity(
type: NSClassFromString(typeName) as! CoreStoreObject.Type,
entityName: entityName
)
}

View File

@@ -27,9 +27,14 @@ import CoreData
import Foundation
// MARK: - ManagedObjectProtocol
// MARK: Operators
public extension ManagedObjectProtocol where Self: ManagedObject {
infix operator .= : AssignmentPrecedence
// MARK: - DynamicObject
public extension DynamicObject where Self: CoreStoreObject {
public typealias Relationship = RelationshipContainer<Self>
}
@@ -37,11 +42,11 @@ public extension ManagedObjectProtocol where Self: ManagedObject {
// MARK: - RelationshipContainer
public enum RelationshipContainer<O: ManagedObject> {
public enum RelationshipContainer<O: CoreStoreObject> {
// MARK: - ToOne
public final class ToOne<D: ManagedObject>: RelationshipProtocol {
public final class ToOne<D: CoreStoreObject>: RelationshipProtocol {
// MARK: -
@@ -49,65 +54,83 @@ public enum RelationshipContainer<O: ManagedObject> {
relationship.value = value
}
public static postfix func * (_ relationship: RelationshipContainer<O>.ToOne<D>) -> D? {
public static func .=<O2: CoreStoreObject> (_ relationship: RelationshipContainer<O>.ToOne<D>, _ relationship2: RelationshipContainer<O2>.ToOne<D>) {
return relationship.value
relationship.value = relationship2.value
}
public init(_ keyPath: String, deleteRule: DeleteRule = .nullify) {
public convenience init(_ keyPath: KeyPath, deleteRule: DeleteRule = .nullify) {
self.keyPath = keyPath
self.deleteRule = deleteRule.nativeValue
self.inverse = (D.self, nil)
self.init(keyPath: keyPath, inverseKeyPath: { nil }, deleteRule: deleteRule)
}
public init(_ keyPath: String, inverse: (D) -> RelationshipContainer<D>.ToOne<O>, deleteRule: DeleteRule = .nullify) {
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToOne<O>, deleteRule: DeleteRule = .nullify) {
self.keyPath = keyPath
self.deleteRule = deleteRule.nativeValue
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToManyOrdered<O>, deleteRule: DeleteRule = .nullify) {
let inverseRelationship = inverse(D.meta)
self.inverse = (D.self, inverseRelationship.keyPath)
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToManyUnordered<O>, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
public var value: D? {
get {
let object = self.accessRawObject()
let key = self.keyPath
return object.value(forKey: key)
.flatMap({ D.cs_from(object: $0 as! NSManagedObject) })
return self.accessRawObject()
.getValue(
forKvcKey: self.keyPath,
didGetValue: { $0.flatMap({ D.cs_fromRaw(object: $0 as! NSManagedObject) }) }
)
}
set {
let object = self.accessRawObject()
let key = self.keyPath
object.setValue(newValue?.rawObject, forKey: key)
self.accessRawObject()
.setValue(
newValue,
forKvcKey: self.keyPath,
willSetValue: { $0?.rawObject }
)
}
}
// MARK: RelationshipProtocol
public let keyPath: String
public let keyPath: KeyPath
internal let isToMany = false
internal let isOrdered = false
internal let deleteRule: NSDeleteRule
internal let inverse: (type: ManagedObject.Type, keyPath: String?)
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
internal var accessRawObject: () -> NSManagedObject = {
fatalError("\(O.self) relationship values should not be accessed")
CoreStore.abort("Attempted to access values from a \(cs_typeName(O.self)) meta object. Meta objects are only used for querying keyPaths and infering types.")
}
// MARK: Private
private init(keyPath: KeyPath, inverseKeyPath: @escaping () -> KeyPath?, deleteRule: DeleteRule) {
self.keyPath = keyPath
self.deleteRule = deleteRule.nativeValue
self.inverse = (D.self, inverseKeyPath)
}
}
// MARK: - ToManyOrdered
public final class ToManyOrdered<D: ManagedObject>: RelationshipProtocol {
public final class ToManyOrdered<D: CoreStoreObject>: RelationshipProtocol {
// MARK: -
@@ -116,52 +139,194 @@ public enum RelationshipContainer<O: ManagedObject> {
relationship.value = value
}
public static postfix func * (_ relationship: RelationshipContainer<O>.ToManyOrdered<D>) -> [D] {
public static func .=<C: Collection> (_ relationship: RelationshipContainer<O>.ToManyOrdered<D>, _ value: C) where C.Iterator.Element == D {
return relationship.value
relationship.value = Array(value)
}
public init(_ keyPath: String, deleteRule: DeleteRule = .nullify) {
public static func .=<O2: CoreStoreObject> (_ relationship: RelationshipContainer<O>.ToManyOrdered<D>, _ relationship2: RelationshipContainer<O2>.ToManyOrdered<D>) {
self.keyPath = keyPath
self.deleteRule = deleteRule.nativeValue
self.inverse = (D.self, nil)
relationship.value = relationship2.value
}
public convenience init(_ keyPath: KeyPath, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { nil }, deleteRule: deleteRule)
}
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToOne<O>, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToManyOrdered<O>, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToManyUnordered<O>, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
// TODO: add subscripts, indexed operations for more performant single updates
public var value: [D] {
get {
let object = self.accessRawObject()
let key = self.keyPath
guard let orderedSet = object.value(forKey: key) as! NSOrderedSet? else {
return []
}
return orderedSet.array as! [D]
return self.accessRawObject()
.getValue(
forKvcKey: self.keyPath,
didGetValue: {
guard let orderedSet = $0 as! NSOrderedSet? else {
return []
}
return orderedSet.map({ D.cs_fromRaw(object: $0 as! NSManagedObject) })
}
)
}
set {
let object = self.accessRawObject()
let key = self.keyPath
object.setValue(NSOrderedSet(array: newValue), forKey: key)
self.accessRawObject()
.setValue(
newValue,
forKvcKey: self.keyPath,
willSetValue: { NSOrderedSet(array: $0.map({ $0.rawObject! })) }
)
}
}
// MARK: RelationshipProtocol
public let keyPath: String
public let keyPath: KeyPath
internal let isToMany = true
internal let isOptional = true
internal let isOrdered = true
internal let deleteRule: NSDeleteRule
internal let inverse: (type: ManagedObject.Type, keyPath: String?)
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
internal var accessRawObject: () -> NSManagedObject = {
fatalError("\(O.self) relationship values should not be accessed")
CoreStore.abort("Attempted to access values from a \(cs_typeName(O.self)) meta object. Meta objects are only used for querying keyPaths and infering types.")
}
// MARK: Private
private init(keyPath: String, inverseKeyPath: @escaping () -> String?, deleteRule: DeleteRule) {
self.keyPath = keyPath
self.deleteRule = deleteRule.nativeValue
self.inverse = (D.self, inverseKeyPath)
}
}
// MARK: - ToManyUnordered
public final class ToManyUnordered<D: CoreStoreObject>: RelationshipProtocol {
// MARK: -
public static func .= (_ relationship: RelationshipContainer<O>.ToManyUnordered<D>, _ value: Set<D>) {
relationship.value = value
}
public static func .=<C: Collection> (_ relationship: RelationshipContainer<O>.ToManyUnordered<D>, _ value: C) where C.Iterator.Element == D {
relationship.value = Set(value)
}
public static func .=<O2: CoreStoreObject> (_ relationship: RelationshipContainer<O>.ToManyUnordered<D>, _ relationship2: RelationshipContainer<O2>.ToManyUnordered<D>) {
relationship.value = relationship2.value
}
public static func .=<O2: CoreStoreObject> (_ relationship: RelationshipContainer<O>.ToManyUnordered<D>, _ relationship2: RelationshipContainer<O2>.ToManyOrdered<D>) {
relationship.value = Set(relationship2.value)
}
public convenience init(_ keyPath: KeyPath, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { nil }, deleteRule: deleteRule)
}
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToOne<O>, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToManyOrdered<O>, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
public convenience init(_ keyPath: KeyPath, inverse: @escaping (D) -> RelationshipContainer<D>.ToManyUnordered<O>, deleteRule: DeleteRule = .nullify) {
self.init(keyPath: keyPath, inverseKeyPath: { inverse(D.meta).keyPath }, deleteRule: deleteRule)
}
// TODO: add subscripts, indexed operations for more performant single updates
public var value: Set<D> {
get {
return self.accessRawObject()
.getValue(
forKvcKey: self.keyPath,
didGetValue: {
guard let set = $0 as! NSSet? else {
return []
}
return Set(set.map({ D.cs_fromRaw(object: $0 as! NSManagedObject) }))
}
)
}
set {
self.accessRawObject()
.setValue(
newValue,
forKvcKey: self.keyPath,
willSetValue: { NSSet(array: $0.map({ $0.rawObject! })) }
)
}
}
// MARK: RelationshipProtocol
public let keyPath: KeyPath
internal let isToMany = true
internal let isOptional = true
internal let isOrdered = true
internal let deleteRule: NSDeleteRule
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
internal var accessRawObject: () -> NSManagedObject = {
CoreStore.abort("Attempted to access values from a \(cs_typeName(O.self)) meta object. Meta objects are only used for querying keyPaths and infering types.")
}
// MARK: Private
private init(keyPath: KeyPath, inverseKeyPath: @escaping () -> KeyPath?, deleteRule: DeleteRule) {
self.keyPath = keyPath
self.deleteRule = deleteRule.nativeValue
self.inverse = (D.self, inverseKeyPath)
}
}
@@ -191,10 +356,10 @@ public enum RelationshipContainer<O: ManagedObject> {
internal protocol RelationshipProtocol: class {
var keyPath: String { get }
var keyPath: KeyPath { get }
var isToMany: Bool { get }
var isOrdered: Bool { get }
var deleteRule: NSDeleteRule { get }
var inverse: (type: ManagedObject.Type, keyPath: String?) { get }
var inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?) { get }
var accessRawObject: () -> NSManagedObject { get set }
}

View File

@@ -1,5 +1,5 @@
//
// Attribute.swift
// Value.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
@@ -30,56 +30,60 @@ import Foundation
// MARK: Operators
infix operator .= : AssignmentPrecedence
postfix operator *
// MARK: - ManagedObjectProtocol
// MARK: - DynamicObject
public extension ManagedObjectProtocol where Self: ManagedObject {
public extension DynamicObject where Self: CoreStoreObject {
public typealias Attribute = AttributeContainer<Self>
public typealias Value = ValueContainer<Self>
}
// MARK: - AttributeContainer
// MARK: - ValueContainer
public enum AttributeContainer<O: ManagedObject> {
public enum ValueContainer<O: CoreStoreObject> {
// MARK: - Required
public final class Required<V: ImportableAttributeType>: AttributeProtocol {
public static func .= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) {
public static func .= (_ attribute: ValueContainer<O>.Required<V>, _ value: V) {
attribute.value = value
}
public static postfix func * (_ attribute: AttributeContainer<O>.Required<V>) -> V {
public static func .=<O2: CoreStoreObject> (_ attribute: ValueContainer<O>.Required<V>, _ attribute2: ValueContainer<O2>.Required<V>) {
return attribute.value
attribute.value = attribute2.value
}
public init(_ keyPath: String, `default`: V = V.cs_emptyValue(), isIndexed: Bool = false) {
public init(_ keyPath: KeyPath, `default`: V = V.cs_emptyValue(), isIndexed: Bool = false, isTransient: Bool = false) {
self.keyPath = keyPath
self.defaultValue = `default`.cs_toImportableNativeType()
self.isIndexed = isIndexed
self.isTransient = isTransient
self.defaultValue = `default`.cs_toImportableNativeType()
}
public var value: V {
get {
let object = self.accessRawObject()
let key = self.keyPath
let value = object.value(forKey: key)! as! V.ImportableNativeType
return V.cs_fromImportableNativeType(value)!
return self.accessRawObject()
.getValue(
forKvcKey: self.keyPath,
didGetValue: { V.cs_fromImportableNativeType($0 as! V.ImportableNativeType)! }
)
}
set {
let object = self.accessRawObject()
let key = self.keyPath
object.setValue(newValue.cs_toImportableNativeType(), forKey: key)
self.accessRawObject()
.setValue(
newValue,
forKvcKey: self.keyPath,
willSetValue: { $0.cs_toImportableNativeType() }
)
}
}
@@ -91,15 +95,16 @@ public enum AttributeContainer<O: ManagedObject> {
return V.cs_rawAttributeType
}
public let keyPath: String
public let keyPath: KeyPath
internal let isOptional = false
internal let isIndexed: Bool
internal let isTransient: Bool
internal let defaultValue: Any?
internal var accessRawObject: () -> NSManagedObject = {
fatalError("\(O.self) attribute values should not be accessed")
CoreStore.abort("Attempted to access values from a \(cs_typeName(O.self)) meta object. Meta objects are only used for querying keyPaths and infering types.")
}
}
@@ -108,19 +113,25 @@ public enum AttributeContainer<O: ManagedObject> {
public final class Optional<V: ImportableAttributeType>: AttributeProtocol {
public static func .= (_ attribute: AttributeContainer<O>.Optional<V>, _ value: V?) {
public static func .= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) {
attribute.value = value
}
public static postfix func * (_ attribute: AttributeContainer<O>.Optional<V>) -> V? {
public static func .=<O2: CoreStoreObject> (_ attribute: ValueContainer<O>.Optional<V>, _ attribute2: ValueContainer<O2>.Optional<V>) {
return attribute.value
attribute.value = attribute2.value
}
public init(_ keyPath: String, `default`: V? = nil) {
public static func .=<O2: CoreStoreObject> (_ attribute: ValueContainer<O>.Optional<V>, _ attribute2: ValueContainer<O2>.Required<V>) {
attribute.value = attribute2.value
}
public init(_ keyPath: KeyPath, `default`: V? = nil, isTransient: Bool = false) {
self.keyPath = keyPath
self.isTransient = isTransient
self.defaultValue = `default`?.cs_toImportableNativeType()
}
@@ -128,19 +139,20 @@ public enum AttributeContainer<O: ManagedObject> {
get {
let object = self.accessRawObject()
let key = self.keyPath
guard let value = object.value(forKey: key) as! V.ImportableNativeType? else {
return nil
}
return V.cs_fromImportableNativeType(value)
return self.accessRawObject()
.getValue(
forKvcKey: self.keyPath,
didGetValue: { ($0 as! V.ImportableNativeType?).flatMap(V.cs_fromImportableNativeType) }
)
}
set {
let object = self.accessRawObject()
let key = self.keyPath
object.setValue(newValue?.cs_toImportableNativeType(), forKey: key)
self.accessRawObject()
.setValue(
newValue,
forKvcKey: self.keyPath,
willSetValue: { $0?.cs_toImportableNativeType() }
)
}
}
@@ -152,14 +164,15 @@ public enum AttributeContainer<O: ManagedObject> {
return V.cs_rawAttributeType
}
public let keyPath: String
public let keyPath: KeyPath
internal let isOptional = true
internal let isIndexed = false
internal let isTransient: Bool
internal let defaultValue: Any?
internal var accessRawObject: () -> NSManagedObject = {
fatalError("\(O.self) attribute values should not be accessed")
CoreStore.abort("Attempted to access values from a \(cs_typeName(O.self)) meta object. Meta objects are only used for querying keyPaths and infering types.")
}
}
}
@@ -171,9 +184,10 @@ internal protocol AttributeProtocol: class {
static var attributeType: NSAttributeType { get }
var keyPath: String { get }
var keyPath: KeyPath { get }
var isOptional: Bool { get }
var isIndexed: Bool { get }
var isTransient: Bool { get }
var defaultValue: Any? { get }
var accessRawObject: () -> NSManagedObject { get set }
}