mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-20 16:44:08 +01:00
support transformable values
This commit is contained in:
@@ -406,6 +406,10 @@
|
|||||||
B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
|
B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
|
||||||
B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
|
B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
|
||||||
B58B22F51C93C1BA00521925 /* CoreStore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F03A53019C5C6DA005002A5 /* CoreStore.framework */; };
|
B58B22F51C93C1BA00521925 /* CoreStore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F03A53019C5C6DA005002A5 /* CoreStore.framework */; };
|
||||||
|
B58D0C631EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58D0C621EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift */; };
|
||||||
|
B58D0C641EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58D0C621EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift */; };
|
||||||
|
B58D0C651EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58D0C621EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift */; };
|
||||||
|
B58D0C661EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58D0C621EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift */; };
|
||||||
B596BBB21DD5A014001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
B596BBB21DD5A014001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
||||||
B596BBB31DD5A014001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
B596BBB31DD5A014001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
||||||
B596BBB41DD5A016001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
B596BBB41DD5A016001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
||||||
@@ -761,6 +765,7 @@
|
|||||||
B57D27BD1D0BBE8200539C58 /* BaseTestDataTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestDataTestCase.swift; sourceTree = "<group>"; };
|
B57D27BD1D0BBE8200539C58 /* BaseTestDataTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestDataTestCase.swift; sourceTree = "<group>"; };
|
||||||
B57D27C11D0BC20100539C58 /* QueryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryTests.swift; sourceTree = "<group>"; };
|
B57D27C11D0BC20100539C58 /* QueryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryTests.swift; sourceTree = "<group>"; };
|
||||||
B58085741CDF7F00004C2EEB /* SetupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetupTests.swift; sourceTree = "<group>"; };
|
B58085741CDF7F00004C2EEB /* SetupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetupTests.swift; sourceTree = "<group>"; };
|
||||||
|
B58D0C621EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSManagedObject+DynamicModel.swift"; sourceTree = "<group>"; };
|
||||||
B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConvenienceTests.swift; sourceTree = "<group>"; };
|
B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConvenienceTests.swift; sourceTree = "<group>"; };
|
||||||
B596BBB51DD5BC67001DCDD9 /* FetchableSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchableSource.swift; sourceTree = "<group>"; };
|
B596BBB51DD5BC67001DCDD9 /* FetchableSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchableSource.swift; sourceTree = "<group>"; };
|
||||||
B596BBBA1DD5C39F001DCDD9 /* QueryableSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryableSource.swift; sourceTree = "<group>"; };
|
B596BBBA1DD5C39F001DCDD9 /* QueryableSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryableSource.swift; sourceTree = "<group>"; };
|
||||||
@@ -1366,6 +1371,7 @@
|
|||||||
B5E84F2B1AFF849C0064E85B /* NotificationObserver.swift */,
|
B5E84F2B1AFF849C0064E85B /* NotificationObserver.swift */,
|
||||||
B533C4DA1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift */,
|
B533C4DA1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift */,
|
||||||
B52FD3A91E3B3EF10001D919 /* NSManagedObject+Logging.swift */,
|
B52FD3A91E3B3EF10001D919 /* NSManagedObject+Logging.swift */,
|
||||||
|
B58D0C621EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift */,
|
||||||
B5E84F2C1AFF849C0064E85B /* NSManagedObjectContext+CoreStore.swift */,
|
B5E84F2C1AFF849C0064E85B /* NSManagedObjectContext+CoreStore.swift */,
|
||||||
B5E84F351AFF85470064E85B /* NSManagedObjectContext+Querying.swift */,
|
B5E84F351AFF85470064E85B /* NSManagedObjectContext+Querying.swift */,
|
||||||
B5E84F321AFF85470064E85B /* NSManagedObjectContext+Setup.swift */,
|
B5E84F321AFF85470064E85B /* NSManagedObjectContext+Setup.swift */,
|
||||||
@@ -1802,6 +1808,7 @@
|
|||||||
B5E84F391AFF85470064E85B /* NSManagedObjectContext+Querying.swift in Sources */,
|
B5E84F391AFF85470064E85B /* NSManagedObjectContext+Querying.swift in Sources */,
|
||||||
B5D33A011E96012400C880DE /* Relationship.swift in Sources */,
|
B5D33A011E96012400C880DE /* Relationship.swift in Sources */,
|
||||||
B5E84EE81AFF84610064E85B /* CoreStoreLogger.swift in Sources */,
|
B5E84EE81AFF84610064E85B /* CoreStoreLogger.swift in Sources */,
|
||||||
|
B58D0C631EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift in Sources */,
|
||||||
B533C4DB1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift in Sources */,
|
B533C4DB1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift in Sources */,
|
||||||
B559CD491CAA8C6D00E4D58B /* CSStorageInterface.swift in Sources */,
|
B559CD491CAA8C6D00E4D58B /* CSStorageInterface.swift in Sources */,
|
||||||
B5ECDC2F1CA81CDC00C7F112 /* CSCoreStore+Transaction.swift in Sources */,
|
B5ECDC2F1CA81CDC00C7F112 /* CSCoreStore+Transaction.swift in Sources */,
|
||||||
@@ -1977,6 +1984,7 @@
|
|||||||
82BA18D81C4BBD7100A0916E /* WeakObject.swift in Sources */,
|
82BA18D81C4BBD7100A0916E /* WeakObject.swift in Sources */,
|
||||||
B5D33A021E96012400C880DE /* Relationship.swift in Sources */,
|
B5D33A021E96012400C880DE /* Relationship.swift in Sources */,
|
||||||
B559CD4B1CAA8C6D00E4D58B /* CSStorageInterface.swift in Sources */,
|
B559CD4B1CAA8C6D00E4D58B /* CSStorageInterface.swift in Sources */,
|
||||||
|
B58D0C641EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift in Sources */,
|
||||||
B533C4DC1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift in Sources */,
|
B533C4DC1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift in Sources */,
|
||||||
B5ECDC311CA81CDC00C7F112 /* CSCoreStore+Transaction.swift in Sources */,
|
B5ECDC311CA81CDC00C7F112 /* CSCoreStore+Transaction.swift in Sources */,
|
||||||
82BA18AF1C4BBD3100A0916E /* CoreStore+Transaction.swift in Sources */,
|
82BA18AF1C4BBD3100A0916E /* CoreStore+Transaction.swift in Sources */,
|
||||||
@@ -2152,6 +2160,7 @@
|
|||||||
B546F96C1C9AF26D00D5AC55 /* CSInMemoryStore.swift in Sources */,
|
B546F96C1C9AF26D00D5AC55 /* CSInMemoryStore.swift in Sources */,
|
||||||
B5D33A041E96012400C880DE /* Relationship.swift in Sources */,
|
B5D33A041E96012400C880DE /* Relationship.swift in Sources */,
|
||||||
B52DD1C61BE1F94600949AFE /* NSManagedObjectContext+CoreStore.swift in Sources */,
|
B52DD1C61BE1F94600949AFE /* NSManagedObjectContext+CoreStore.swift in Sources */,
|
||||||
|
B58D0C661EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift in Sources */,
|
||||||
B533C4DE1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift in Sources */,
|
B533C4DE1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift in Sources */,
|
||||||
B5220E141D130614009BC71E /* DataStack+Observing.swift in Sources */,
|
B5220E141D130614009BC71E /* DataStack+Observing.swift in Sources */,
|
||||||
B52DD1A21BE1F92C00949AFE /* CoreStore+Transaction.swift in Sources */,
|
B52DD1A21BE1F92C00949AFE /* CoreStore+Transaction.swift in Sources */,
|
||||||
@@ -2327,6 +2336,7 @@
|
|||||||
B56321B61BD6521C006C9394 /* WeakObject.swift in Sources */,
|
B56321B61BD6521C006C9394 /* WeakObject.swift in Sources */,
|
||||||
B5D33A031E96012400C880DE /* Relationship.swift in Sources */,
|
B5D33A031E96012400C880DE /* Relationship.swift in Sources */,
|
||||||
B559CD4C1CAA8C6D00E4D58B /* CSStorageInterface.swift in Sources */,
|
B559CD4C1CAA8C6D00E4D58B /* CSStorageInterface.swift in Sources */,
|
||||||
|
B58D0C651EAA0C7E003EDD87 /* NSManagedObject+DynamicModel.swift in Sources */,
|
||||||
B533C4DD1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift in Sources */,
|
B533C4DD1D7D4BFA001383CB /* DispatchQueue+CoreStore.swift in Sources */,
|
||||||
B5ECDC321CA81CDC00C7F112 /* CSCoreStore+Transaction.swift in Sources */,
|
B5ECDC321CA81CDC00C7F112 /* CSCoreStore+Transaction.swift in Sources */,
|
||||||
B56321AC1BD6521C006C9394 /* Functions.swift in Sources */,
|
B56321AC1BD6521C006C9394 /* Functions.swift in Sources */,
|
||||||
|
|||||||
@@ -45,7 +45,13 @@ class Dog: Animal {
|
|||||||
|
|
||||||
class Person: CoreStoreObject {
|
class Person: CoreStoreObject {
|
||||||
|
|
||||||
let name = Value.Required<String>("name")
|
let name = Value.Required<String>(
|
||||||
|
"name",
|
||||||
|
customGetter: { (`self`, getValue) in
|
||||||
|
|
||||||
|
return "Mr. \(getValue())"
|
||||||
|
}
|
||||||
|
)
|
||||||
let pet = Relationship.ToOne<Animal>("pet", inverse: { $0.master })
|
let pet = Relationship.ToOne<Animal>("pet", inverse: { $0.master })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,6 +114,9 @@ class DynamicModelTests: BaseTestDataTestCase {
|
|||||||
let person = transaction.create(Into<Person>())
|
let person = transaction.create(Into<Person>())
|
||||||
XCTAssertNil(person.pet.value)
|
XCTAssertNil(person.pet.value)
|
||||||
|
|
||||||
|
person.name .= "John"
|
||||||
|
XCTAssertEqual(person.name.value, "Mr. John") // Custom getter
|
||||||
|
|
||||||
person.pet .= dog
|
person.pet .= dog
|
||||||
XCTAssertEqual(person.pet.value, dog)
|
XCTAssertEqual(person.pet.value, dog)
|
||||||
XCTAssertEqual(person.pet.value?.master.value, person)
|
XCTAssertEqual(person.pet.value?.master.value, person)
|
||||||
|
|||||||
@@ -213,7 +213,6 @@ class SetupTests: BaseTestDataTestCase {
|
|||||||
self.prepareTestDataForStack(stack)
|
self.prepareTestDataForStack(stack)
|
||||||
}
|
}
|
||||||
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path))
|
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path))
|
||||||
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-wal")))
|
|
||||||
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-shm")))
|
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-shm")))
|
||||||
|
|
||||||
return try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
return try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
||||||
@@ -334,7 +333,6 @@ class SetupTests: BaseTestDataTestCase {
|
|||||||
self.prepareTestDataForStack(stack)
|
self.prepareTestDataForStack(stack)
|
||||||
}
|
}
|
||||||
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path))
|
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path))
|
||||||
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-wal")))
|
|
||||||
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-shm")))
|
XCTAssertTrue(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-shm")))
|
||||||
|
|
||||||
return try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
return try NSPersistentStoreCoordinator.metadataForPersistentStore(
|
||||||
|
|||||||
@@ -62,13 +62,27 @@ public extension DynamicSchema {
|
|||||||
for (attributeName, attribute) in attributesByName {
|
for (attributeName, attribute) in attributesByName {
|
||||||
|
|
||||||
let containerType: String
|
let containerType: String
|
||||||
if attribute.isOptional {
|
if attribute.attributeType == .transformableAttributeType {
|
||||||
|
|
||||||
containerType = "Value.Optional"
|
if attribute.isOptional {
|
||||||
|
|
||||||
|
containerType = "Transformable.Optional"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
containerType = "Transformable.Required"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
containerType = "Value.Required"
|
if attribute.isOptional {
|
||||||
|
|
||||||
|
containerType = "Value.Optional"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
containerType = "Value.Required"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let valueType: Any.Type
|
let valueType: Any.Type
|
||||||
var defaultString = ""
|
var defaultString = ""
|
||||||
@@ -151,11 +165,29 @@ public extension DynamicSchema {
|
|||||||
}
|
}
|
||||||
defaultString = ", default: Data(bytes: [\(bytes.joined(separator: ", "))])"
|
defaultString = ", default: Data(bytes: [\(bytes.joined(separator: ", "))])"
|
||||||
}
|
}
|
||||||
|
case .transformableAttributeType:
|
||||||
|
if let attributeValueClassName = attribute.attributeValueClassName {
|
||||||
|
|
||||||
|
valueType = NSClassFromString(attributeValueClassName)!
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
valueType = (NSCoding & NSCopying).self
|
||||||
|
}
|
||||||
|
if let defaultValue = attribute.defaultValue {
|
||||||
|
|
||||||
|
defaultString = ", default: /* \"\(defaultValue)\" */"
|
||||||
|
}
|
||||||
|
else if !attribute.isOptional {
|
||||||
|
|
||||||
|
defaultString = ", default: /* required */"
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
fatalError("Unsupported attribute type: \(attribute.attributeType)")
|
fatalError("Unsupported attribute type: \(attribute.attributeType.rawValue)")
|
||||||
}
|
}
|
||||||
|
let indexedString = attribute.isIndexed ? ", isIndexed: true" : ""
|
||||||
let transientString = attribute.isTransient ? ", isTransient: true" : ""
|
let transientString = attribute.isTransient ? ", isTransient: true" : ""
|
||||||
output.append(" let \(attributeName) = \(containerType)<\(String(describing: valueType))>(\"\(attributeName)\"\(defaultString)\(transientString))\n")
|
output.append(" let \(attributeName) = \(containerType)<\(String(describing: valueType))>(\"\(attributeName)\"\(indexedString)\(defaultString)\(transientString))\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,29 +78,29 @@ public extension NSManagedObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@nonobjc @inline(__always)
|
@nonobjc @inline(__always)
|
||||||
public func getValue(forKvcKey kvcKey: KeyPath) -> CoreDataNativeType? {
|
public func getValue(forKvcKey kvcKey: KeyPath) -> Any? {
|
||||||
|
|
||||||
self.willAccessValue(forKey: kvcKey)
|
self.willAccessValue(forKey: kvcKey)
|
||||||
defer {
|
defer {
|
||||||
|
|
||||||
self.didAccessValue(forKey: kvcKey)
|
self.didAccessValue(forKey: kvcKey)
|
||||||
}
|
}
|
||||||
return self.primitiveValue(forKey: kvcKey) as! CoreDataNativeType?
|
return self.primitiveValue(forKey: kvcKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
@nonobjc @inline(__always)
|
@nonobjc @inline(__always)
|
||||||
public func getValue<T>(forKvcKey kvcKey: KeyPath, didGetValue: (CoreDataNativeType?) throws -> T) rethrows -> T {
|
public func getValue<T>(forKvcKey kvcKey: KeyPath, didGetValue: (Any?) throws -> T) rethrows -> T {
|
||||||
|
|
||||||
self.willAccessValue(forKey: kvcKey)
|
self.willAccessValue(forKey: kvcKey)
|
||||||
defer {
|
defer {
|
||||||
|
|
||||||
self.didAccessValue(forKey: kvcKey)
|
self.didAccessValue(forKey: kvcKey)
|
||||||
}
|
}
|
||||||
return try didGetValue(self.primitiveValue(forKey: kvcKey) as! CoreDataNativeType?)
|
return try didGetValue(self.primitiveValue(forKey: kvcKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
@nonobjc @inline(__always)
|
@nonobjc @inline(__always)
|
||||||
public func getValue<T>(forKvcKey kvcKey: KeyPath, willGetValue: () throws -> Void, didGetValue: (CoreDataNativeType?) throws -> T) rethrows -> T {
|
public func getValue<T>(forKvcKey kvcKey: KeyPath, willGetValue: () throws -> Void, didGetValue: (Any?) throws -> T) rethrows -> T {
|
||||||
|
|
||||||
self.willAccessValue(forKey: kvcKey)
|
self.willAccessValue(forKey: kvcKey)
|
||||||
defer {
|
defer {
|
||||||
@@ -108,11 +108,11 @@ public extension NSManagedObject {
|
|||||||
self.didAccessValue(forKey: kvcKey)
|
self.didAccessValue(forKey: kvcKey)
|
||||||
}
|
}
|
||||||
try willGetValue()
|
try willGetValue()
|
||||||
return try didGetValue(self.primitiveValue(forKey: kvcKey) as! CoreDataNativeType?)
|
return try didGetValue(self.primitiveValue(forKey: kvcKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
@nonobjc @inline(__always)
|
@nonobjc @inline(__always)
|
||||||
public func setValue(_ value: CoreDataNativeType?, forKvcKey KVCKey: KeyPath) {
|
public func setValue(_ value: Any?, forKvcKey KVCKey: KeyPath) {
|
||||||
|
|
||||||
self.willChangeValue(forKey: KVCKey)
|
self.willChangeValue(forKey: KVCKey)
|
||||||
defer {
|
defer {
|
||||||
@@ -123,7 +123,7 @@ public extension NSManagedObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@nonobjc @inline(__always)
|
@nonobjc @inline(__always)
|
||||||
public func setValue<T>(_ value: T, forKvcKey KVCKey: KeyPath, willSetValue: (T) throws -> CoreDataNativeType?) rethrows {
|
public func setValue<T>(_ value: T, forKvcKey KVCKey: KeyPath, willSetValue: (T) throws -> Any?) rethrows {
|
||||||
|
|
||||||
self.willChangeValue(forKey: KVCKey)
|
self.willChangeValue(forKey: KVCKey)
|
||||||
defer {
|
defer {
|
||||||
|
|||||||
@@ -45,6 +45,16 @@ public extension DynamicObject where Self: CoreStoreObject {
|
|||||||
|
|
||||||
return condition(self.meta)
|
return condition(self.meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static func ascending<O: CoreStoreObject, V: ImportableAttributeType>(_ attribute: (Self) -> ValueContainer<O>.Optional<V>) -> OrderBy {
|
||||||
|
|
||||||
|
return OrderBy(.ascending(attribute(self.meta).keyPath))
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func descending<O: CoreStoreObject, V: ImportableAttributeType>(_ attribute: (Self) -> ValueContainer<O>.Optional<V>) -> OrderBy {
|
||||||
|
|
||||||
|
return OrderBy(.descending(attribute(self.meta).keyPath))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
61
Sources/Internal/NSManagedObject+DynamicModel.swift
Normal file
61
Sources/Internal/NSManagedObject+DynamicModel.swift
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
//
|
||||||
|
// NSManagedObject+DynamicModel.swift
|
||||||
|
// CoreStore
|
||||||
|
//
|
||||||
|
// Copyright © 2017 John Rommel Estropia
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import CoreData
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: - NSManagedObject
|
||||||
|
|
||||||
|
internal extension NSManagedObject {
|
||||||
|
|
||||||
|
@nonobjc
|
||||||
|
internal weak var coreStoreObject: CoreStoreObject? {
|
||||||
|
|
||||||
|
get {
|
||||||
|
|
||||||
|
return cs_getAssociatedObjectForKey(
|
||||||
|
&PropertyKeys.coreStoreObject,
|
||||||
|
inObject: self
|
||||||
|
)
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
|
||||||
|
cs_setAssociatedWeakObject(
|
||||||
|
newValue,
|
||||||
|
forKey: &PropertyKeys.coreStoreObject,
|
||||||
|
inObject: self
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: Private
|
||||||
|
|
||||||
|
private struct PropertyKeys {
|
||||||
|
|
||||||
|
static var coreStoreObject: Void?
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -81,7 +81,7 @@ public extension MigrationMappingProvider {
|
|||||||
|
|
||||||
public final class UnsafeMigrationProxyObject {
|
public final class UnsafeMigrationProxyObject {
|
||||||
|
|
||||||
public subscript(kvcKey: KeyPath) -> CoreDataNativeType? {
|
public subscript(kvcKey: KeyPath) -> Any? {
|
||||||
|
|
||||||
get {
|
get {
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public extension NSManagedObject {
|
|||||||
- returns: the primitive value for the KVC key
|
- returns: the primitive value for the KVC key
|
||||||
*/
|
*/
|
||||||
@objc
|
@objc
|
||||||
public func cs_accessValueForKVCKey(_ kvcKey: KeyPath) -> CoreDataNativeType? {
|
public func cs_accessValueForKVCKey(_ kvcKey: KeyPath) -> Any? {
|
||||||
|
|
||||||
return self.getValue(forKvcKey: kvcKey)
|
return self.getValue(forKvcKey: kvcKey)
|
||||||
}
|
}
|
||||||
@@ -50,7 +50,7 @@ public extension NSManagedObject {
|
|||||||
- parameter KVCKey: the KVC key
|
- parameter KVCKey: the KVC key
|
||||||
*/
|
*/
|
||||||
@objc
|
@objc
|
||||||
public func cs_setValue(_ value: CoreDataNativeType?, forKVCKey KVCKey: KeyPath) {
|
public func cs_setValue(_ value: Any?, forKVCKey KVCKey: KeyPath) {
|
||||||
|
|
||||||
self.setValue(value, forKvcKey: KVCKey)
|
self.setValue(value, forKvcKey: KVCKey)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,11 +54,11 @@ open /*abstract*/ class CoreStoreObject: DynamicObject, Hashable {
|
|||||||
Do not call this directly. This is exposed as public only as a required initializer.
|
Do not call this directly. This is exposed as public only as a required initializer.
|
||||||
- Important: subclasses that need a custom initializer should override both `init(_:)` and `init(asMeta:)`, and to call their corresponding super implementations.
|
- Important: subclasses that need a custom initializer should override both `init(_:)` and `init(asMeta:)`, and to call their corresponding super implementations.
|
||||||
*/
|
*/
|
||||||
public required init(_ object: NSManagedObject) {
|
public required init(rawObject: NSManagedObject) {
|
||||||
|
|
||||||
self.isMeta = false
|
self.isMeta = false
|
||||||
self.rawObject = object
|
self.rawObject = rawObject
|
||||||
self.initializeAttributes(Mirror(reflecting: self), { [unowned object] in object })
|
self.initializeAttributes(Mirror(reflecting: self), { [unowned self] in self })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,18 +105,18 @@ open /*abstract*/ class CoreStoreObject: DynamicObject, Hashable {
|
|||||||
|
|
||||||
// MARK: Private
|
// MARK: Private
|
||||||
|
|
||||||
private func initializeAttributes(_ mirror: Mirror, _ accessRawObject: @escaping () -> NSManagedObject) {
|
private func initializeAttributes(_ mirror: Mirror, _ parentObject: @escaping () -> CoreStoreObject) {
|
||||||
|
|
||||||
_ = mirror.superclassMirror.flatMap({ self.initializeAttributes($0, accessRawObject) })
|
_ = mirror.superclassMirror.flatMap({ self.initializeAttributes($0, parentObject) })
|
||||||
for child in mirror.children {
|
for child in mirror.children {
|
||||||
|
|
||||||
switch child.value {
|
switch child.value {
|
||||||
|
|
||||||
case let property as AttributeProtocol:
|
case let property as AttributeProtocol:
|
||||||
property.accessRawObject = accessRawObject
|
property.parentObject = parentObject
|
||||||
|
|
||||||
case let property as RelationshipProtocol:
|
case let property as RelationshipProtocol:
|
||||||
property.accessRawObject = accessRawObject
|
property.parentObject = parentObject
|
||||||
|
|
||||||
default:
|
default:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -85,12 +85,23 @@ extension CoreStoreObject {
|
|||||||
|
|
||||||
context.assign(object, to: store)
|
context.assign(object, to: store)
|
||||||
}
|
}
|
||||||
return self.init(object)
|
return self.cs_fromRaw(object: object)
|
||||||
}
|
}
|
||||||
|
|
||||||
public class func cs_fromRaw(object: NSManagedObject) -> Self {
|
public class func cs_fromRaw(object: NSManagedObject) -> Self {
|
||||||
|
|
||||||
return self.init(object)
|
if let coreStoreObject = object.coreStoreObject {
|
||||||
|
|
||||||
|
@inline(__always)
|
||||||
|
func forceCast<T: CoreStoreObject>(_ value: CoreStoreObject) -> T {
|
||||||
|
|
||||||
|
return value as! T
|
||||||
|
}
|
||||||
|
return forceCast(coreStoreObject)
|
||||||
|
}
|
||||||
|
let coreStoreObject = self.init(rawObject: object)
|
||||||
|
object.coreStoreObject = coreStoreObject
|
||||||
|
return coreStoreObject
|
||||||
}
|
}
|
||||||
|
|
||||||
public func cs_toRaw() -> NSManagedObject {
|
public func cs_toRaw() -> NSManagedObject {
|
||||||
|
|||||||
@@ -84,32 +84,32 @@ public enum RelationshipContainer<O: CoreStoreObject> {
|
|||||||
|
|
||||||
get {
|
get {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
return self.accessRawObject()
|
return object.rawObject!.getValue(
|
||||||
.getValue(
|
forKvcKey: self.keyPath,
|
||||||
forKvcKey: self.keyPath,
|
didGetValue: { $0.flatMap({ D.cs_fromRaw(object: $0 as! NSManagedObject) }) }
|
||||||
didGetValue: { $0.flatMap({ D.cs_fromRaw(object: $0 as! NSManagedObject) }) }
|
)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isEditableInContext() == true,
|
object.rawObject!.isEditableInContext() == true,
|
||||||
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
||||||
)
|
)
|
||||||
self.accessRawObject()
|
object.rawObject!.setValue(
|
||||||
.setValue(
|
newValue,
|
||||||
newValue,
|
forKvcKey: self.keyPath,
|
||||||
forKvcKey: self.keyPath,
|
willSetValue: { $0?.rawObject }
|
||||||
willSetValue: { $0?.rawObject }
|
)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ public enum RelationshipContainer<O: CoreStoreObject> {
|
|||||||
internal let maxCount: Int = 1
|
internal let maxCount: Int = 1
|
||||||
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
|
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
|
||||||
|
|
||||||
internal var accessRawObject: () -> NSManagedObject = {
|
internal var parentObject: () -> CoreStoreObject = {
|
||||||
|
|
||||||
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.")
|
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.")
|
||||||
}
|
}
|
||||||
@@ -189,39 +189,39 @@ public enum RelationshipContainer<O: CoreStoreObject> {
|
|||||||
|
|
||||||
get {
|
get {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
return self.accessRawObject()
|
return object.rawObject!.getValue(
|
||||||
.getValue(
|
forKvcKey: self.keyPath,
|
||||||
forKvcKey: self.keyPath,
|
didGetValue: {
|
||||||
didGetValue: {
|
|
||||||
|
|
||||||
guard let orderedSet = $0 as! NSOrderedSet? else {
|
guard let orderedSet = $0 as! NSOrderedSet? else {
|
||||||
|
|
||||||
return []
|
return []
|
||||||
}
|
|
||||||
return orderedSet.map({ D.cs_fromRaw(object: $0 as! NSManagedObject) })
|
|
||||||
}
|
}
|
||||||
)
|
return orderedSet.map({ D.cs_fromRaw(object: $0 as! NSManagedObject) })
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isEditableInContext() == true,
|
object.rawObject!.isEditableInContext() == true,
|
||||||
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
||||||
)
|
)
|
||||||
self.accessRawObject()
|
object.rawObject!.setValue(
|
||||||
.setValue(
|
newValue,
|
||||||
newValue,
|
forKvcKey: self.keyPath,
|
||||||
forKvcKey: self.keyPath,
|
willSetValue: { NSOrderedSet(array: $0.map({ $0.rawObject! })) }
|
||||||
willSetValue: { NSOrderedSet(array: $0.map({ $0.rawObject! })) }
|
)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,7 +238,7 @@ public enum RelationshipContainer<O: CoreStoreObject> {
|
|||||||
internal let maxCount: Int
|
internal let maxCount: Int
|
||||||
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
|
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
|
||||||
|
|
||||||
internal var accessRawObject: () -> NSManagedObject = {
|
internal var parentObject: () -> CoreStoreObject = {
|
||||||
|
|
||||||
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.")
|
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.")
|
||||||
}
|
}
|
||||||
@@ -311,39 +311,39 @@ public enum RelationshipContainer<O: CoreStoreObject> {
|
|||||||
|
|
||||||
get {
|
get {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
return self.accessRawObject()
|
return object.rawObject!.getValue(
|
||||||
.getValue(
|
forKvcKey: self.keyPath,
|
||||||
forKvcKey: self.keyPath,
|
didGetValue: {
|
||||||
didGetValue: {
|
|
||||||
|
|
||||||
guard let set = $0 as! NSSet? else {
|
guard let set = $0 as! NSSet? else {
|
||||||
|
|
||||||
return []
|
return []
|
||||||
}
|
|
||||||
return Set(set.map({ D.cs_fromRaw(object: $0 as! NSManagedObject) }))
|
|
||||||
}
|
}
|
||||||
)
|
return Set(set.map({ D.cs_fromRaw(object: $0 as! NSManagedObject) }))
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isEditableInContext() == true,
|
object.rawObject!.isEditableInContext() == true,
|
||||||
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
||||||
)
|
)
|
||||||
self.accessRawObject()
|
object.rawObject!.setValue(
|
||||||
.setValue(
|
newValue,
|
||||||
newValue,
|
forKvcKey: self.keyPath,
|
||||||
forKvcKey: self.keyPath,
|
willSetValue: { NSSet(array: $0.map({ $0.rawObject! })) }
|
||||||
willSetValue: { NSSet(array: $0.map({ $0.rawObject! })) }
|
)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,7 +360,7 @@ public enum RelationshipContainer<O: CoreStoreObject> {
|
|||||||
internal let maxCount: Int
|
internal let maxCount: Int
|
||||||
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
|
internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?)
|
||||||
|
|
||||||
internal var accessRawObject: () -> NSManagedObject = {
|
internal var parentObject: () -> CoreStoreObject = {
|
||||||
|
|
||||||
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.")
|
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.")
|
||||||
}
|
}
|
||||||
@@ -411,7 +411,7 @@ internal protocol RelationshipProtocol: class {
|
|||||||
var isOrdered: Bool { get }
|
var isOrdered: Bool { get }
|
||||||
var deleteRule: NSDeleteRule { get }
|
var deleteRule: NSDeleteRule { get }
|
||||||
var inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?) { get }
|
var inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPath?) { get }
|
||||||
var accessRawObject: () -> NSManagedObject { get set }
|
var parentObject: () -> CoreStoreObject { get set }
|
||||||
var minCount: Int { get }
|
var minCount: Int { get }
|
||||||
var maxCount: Int { get }
|
var maxCount: Int { get }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ infix operator .= : AssignmentPrecedence
|
|||||||
public extension DynamicObject where Self: CoreStoreObject {
|
public extension DynamicObject where Self: CoreStoreObject {
|
||||||
|
|
||||||
public typealias Value = ValueContainer<Self>
|
public typealias Value = ValueContainer<Self>
|
||||||
|
public typealias Transformable = TransformableContainer<Self>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -58,44 +59,59 @@ public enum ValueContainer<O: CoreStoreObject> {
|
|||||||
attribute.value = attribute2.value
|
attribute.value = attribute2.value
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(_ keyPath: KeyPath, `default`: V = V.cs_emptyValue(), isIndexed: Bool = false, isTransient: Bool = false) {
|
public init(_ keyPath: KeyPath, `default`: V = V.cs_emptyValue(), isIndexed: Bool = false, isTransient: Bool = false, customGetter: @escaping (_ `self`: O, _ getValue: () -> V) -> V = { $1() }, customSetter: @escaping (_ `self`: O, _ setValue: (V) -> Void, _ newValue: V) -> Void = { $1($2) }) {
|
||||||
|
|
||||||
self.keyPath = keyPath
|
self.keyPath = keyPath
|
||||||
self.isIndexed = isIndexed
|
self.isIndexed = isIndexed
|
||||||
self.isTransient = isTransient
|
self.isTransient = isTransient
|
||||||
self.defaultValue = `default`.cs_toImportableNativeType()
|
self.defaultValue = `default`.cs_toImportableNativeType()
|
||||||
|
self.customGetter = customGetter
|
||||||
|
self.customSetter = customSetter
|
||||||
}
|
}
|
||||||
|
|
||||||
public var value: V {
|
public var value: V {
|
||||||
|
|
||||||
get {
|
get {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
return self.accessRawObject()
|
return self.customGetter(
|
||||||
.getValue(
|
object,
|
||||||
forKvcKey: self.keyPath,
|
{ () -> V in
|
||||||
didGetValue: { V.cs_fromImportableNativeType($0 as! V.ImportableNativeType)! }
|
|
||||||
)
|
return object.rawObject!.getValue(
|
||||||
|
forKvcKey: self.keyPath,
|
||||||
|
didGetValue: { V.cs_fromImportableNativeType($0 as! V.ImportableNativeType)! }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isEditableInContext() == true,
|
object.rawObject!.isEditableInContext() == true,
|
||||||
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
||||||
)
|
)
|
||||||
self.accessRawObject()
|
self.customSetter(
|
||||||
.setValue(
|
object,
|
||||||
newValue,
|
{ (newValue: V) -> Void in
|
||||||
forKvcKey: self.keyPath,
|
|
||||||
willSetValue: { ($0.cs_toImportableNativeType() as! CoreDataNativeType) }
|
object.rawObject!.setValue(
|
||||||
)
|
newValue,
|
||||||
|
forKvcKey: self.keyPath,
|
||||||
|
willSetValue: { $0.cs_toImportableNativeType() }
|
||||||
|
)
|
||||||
|
},
|
||||||
|
newValue
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,10 +130,16 @@ public enum ValueContainer<O: CoreStoreObject> {
|
|||||||
internal let isTransient: Bool
|
internal let isTransient: Bool
|
||||||
internal let defaultValue: Any?
|
internal let defaultValue: Any?
|
||||||
|
|
||||||
internal var accessRawObject: () -> NSManagedObject = {
|
internal var parentObject: () -> CoreStoreObject = {
|
||||||
|
|
||||||
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.")
|
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 let customGetter: (_ `self`: O, _ getValue: () -> V) -> V
|
||||||
|
private let customSetter: (_ `self`: O, _ setValue: (V) -> Void, _ newValue: V) -> Void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -140,43 +162,58 @@ public enum ValueContainer<O: CoreStoreObject> {
|
|||||||
attribute.value = attribute2.value
|
attribute.value = attribute2.value
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(_ keyPath: KeyPath, `default`: V? = nil, isTransient: Bool = false) {
|
public init(_ keyPath: KeyPath, `default`: V? = nil, isTransient: Bool = false, customGetter: @escaping (_ `self`: O, _ getValue: () -> V?) -> V? = { $1() }, customSetter: @escaping (_ `self`: O, _ setValue: (V?) -> Void, _ newValue: V?) -> Void = { $1($2) }) {
|
||||||
|
|
||||||
self.keyPath = keyPath
|
self.keyPath = keyPath
|
||||||
self.isTransient = isTransient
|
self.isTransient = isTransient
|
||||||
self.defaultValue = `default`?.cs_toImportableNativeType()
|
self.defaultValue = `default`?.cs_toImportableNativeType()
|
||||||
|
self.customGetter = customGetter
|
||||||
|
self.customSetter = customSetter
|
||||||
}
|
}
|
||||||
|
|
||||||
public var value: V? {
|
public var value: V? {
|
||||||
|
|
||||||
get {
|
get {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
return self.accessRawObject()
|
return self.customGetter(
|
||||||
.getValue(
|
object,
|
||||||
forKvcKey: self.keyPath,
|
{ () -> V? in
|
||||||
didGetValue: { ($0 as! V.ImportableNativeType?).flatMap(V.cs_fromImportableNativeType) }
|
|
||||||
)
|
return object.rawObject!.getValue(
|
||||||
|
forKvcKey: self.keyPath,
|
||||||
|
didGetValue: { ($0 as! V.ImportableNativeType?).flatMap(V.cs_fromImportableNativeType) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isRunningInAllowedQueue() == true,
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
)
|
)
|
||||||
CoreStore.assert(
|
CoreStore.assert(
|
||||||
self.accessRawObject().isEditableInContext() == true,
|
object.rawObject!.isEditableInContext() == true,
|
||||||
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
||||||
)
|
)
|
||||||
self.accessRawObject()
|
self.customSetter(
|
||||||
.setValue(
|
object,
|
||||||
newValue,
|
{ (newValue: V?) -> Void in
|
||||||
forKvcKey: self.keyPath,
|
|
||||||
willSetValue: { ($0?.cs_toImportableNativeType() as! CoreDataNativeType?) }
|
object.rawObject!.setValue(
|
||||||
)
|
newValue,
|
||||||
|
forKvcKey: self.keyPath,
|
||||||
|
willSetValue: { $0?.cs_toImportableNativeType() }
|
||||||
|
)
|
||||||
|
},
|
||||||
|
newValue
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,10 +231,220 @@ public enum ValueContainer<O: CoreStoreObject> {
|
|||||||
internal let isTransient: Bool
|
internal let isTransient: Bool
|
||||||
internal let defaultValue: Any?
|
internal let defaultValue: Any?
|
||||||
|
|
||||||
internal var accessRawObject: () -> NSManagedObject = {
|
internal var parentObject: () -> CoreStoreObject = {
|
||||||
|
|
||||||
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.")
|
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 let customGetter: (_ `self`: O, _ getValue: () -> V?) -> V?
|
||||||
|
private let customSetter: (_ `self`: O, _ setValue: (V?) -> Void, _ newValue: V?) -> Void
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: - TransformableContainer
|
||||||
|
|
||||||
|
public enum TransformableContainer<O: CoreStoreObject> {
|
||||||
|
|
||||||
|
// MARK: - Required
|
||||||
|
|
||||||
|
public final class Required<V: NSCoding & NSCopying>: AttributeProtocol {
|
||||||
|
|
||||||
|
public static func .= (_ attribute: TransformableContainer<O>.Required<V>, _ value: V) {
|
||||||
|
|
||||||
|
attribute.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func .=<O2: CoreStoreObject> (_ attribute: TransformableContainer<O>.Required<V>, _ attribute2: TransformableContainer<O2>.Required<V>) {
|
||||||
|
|
||||||
|
attribute.value = attribute2.value
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(_ keyPath: KeyPath, `default`: V, isIndexed: Bool = false, isTransient: Bool = false, customGetter: @escaping (_ `self`: O, _ getValue: () -> V) -> V = { $1() }, customSetter: @escaping (_ `self`: O, _ setValue: (V) -> Void, _ newValue: V) -> Void = { $1($2) }) {
|
||||||
|
|
||||||
|
self.keyPath = keyPath
|
||||||
|
self.defaultValue = `default`
|
||||||
|
self.isIndexed = isIndexed
|
||||||
|
self.isTransient = isTransient
|
||||||
|
self.customGetter = customGetter
|
||||||
|
self.customSetter = customSetter
|
||||||
|
}
|
||||||
|
|
||||||
|
public var value: V {
|
||||||
|
|
||||||
|
get {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
|
CoreStore.assert(
|
||||||
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
|
)
|
||||||
|
return self.customGetter(
|
||||||
|
object,
|
||||||
|
{ () -> V in
|
||||||
|
|
||||||
|
return object.rawObject!.getValue(
|
||||||
|
forKvcKey: self.keyPath,
|
||||||
|
didGetValue: { $0 as! V }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
|
CoreStore.assert(
|
||||||
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
|
)
|
||||||
|
CoreStore.assert(
|
||||||
|
object.rawObject!.isEditableInContext() == true,
|
||||||
|
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
||||||
|
)
|
||||||
|
self.customSetter(
|
||||||
|
object,
|
||||||
|
{ (newValue: V) -> Void in
|
||||||
|
|
||||||
|
object.rawObject!.setValue(
|
||||||
|
newValue,
|
||||||
|
forKvcKey: self.keyPath
|
||||||
|
)
|
||||||
|
},
|
||||||
|
newValue
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: AttributeProtocol
|
||||||
|
|
||||||
|
internal static var attributeType: NSAttributeType {
|
||||||
|
|
||||||
|
return .transformableAttributeType
|
||||||
|
}
|
||||||
|
|
||||||
|
public let keyPath: KeyPath
|
||||||
|
|
||||||
|
internal let isOptional = false
|
||||||
|
internal let isIndexed: Bool
|
||||||
|
internal let isTransient: Bool
|
||||||
|
internal let defaultValue: Any?
|
||||||
|
|
||||||
|
internal var parentObject: () -> CoreStoreObject = {
|
||||||
|
|
||||||
|
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 let customGetter: (_ `self`: O, _ getValue: () -> V) -> V
|
||||||
|
private let customSetter: (_ `self`: O, _ setValue: (V) -> Void, _ newValue: V) -> Void
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: - Optional
|
||||||
|
|
||||||
|
public final class Optional<V: NSCoding & NSCopying>: AttributeProtocol {
|
||||||
|
|
||||||
|
public static func .= (_ attribute: TransformableContainer<O>.Optional<V>, _ value: V) {
|
||||||
|
|
||||||
|
attribute.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func .=<O2: CoreStoreObject> (_ attribute: TransformableContainer<O>.Optional<V>, _ attribute2: TransformableContainer<O2>.Optional<V>) {
|
||||||
|
|
||||||
|
attribute.value = attribute2.value
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func .=<O2: CoreStoreObject> (_ attribute: TransformableContainer<O>.Optional<V>, _ attribute2: TransformableContainer<O2>.Required<V>) {
|
||||||
|
|
||||||
|
attribute.value = attribute2.value
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(_ keyPath: KeyPath, `default`: V? = nil, isIndexed: Bool = false, isTransient: Bool = false, customGetter: @escaping (_ `self`: O, _ getValue: () -> V?) -> V? = { $1() }, customSetter: @escaping (_ `self`: O, _ setValue: (V?) -> Void, _ newValue: V?) -> Void = { $1($2) }) {
|
||||||
|
|
||||||
|
self.keyPath = keyPath
|
||||||
|
self.defaultValue = `default`
|
||||||
|
self.isIndexed = isIndexed
|
||||||
|
self.isTransient = isTransient
|
||||||
|
self.customGetter = customGetter
|
||||||
|
self.customSetter = customSetter
|
||||||
|
}
|
||||||
|
|
||||||
|
public var value: V? {
|
||||||
|
|
||||||
|
get {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
|
CoreStore.assert(
|
||||||
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
|
)
|
||||||
|
return self.customGetter(
|
||||||
|
object,
|
||||||
|
{ () -> V? in
|
||||||
|
|
||||||
|
return object.rawObject!.getValue(
|
||||||
|
forKvcKey: self.keyPath,
|
||||||
|
didGetValue: { $0 as! V? }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
|
||||||
|
let object = self.parentObject() as! O
|
||||||
|
CoreStore.assert(
|
||||||
|
object.rawObject!.isRunningInAllowedQueue() == true,
|
||||||
|
"Attempted to access \(cs_typeName(O.self))'s value outside it's designated queue."
|
||||||
|
)
|
||||||
|
CoreStore.assert(
|
||||||
|
object.rawObject!.isEditableInContext() == true,
|
||||||
|
"Attempted to update a \(cs_typeName(O.self))'s value from outside a transaction."
|
||||||
|
)
|
||||||
|
self.customSetter(
|
||||||
|
object,
|
||||||
|
{ (newValue: V?) -> Void in
|
||||||
|
|
||||||
|
object.rawObject!.setValue(
|
||||||
|
newValue,
|
||||||
|
forKvcKey: self.keyPath
|
||||||
|
)
|
||||||
|
},
|
||||||
|
newValue
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: AttributeProtocol
|
||||||
|
|
||||||
|
internal static var attributeType: NSAttributeType {
|
||||||
|
|
||||||
|
return .transformableAttributeType
|
||||||
|
}
|
||||||
|
|
||||||
|
public let keyPath: KeyPath
|
||||||
|
|
||||||
|
internal let isOptional = false
|
||||||
|
internal let isIndexed: Bool
|
||||||
|
internal let isTransient: Bool
|
||||||
|
internal let defaultValue: Any?
|
||||||
|
|
||||||
|
internal var parentObject: () -> CoreStoreObject = {
|
||||||
|
|
||||||
|
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 let customGetter: (_ `self`: O, _ getValue: () -> V?) -> V?
|
||||||
|
private let customSetter: (_ `self`: O, _ setValue: (V?) -> Void, _ newValue: V?) -> Void
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,5 +460,5 @@ internal protocol AttributeProtocol: class {
|
|||||||
var isIndexed: Bool { get }
|
var isIndexed: Bool { get }
|
||||||
var isTransient: Bool { get }
|
var isTransient: Bool { get }
|
||||||
var defaultValue: Any? { get }
|
var defaultValue: Any? { get }
|
||||||
var accessRawObject: () -> NSManagedObject { get set }
|
var parentObject: () -> CoreStoreObject { get set }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user