From aff966aac9d40e7d0e7d3847e36cb5789d8c75a4 Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Wed, 5 Jul 2017 08:45:10 +0900 Subject: [PATCH] WIP: Query builders --- CoreStore.xcodeproj/project.pbxproj | 10 ++ .../ObjectObserverDemoViewController.swift | 2 +- .../TransactionsDemoViewController.swift | 2 +- README.md | 2 +- Sources/CSGroupBy.swift | 6 +- Sources/CSSectionBy.swift | 4 +- Sources/CSSelect.swift | 14 +- Sources/CSWhere.swift | 4 +- Sources/CoreStoreBridge.h | 2 +- Sources/CoreStoreManagedObject.swift | 2 +- Sources/CoreStoreSchema.swift | 16 +- Sources/CustomSchemaMappingProvider.swift | 16 +- Sources/FetchCondition.swift | 143 ++++++++++++++++++ Sources/GroupBy.swift | 6 +- Sources/ListMonitor.swift | 2 +- .../NSEntityDescription+DynamicModel.swift | 8 +- Sources/NSManagedObject+Convenience.swift | 20 +-- Sources/NSManagedObject+ObjectiveC.swift | 4 +- Sources/ObjectObserver.swift | 4 +- Sources/OrderBy.swift | 12 +- Sources/Relationship.swift | 44 +++--- Sources/SectionBy.swift | 6 +- Sources/Select.swift | 26 ++-- Sources/Value.swift | 20 +-- Sources/Where.swift | 10 +- 25 files changed, 269 insertions(+), 116 deletions(-) create mode 100644 Sources/FetchCondition.swift diff --git a/CoreStore.xcodeproj/project.pbxproj b/CoreStore.xcodeproj/project.pbxproj index 1a0c170..c5a6d7e 100644 --- a/CoreStore.xcodeproj/project.pbxproj +++ b/CoreStore.xcodeproj/project.pbxproj @@ -324,6 +324,10 @@ B5519A601CA21954002BEF78 /* CSAsynchronousDataTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5519A5E1CA21954002BEF78 /* CSAsynchronousDataTransaction.swift */; }; B5519A611CA21954002BEF78 /* CSAsynchronousDataTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5519A5E1CA21954002BEF78 /* CSAsynchronousDataTransaction.swift */; }; B5519A621CA21954002BEF78 /* CSAsynchronousDataTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5519A5E1CA21954002BEF78 /* CSAsynchronousDataTransaction.swift */; }; + B55514EA1EED8BF900BAB888 /* FetchCondition.swift in Sources */ = {isa = PBXBuildFile; fileRef = B55514E91EED8BF900BAB888 /* FetchCondition.swift */; }; + B55514EB1EED8BF900BAB888 /* FetchCondition.swift in Sources */ = {isa = PBXBuildFile; fileRef = B55514E91EED8BF900BAB888 /* FetchCondition.swift */; }; + B55514EC1EED8BF900BAB888 /* FetchCondition.swift in Sources */ = {isa = PBXBuildFile; fileRef = B55514E91EED8BF900BAB888 /* FetchCondition.swift */; }; + B55514ED1EED8BF900BAB888 /* FetchCondition.swift in Sources */ = {isa = PBXBuildFile; fileRef = B55514E91EED8BF900BAB888 /* FetchCondition.swift */; }; B55717441D15B09E009BDBCA /* CoreStoreBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = B55717421D15AF9C009BDBCA /* CoreStoreBridge.h */; settings = {ATTRIBUTES = (Public, ); }; }; B55717451D15B09F009BDBCA /* CoreStoreBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = B55717421D15AF9C009BDBCA /* CoreStoreBridge.h */; settings = {ATTRIBUTES = (Public, ); }; }; B55717461D15B0A1009BDBCA /* CoreStoreBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = B55717421D15AF9C009BDBCA /* CoreStoreBridge.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -786,6 +790,7 @@ B5519A5E1CA21954002BEF78 /* CSAsynchronousDataTransaction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSAsynchronousDataTransaction.swift; sourceTree = ""; }; B5548CD51BD65AE00077652A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; }; B5548CD71BD65AE50077652A /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/CoreData.framework; sourceTree = DEVELOPER_DIR; }; + B55514E91EED8BF900BAB888 /* FetchCondition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchCondition.swift; sourceTree = ""; }; B55717421D15AF9C009BDBCA /* CoreStoreBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CoreStoreBridge.h; sourceTree = ""; }; B559CD421CAA8B6300E4D58B /* CSSetupResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSSetupResult.swift; sourceTree = ""; }; B559CD481CAA8C6D00E4D58B /* CSStorageInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSStorageInterface.swift; sourceTree = ""; }; @@ -1374,6 +1379,7 @@ B5E84F061AFF847B0064E85B /* DataStack+Querying.swift */, B5E84F071AFF847B0064E85B /* CoreStore+Querying.swift */, B596BBB51DD5BC67001DCDD9 /* FetchableSource.swift */, + B55514E91EED8BF900BAB888 /* FetchCondition.swift */, B596BBBA1DD5C39F001DCDD9 /* QueryableSource.swift */, B549F65D1E569C7400FBAB2D /* QueryableAttributeType.swift */, B5E84F0A1AFF847B0064E85B /* Protocol Clauses */, @@ -1794,6 +1800,7 @@ B5D339D81E9489AB00C880DE /* CoreStoreObject.swift in Sources */, B5D3F6451C887C0A00C7492A /* LegacySQLiteStore.swift in Sources */, B56923FA1EB82956007C4DC9 /* CSXcodeDataModelSchema.swift in Sources */, + B55514EA1EED8BF900BAB888 /* FetchCondition.swift in Sources */, B596BBBB1DD5C39F001DCDD9 /* QueryableSource.swift in Sources */, B5ECDBFF1CA80CBA00C7F112 /* CSWhere.swift in Sources */, B5ECDC051CA8138100C7F112 /* CSOrderBy.swift in Sources */, @@ -1980,6 +1987,7 @@ B5D339D91E9489AB00C880DE /* CoreStoreObject.swift in Sources */, 82BA18CE1C4BBD7100A0916E /* FetchedResultsControllerDelegate.swift in Sources */, B56923FB1EB82956007C4DC9 /* CSXcodeDataModelSchema.swift in Sources */, + B55514EB1EED8BF900BAB888 /* FetchCondition.swift in Sources */, B596BBBC1DD5C39F001DCDD9 /* QueryableSource.swift in Sources */, B5ECDC011CA80CBA00C7F112 /* CSWhere.swift in Sources */, B5ECDC071CA8138100C7F112 /* CSOrderBy.swift in Sources */, @@ -2166,6 +2174,7 @@ B5D339DB1E9489AB00C880DE /* CoreStoreObject.swift in Sources */, B52DD1951BE1F92500949AFE /* CoreStoreError.swift in Sources */, B56923FD1EB82956007C4DC9 /* CSXcodeDataModelSchema.swift in Sources */, + B55514ED1EED8BF900BAB888 /* FetchCondition.swift in Sources */, B596BBBE1DD5C39F001DCDD9 /* QueryableSource.swift in Sources */, B546F9601C9A12B800D5AC55 /* CSSQliteStore.swift in Sources */, B5ECDC0F1CA8161B00C7F112 /* CSGroupBy.swift in Sources */, @@ -2352,6 +2361,7 @@ B5D339DA1E9489AB00C880DE /* CoreStoreObject.swift in Sources */, B5ECDC021CA80CBA00C7F112 /* CSWhere.swift in Sources */, B56923FC1EB82956007C4DC9 /* CSXcodeDataModelSchema.swift in Sources */, + B55514EC1EED8BF900BAB888 /* FetchCondition.swift in Sources */, B596BBBD1DD5C39F001DCDD9 /* QueryableSource.swift in Sources */, B5ECDC081CA8138100C7F112 /* CSOrderBy.swift in Sources */, B5E1B59B1CAA0C23007FD580 /* CSObjectObserver.swift in Sources */, diff --git a/CoreStoreDemo/CoreStoreDemo/List and Object Observers Demo/ObjectObserverDemoViewController.swift b/CoreStoreDemo/CoreStoreDemo/List and Object Observers Demo/ObjectObserverDemoViewController.swift index dc84a04..502bcbc 100644 --- a/CoreStoreDemo/CoreStoreDemo/List and Object Observers Demo/ObjectObserverDemoViewController.swift +++ b/CoreStoreDemo/CoreStoreDemo/List and Object Observers Demo/ObjectObserverDemoViewController.swift @@ -85,7 +85,7 @@ class ObjectObserverDemoViewController: UIViewController, ObjectObserver { // MARK: ObjectObserver - func objectMonitor(_ monitor: ObjectMonitor, didUpdateObject object: Palette, changedPersistentKeys: Set) { + func objectMonitor(_ monitor: ObjectMonitor, didUpdateObject object: Palette, changedPersistentKeys: Set) { self.reloadPaletteInfo(object, changedKeys: changedPersistentKeys) } diff --git a/CoreStoreDemo/CoreStoreDemo/Transactions Demo/TransactionsDemoViewController.swift b/CoreStoreDemo/CoreStoreDemo/Transactions Demo/TransactionsDemoViewController.swift index 5662123..2d9a259 100644 --- a/CoreStoreDemo/CoreStoreDemo/Transactions Demo/TransactionsDemoViewController.swift +++ b/CoreStoreDemo/CoreStoreDemo/Transactions Demo/TransactionsDemoViewController.swift @@ -131,7 +131,7 @@ class TransactionsDemoViewController: UIViewController, MKMapViewDelegate, Objec // none } - func objectMonitor(_ monitor: ObjectMonitor, didUpdateObject object: Place, changedPersistentKeys: Set) { + func objectMonitor(_ monitor: ObjectMonitor, didUpdateObject object: Place, changedPersistentKeys: Set) { if let mapView = self.mapView { diff --git a/README.md b/README.md index 6a5f188..d03b127 100644 --- a/README.md +++ b/README.md @@ -1447,7 +1447,7 @@ class MyViewController: UIViewController, ObjectObserver { // ... } - func objectMonitor(monitor: ObjectMonitor, didUpdateObject object: MyPersonEntity, changedPersistentKeys: Set) { + func objectMonitor(monitor: ObjectMonitor, didUpdateObject object: MyPersonEntity, changedPersistentKeys: Set) { // ... } diff --git a/Sources/CSGroupBy.swift b/Sources/CSGroupBy.swift index 189f0a4..9f79679 100644 --- a/Sources/CSGroupBy.swift +++ b/Sources/CSGroupBy.swift @@ -41,7 +41,7 @@ public final class CSGroupBy: NSObject, CSQueryClause, CoreStoreObjectiveCType { The list of key path strings to group results with */ @objc - public var keyPaths: [RawKeyPath] { + public var keyPaths: [KeyPathString] { return self.bridgeToSwift.keyPaths } @@ -52,7 +52,7 @@ public final class CSGroupBy: NSObject, CSQueryClause, CoreStoreObjectiveCType { - parameter keyPath: a key path string to group results with */ @objc - public convenience init(keyPath: RawKeyPath) { + public convenience init(keyPath: KeyPathString) { self.init(GroupBy(keyPath)) } @@ -63,7 +63,7 @@ public final class CSGroupBy: NSObject, CSQueryClause, CoreStoreObjectiveCType { - parameter keyPaths: a list of key path strings to group results with */ @objc - public convenience init(keyPaths: [RawKeyPath]) { + public convenience init(keyPaths: [KeyPathString]) { self.init(GroupBy(keyPaths)) } diff --git a/Sources/CSSectionBy.swift b/Sources/CSSectionBy.swift index ba0a93c..bccdffc 100644 --- a/Sources/CSSectionBy.swift +++ b/Sources/CSSectionBy.swift @@ -45,7 +45,7 @@ public final class CSSectionBy: NSObject, CoreStoreObjectiveCType { - returns: a `CSSectionBy` clause with the key path to use to group `CSListMonitor` objects into sections */ @objc - public static func keyPath(_ sectionKeyPath: RawKeyPath) -> CSSectionBy { + public static func keyPath(_ sectionKeyPath: KeyPathString) -> CSSectionBy { return self.init(SectionBy(sectionKeyPath)) } @@ -58,7 +58,7 @@ public final class CSSectionBy: NSObject, CoreStoreObjectiveCType { - returns: a `CSSectionBy` clause with the key path to use to group `CSListMonitor` objects into sections */ @objc - public static func keyPath(_ sectionKeyPath: RawKeyPath, sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> CSSectionBy { + public static func keyPath(_ sectionKeyPath: KeyPathString, sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> CSSectionBy { return self.init(SectionBy(sectionKeyPath, sectionIndexTransformer)) } diff --git a/Sources/CSSelect.swift b/Sources/CSSelect.swift index e47d107..c06b4f6 100644 --- a/Sources/CSSelect.swift +++ b/Sources/CSSelect.swift @@ -48,7 +48,7 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType { - parameter keyPath: the attribute name */ @objc - public convenience init(keyPath: RawKeyPath) { + public convenience init(keyPath: KeyPathString) { self.init(.attribute(keyPath)) } @@ -65,7 +65,7 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType { - returns: a `CSSelectTerm` to a `CSSelect` clause for querying the average value of an attribute */ @objc - public static func average(_ keyPath: RawKeyPath, as alias: RawKeyPath?) -> CSSelectTerm { + public static func average(_ keyPath: KeyPathString, as alias: KeyPathString?) -> CSSelectTerm { return self.init(.average(keyPath, as: alias)) } @@ -82,7 +82,7 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType { - returns: a `SelectTerm` to a `Select` clause for a count query */ @objc - public static func count(_ keyPath: RawKeyPath, as alias: RawKeyPath?) -> CSSelectTerm { + public static func count(_ keyPath: KeyPathString, as alias: KeyPathString?) -> CSSelectTerm { return self.init(.count(keyPath, as: alias)) } @@ -99,7 +99,7 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType { - returns: a `CSSelectTerm` to a `CSSelect` clause for querying the maximum value for an attribute */ @objc - public static func maximum(_ keyPath: RawKeyPath, as alias: RawKeyPath?) -> CSSelectTerm { + public static func maximum(_ keyPath: KeyPathString, as alias: KeyPathString?) -> CSSelectTerm { return self.init(.maximum(keyPath, as: alias)) } @@ -116,7 +116,7 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType { - returns: a `CSSelectTerm` to a `CSSelect` clause for querying the minimum value for an attribute */ @objc - public static func minimum(_ keyPath: RawKeyPath, as alias: RawKeyPath?) -> CSSelectTerm { + public static func minimum(_ keyPath: KeyPathString, as alias: KeyPathString?) -> CSSelectTerm { return self.init(.minimum(keyPath, as: alias)) } @@ -133,7 +133,7 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType { - returns: a `CSSelectTerm` to a `CSSelect` clause for querying the sum value for an attribute */ @objc - public static func sum(_ keyPath: RawKeyPath, as alias: RawKeyPath?) -> CSSelectTerm { + public static func sum(_ keyPath: KeyPathString, as alias: KeyPathString?) -> CSSelectTerm { return self.init(.sum(keyPath, as: alias)) } @@ -150,7 +150,7 @@ public final class CSSelectTerm: NSObject, CoreStoreObjectiveCType { - returns: a `SelectTerm` to a `Select` clause for querying the sum value for an attribute */ @objc - public static func objectIDAs(_ alias: RawKeyPath? = nil) -> CSSelectTerm { + public static func objectIDAs(_ alias: KeyPathString? = nil) -> CSSelectTerm { return self.init(.objectID(as: alias)) } diff --git a/Sources/CSWhere.swift b/Sources/CSWhere.swift index 5949990..f36ad16 100644 --- a/Sources/CSWhere.swift +++ b/Sources/CSWhere.swift @@ -85,7 +85,7 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau - parameter value: the arguments for the `==` operator */ @objc - public convenience init(keyPath: RawKeyPath, isEqualTo value: CoreDataNativeType?) { + public convenience init(keyPath: KeyPathString, isEqualTo value: CoreDataNativeType?) { self.init(value == nil || value is NSNull ? Where("\(keyPath) == nil") @@ -99,7 +99,7 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau - parameter list: the array to check membership of */ @objc - public convenience init(keyPath: RawKeyPath, isMemberOf list: [CoreDataNativeType]) { + public convenience init(keyPath: KeyPathString, isMemberOf list: [CoreDataNativeType]) { self.init(Where("\(keyPath) IN %@", list as NSArray)) } diff --git a/Sources/CoreStoreBridge.h b/Sources/CoreStoreBridge.h index 407258d..43dfc6d 100644 --- a/Sources/CoreStoreBridge.h +++ b/Sources/CoreStoreBridge.h @@ -43,7 +43,7 @@ #define CORESTORE_RETURNS_RETAINED __attribute__((ns_returns_retained)) -#pragma mark - RawKeyPath Utilities +#pragma mark - KeyPathString Utilities #define CSKeyPath(type, property) ({ \ type *_je_keypath_dummy __attribute__((unused)); \ diff --git a/Sources/CoreStoreManagedObject.swift b/Sources/CoreStoreManagedObject.swift index 5a851fd..4790660 100644 --- a/Sources/CoreStoreManagedObject.swift +++ b/Sources/CoreStoreManagedObject.swift @@ -30,5 +30,5 @@ import CoreData private enum Static { static let queue = DispatchQueue.concurrent("com.coreStore.coreStoreManagerObjectBarrierQueue") - static var cache: [ObjectIdentifier: [RawKeyPath: Set]] = [:] + static var cache: [ObjectIdentifier: [KeyPathString: Set]] = [:] } diff --git a/Sources/CoreStoreSchema.swift b/Sources/CoreStoreSchema.swift index 316923b..22f284d 100644 --- a/Sources/CoreStoreSchema.swift +++ b/Sources/CoreStoreSchema.swift @@ -208,7 +208,7 @@ public final class CoreStoreSchema: DynamicSchema { } let rawModel = NSManagedObjectModel() var entityDescriptionsByEntity: [DynamicEntity: NSEntityDescription] = [:] - var allCustomGettersSetters: [DynamicEntity: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter]] = [:] + var allCustomGettersSetters: [DynamicEntity: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter]] = [:] for entity in self.allEntities { let (entityDescription, customGetterSetterByKeyPaths) = self.entityDescription( @@ -253,10 +253,10 @@ public final class CoreStoreSchema: DynamicSchema { private let allEntities: Set private var entityDescriptionsByEntity: [DynamicEntity: NSEntityDescription] = [:] - private var customGettersSettersByEntity: [DynamicEntity: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter]] = [:] + private var customGettersSettersByEntity: [DynamicEntity: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter]] = [:] private weak var cachedRawModel: NSManagedObjectModel? - private func entityDescription(for entity: DynamicEntity, initializer: (DynamicEntity, ModelVersion) -> (entity: NSEntityDescription, customGetterSetterByKeyPaths: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter])) -> (entity: NSEntityDescription, customGetterSetterByKeyPaths: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter]) { + private func entityDescription(for entity: DynamicEntity, initializer: (DynamicEntity, ModelVersion) -> (entity: NSEntityDescription, customGetterSetterByKeyPaths: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter])) -> (entity: NSEntityDescription, customGetterSetterByKeyPaths: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter]) { if let cachedEntityDescription = self.entityDescriptionsByEntity[entity] { @@ -269,7 +269,7 @@ public final class CoreStoreSchema: DynamicSchema { return (entityDescription, customGetterSetterByKeyPaths) } - private static func firstPassCreateEntityDescription(from entity: DynamicEntity, in modelVersion: ModelVersion) -> (entity: NSEntityDescription, customGetterSetterByKeyPaths: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter]) { + private static func firstPassCreateEntityDescription(from entity: DynamicEntity, in modelVersion: ModelVersion) -> (entity: NSEntityDescription, customGetterSetterByKeyPaths: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter]) { let entityDescription = NSEntityDescription() entityDescription.coreStoreEntity = entity @@ -278,8 +278,8 @@ public final class CoreStoreSchema: DynamicSchema { entityDescription.versionHashModifier = entity.versionHashModifier entityDescription.managedObjectClassName = CoreStoreManagedObject.cs_subclassName(for: entity, in: modelVersion) - var keyPathsByAffectedKeyPaths: [RawKeyPath: Set] = [:] - var customGetterSetterByKeyPaths: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter] = [:] + var keyPathsByAffectedKeyPaths: [KeyPathString: Set] = [:] + var customGetterSetterByKeyPaths: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter] = [:] func createProperties(for type: CoreStoreObject.Type) -> [NSPropertyDescription] { var propertyDescriptions: [NSPropertyDescription] = [] @@ -443,9 +443,9 @@ public final class CoreStoreSchema: DynamicSchema { } } - private static func fourthPassSynthesizeManagedObjectClasses(for entityDescriptionsByEntity: [DynamicEntity: NSEntityDescription], allCustomGettersSetters: [DynamicEntity: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter]]) { + private static func fourthPassSynthesizeManagedObjectClasses(for entityDescriptionsByEntity: [DynamicEntity: NSEntityDescription], allCustomGettersSetters: [DynamicEntity: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter]]) { - func createManagedObjectSubclass(for entityDescription: NSEntityDescription, customGetterSetterByKeyPaths: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter]?) { + func createManagedObjectSubclass(for entityDescription: NSEntityDescription, customGetterSetterByKeyPaths: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter]?) { let superEntity = entityDescription.superentity let className = entityDescription.managedObjectClassName! diff --git a/Sources/CustomSchemaMappingProvider.swift b/Sources/CustomSchemaMappingProvider.swift index a166685..e028433 100644 --- a/Sources/CustomSchemaMappingProvider.swift +++ b/Sources/CustomSchemaMappingProvider.swift @@ -210,7 +210,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { /** Accesses the property value via its keyPath. */ - public subscript(attribute: RawKeyPath) -> Any? { + public subscript(attribute: KeyPathString) -> Any? { return self.rawObject.cs_accessValueForKVCKey(attribute) } @@ -267,7 +267,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { /** Accesses or mutates the property value via its keyPath. */ - public subscript(attribute: RawKeyPath) -> Any? { + public subscript(attribute: KeyPathString) -> Any? { get { return self.rawObject.cs_accessValueForKVCKey(attribute) } set { self.rawObject.cs_setValue(newValue, forKVCKey: attribute) } @@ -304,7 +304,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { // MARK: Internal - internal init(_ rawObject: NSManagedObject, _ sourceAttributesByDestinationKey: [RawKeyPath: NSAttributeDescription]) { + internal init(_ rawObject: NSManagedObject, _ sourceAttributesByDestinationKey: [KeyPathString: NSAttributeDescription]) { self.rawObject = rawObject self.sourceAttributesByDestinationKey = sourceAttributesByDestinationKey @@ -314,7 +314,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { // MARK: FilePrivate fileprivate let rawObject: NSManagedObject - fileprivate let sourceAttributesByDestinationKey: [RawKeyPath: NSAttributeDescription] + fileprivate let sourceAttributesByDestinationKey: [KeyPathString: NSAttributeDescription] } @@ -477,7 +477,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { let transformedRenamingIdentifiers = Set(destinationAttributes.keys) .intersection(sourceAttributes.keys) - var sourceAttributesByDestinationKey: [RawKeyPath: NSAttributeDescription] = [:] + var sourceAttributesByDestinationKey: [KeyPathString: NSAttributeDescription] = [:] for renamingIdentifier in transformedRenamingIdentifiers { let sourceAttribute = sourceAttributes[renamingIdentifier]!.attribute @@ -535,7 +535,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { let userInfo = mapping.userInfo! let transformer = userInfo[CustomEntityMigrationPolicy.UserInfoKey.transformer]! as! CustomMapping.Transformer - let sourceAttributesByDestinationKey = userInfo[CustomEntityMigrationPolicy.UserInfoKey.sourceAttributesByDestinationKey] as! [RawKeyPath: NSAttributeDescription] + let sourceAttributesByDestinationKey = userInfo[CustomEntityMigrationPolicy.UserInfoKey.sourceAttributesByDestinationKey] as! [KeyPathString: NSAttributeDescription] var destinationObject: UnsafeDestinationObject? try transformer( @@ -585,8 +585,8 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { var insertMappings: Set = [] var copyMappings: Set = [] var transformMappings: Set = [] - var allMappedSourceKeys: [RawKeyPath: RawKeyPath] = [:] - var allMappedDestinationKeys: [RawKeyPath: RawKeyPath] = [:] + var allMappedSourceKeys: [KeyPathString: KeyPathString] = [:] + var allMappedDestinationKeys: [KeyPathString: KeyPathString] = [:] let sourceRenamingIdentifiers = sourceModel.cs_resolvedRenamingIdentities() let sourceEntityNames = sourceModel.entitiesByName diff --git a/Sources/FetchCondition.swift b/Sources/FetchCondition.swift new file mode 100644 index 0000000..ac05514 --- /dev/null +++ b/Sources/FetchCondition.swift @@ -0,0 +1,143 @@ +// +// FetchCondition.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 + +public struct ChainedClauseBuilder { + + public let from: From + public let fetchClauses: [FetchClause] = [] + + internal init(from: From) { + + self.from = from + } +} + +extension From: ClauseChain { + + public typealias ObjectType = T + public typealias TraitType = FetchTrait + + public var builder: ChainedClauseBuilder { + + return .init(from: self) + } +} + +public struct ChainedWhere: ClauseChain { + + public typealias ObjectType = D + public typealias TraitType = T + + public let builder: ChainedClauseBuilder + + fileprivate init(builder: ChainedClauseBuilder) { + + var newBuilder = builder +// newBuilder.fetchClauses.append(Where()) + self.builder = newBuilder + } +} + +public struct ChainedOrderBy: ClauseChain { + + public typealias ObjectType = D + public typealias TraitType = T + + public let builder: ChainedClauseBuilder + + fileprivate init(builder: ChainedClauseBuilder) { + + var newBuilder = builder +// newBuilder.fetchClauses.append(Where()) + self.builder = newBuilder + } +} + +public struct ChainedSelect: ClauseChain { + + public typealias ObjectType = D + public typealias TraitType = T + + public let builder: ChainedClauseBuilder + + fileprivate init(builder: ChainedClauseBuilder) { + + var newBuilder = builder +// newBuilder.fetchClauses.append(Where()) + self.builder = newBuilder + } +} + + + + + +public protocol ClauseTrait {} +public enum FetchTrait: ClauseTrait {} +public enum QueryTrait: ClauseTrait {} +public enum SectionTrait: ClauseTrait {} + + +public protocol ClauseChain { + + associatedtype ObjectType: DynamicObject + associatedtype TraitType: ClauseTrait + + var builder: ChainedClauseBuilder { get } +} + +public extension ClauseChain where Self.TraitType == FetchTrait { + + public func `where`() -> ChainedWhere { + + return .init(builder: self.builder) + } + + public func orderBy() -> ChainedOrderBy { + + return .init(builder: self.builder) + } + + public func select() -> ChainedSelect { + + return .init(builder: self.builder) + } +} + +public extension ClauseChain where Self.TraitType == QueryTrait { + + public func `where`() -> ChainedWhere { + + return .init(builder: self.builder) + } + + public func orderBy() -> ChainedOrderBy { + + return .init(builder: self.builder) + } +} diff --git a/Sources/GroupBy.swift b/Sources/GroupBy.swift index 11e7fd2..0c6153f 100644 --- a/Sources/GroupBy.swift +++ b/Sources/GroupBy.swift @@ -37,7 +37,7 @@ public struct GroupBy: QueryClause, Hashable { /** The list of key path strings to group results with */ - public let keyPaths: [RawKeyPath] + public let keyPaths: [KeyPathString] /** Initializes a `GroupBy` clause with an empty list of key path strings @@ -53,7 +53,7 @@ public struct GroupBy: QueryClause, Hashable { - parameter keyPath: a key path string to group results with - parameter keyPaths: a series of key path strings to group results with */ - public init(_ keyPath: RawKeyPath, _ keyPaths: RawKeyPath...) { + public init(_ keyPath: KeyPathString, _ keyPaths: KeyPathString...) { self.init([keyPath] + keyPaths) } @@ -63,7 +63,7 @@ public struct GroupBy: QueryClause, Hashable { - parameter keyPaths: a list of key path strings to group results with */ - public init(_ keyPaths: [RawKeyPath]) { + public init(_ keyPaths: [KeyPathString]) { self.keyPaths = keyPaths } diff --git a/Sources/ListMonitor.swift b/Sources/ListMonitor.swift index de330a2..1492405 100644 --- a/Sources/ListMonitor.swift +++ b/Sources/ListMonitor.swift @@ -997,7 +997,7 @@ public final class ListMonitor: Hashable { fileprivate var fetchedResultsController: CoreStoreFetchedResultsController fileprivate let taskGroup = DispatchGroup() - fileprivate let sectionIndexTransformer: (_ sectionName: RawKeyPath?) -> String? + fileprivate let sectionIndexTransformer: (_ sectionName: KeyPathString?) -> String? private let isSectioned: Bool diff --git a/Sources/NSEntityDescription+DynamicModel.swift b/Sources/NSEntityDescription+DynamicModel.swift index a7bad50..1c9ff17 100644 --- a/Sources/NSEntityDescription+DynamicModel.swift +++ b/Sources/NSEntityDescription+DynamicModel.swift @@ -76,14 +76,14 @@ internal extension NSEntityDescription { } @nonobjc - internal var keyPathsByAffectedKeyPaths: [RawKeyPath: Set] { + internal var keyPathsByAffectedKeyPaths: [KeyPathString: Set] { get { if let userInfo = self.userInfo, let value = userInfo[UserInfoKey.CoreStoreManagedObjectKeyPathsByAffectedKeyPaths] { - return value as! [RawKeyPath: Set] + return value as! [KeyPathString: Set] } return [:] } @@ -97,14 +97,14 @@ internal extension NSEntityDescription { } @nonobjc - internal var customGetterSetterByKeyPaths: [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter] { + internal var customGetterSetterByKeyPaths: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter] { get { if let userInfo = self.userInfo, let value = userInfo[UserInfoKey.CoreStoreManagedObjectCustomGetterSetterByKeyPaths] { - return value as! [RawKeyPath: CoreStoreManagedObject.CustomGetterSetter] + return value as! [KeyPathString: CoreStoreManagedObject.CustomGetterSetter] } return [:] } diff --git a/Sources/NSManagedObject+Convenience.swift b/Sources/NSManagedObject+Convenience.swift index 5c272e3..021bb82 100644 --- a/Sources/NSManagedObject+Convenience.swift +++ b/Sources/NSManagedObject+Convenience.swift @@ -84,7 +84,7 @@ public extension NSManagedObject { - returns: the primitive value for the KVC key */ @nonobjc @inline(__always) - public func getValue(forKvcKey kvcKey: RawKeyPath) -> Any? { + public func getValue(forKvcKey kvcKey: KeyPathString) -> Any? { self.willAccessValue(forKey: kvcKey) defer { @@ -102,7 +102,7 @@ public extension NSManagedObject { - returns: the primitive value transformed by the `didGetValue` closure */ @nonobjc @inline(__always) - public func getValue(forKvcKey kvcKey: RawKeyPath, didGetValue: (Any?) throws -> T) rethrows -> T { + public func getValue(forKvcKey kvcKey: KeyPathString, didGetValue: (Any?) throws -> T) rethrows -> T { self.willAccessValue(forKey: kvcKey) defer { @@ -121,7 +121,7 @@ public extension NSManagedObject { - returns: the primitive value transformed by the `didGetValue` closure */ @nonobjc @inline(__always) - public func getValue(forKvcKey kvcKey: RawKeyPath, willGetValue: () throws -> Void, didGetValue: (Any?) throws -> T) rethrows -> T { + public func getValue(forKvcKey kvcKey: KeyPathString, willGetValue: () throws -> Void, didGetValue: (Any?) throws -> T) rethrows -> T { self.willAccessValue(forKey: kvcKey) defer { @@ -139,7 +139,7 @@ public extension NSManagedObject { - parameter KVCKey: the KVC key */ @nonobjc @inline(__always) - public func setValue(_ value: Any?, forKvcKey KVCKey: RawKeyPath) { + public func setValue(_ value: Any?, forKvcKey KVCKey: KeyPathString) { self.willChangeValue(forKey: KVCKey) defer { @@ -157,7 +157,7 @@ public extension NSManagedObject { - parameter didSetValue: called after executing `setPrimitiveValue(forKey:)`. */ @nonobjc @inline(__always) - public func setValue(_ value: Any?, forKvcKey KVCKey: RawKeyPath, didSetValue: () -> Void) { + public func setValue(_ value: Any?, forKvcKey KVCKey: KeyPathString, didSetValue: () -> Void) { self.willChangeValue(forKey: KVCKey) defer { @@ -177,7 +177,7 @@ public extension NSManagedObject { - parameter didSetValue: called after executing `setPrimitiveValue(forKey:)`. */ @nonobjc @inline(__always) - public func setValue(_ value: T, forKvcKey KVCKey: RawKeyPath, willSetValue: (T) throws -> Any?, didSetValue: (Any?) -> Void = { _ in }) rethrows { + public func setValue(_ value: T, forKvcKey KVCKey: KeyPathString, willSetValue: (T) throws -> Any?, didSetValue: (Any?) -> Void = { _ in }) rethrows { self.willChangeValue(forKey: KVCKey) defer { @@ -212,7 +212,7 @@ public extension NSManagedObject { @available(*, deprecated, renamed: "getValue(forKvcKey:)") @nonobjc - public func accessValueForKVCKey(_ KVCKey: RawKeyPath) -> Any? { + public func accessValueForKVCKey(_ KVCKey: KeyPathString) -> Any? { self.willAccessValue(forKey: KVCKey) defer { @@ -225,7 +225,7 @@ public extension NSManagedObject { @available(*, deprecated, renamed: "getValue(forKvcKey:didGetValue:)") @discardableResult @nonobjc - public func accessValueForKVCKey(_ KVCKey: RawKeyPath, _ didAccessPrimitiveValue: (Any?) throws -> T) rethrows -> T { + public func accessValueForKVCKey(_ KVCKey: KeyPathString, _ didAccessPrimitiveValue: (Any?) throws -> T) rethrows -> T { self.willAccessValue(forKey: KVCKey) defer { @@ -237,7 +237,7 @@ public extension NSManagedObject { @available(*, deprecated, renamed: "setValue(_:forKvcKey:)") @nonobjc - public func setValue(_ value: Any?, forKVCKey KVCKey: RawKeyPath) { + public func setValue(_ value: Any?, forKVCKey KVCKey: KeyPathString) { self.willChangeValue(forKey: KVCKey) defer { @@ -250,7 +250,7 @@ public extension NSManagedObject { @available(*, deprecated, renamed: "setValue(_:forKvcKey:didSetValue:)") @discardableResult @nonobjc - public func setValue(_ value: Any?, forKVCKey KVCKey: RawKeyPath, _ didSetPrimitiveValue: (Any?) throws -> T) rethrows -> T { + public func setValue(_ value: Any?, forKVCKey KVCKey: KeyPathString, _ didSetPrimitiveValue: (Any?) throws -> T) rethrows -> T { self.willChangeValue(forKey: KVCKey) defer { diff --git a/Sources/NSManagedObject+ObjectiveC.swift b/Sources/NSManagedObject+ObjectiveC.swift index fdb702d..66301cd 100644 --- a/Sources/NSManagedObject+ObjectiveC.swift +++ b/Sources/NSManagedObject+ObjectiveC.swift @@ -38,7 +38,7 @@ public extension NSManagedObject { - returns: the primitive value for the KVC key */ @objc - public func cs_accessValueForKVCKey(_ KVCKey: RawKeyPath) -> Any? { + public func cs_accessValueForKVCKey(_ KVCKey: KeyPathString) -> Any? { return self.getValue(forKvcKey: KVCKey) } @@ -50,7 +50,7 @@ public extension NSManagedObject { - parameter KVCKey: the KVC key */ @objc - public func cs_setValue(_ value: Any?, forKVCKey KVCKey: RawKeyPath) { + public func cs_setValue(_ value: Any?, forKVCKey KVCKey: KeyPathString) { self.setValue(value, forKvcKey: KVCKey) } diff --git a/Sources/ObjectObserver.swift b/Sources/ObjectObserver.swift index 0a86067..5ace163 100644 --- a/Sources/ObjectObserver.swift +++ b/Sources/ObjectObserver.swift @@ -61,7 +61,7 @@ public protocol ObjectObserver: class { - parameter object: the `DynamicObject` instance being observed - parameter changedPersistentKeys: a `Set` of key paths for the attributes that were changed. Note that `changedPersistentKeys` only contains keys for attributes/relationships present in the persistent store, thus transient properties will not be reported. */ - func objectMonitor(_ monitor: ObjectMonitor, didUpdateObject object: ObjectEntityType, changedPersistentKeys: Set) + func objectMonitor(_ monitor: ObjectMonitor, didUpdateObject object: ObjectEntityType, changedPersistentKeys: Set) /** Handles processing right after `object` is deleted. (Optional) @@ -81,7 +81,7 @@ public extension ObjectObserver { public func objectMonitor(_ monitor: ObjectMonitor, willUpdateObject object: ObjectEntityType) { } - public func objectMonitor(_ monitor: ObjectMonitor, didUpdateObject object: ObjectEntityType, changedPersistentKeys: Set) { } + public func objectMonitor(_ monitor: ObjectMonitor, didUpdateObject object: ObjectEntityType, changedPersistentKeys: Set) { } public func objectMonitor(_ monitor: ObjectMonitor, didDeleteObject object: ObjectEntityType) { } } diff --git a/Sources/OrderBy.swift b/Sources/OrderBy.swift index 862dbda..71f5e3e 100644 --- a/Sources/OrderBy.swift +++ b/Sources/OrderBy.swift @@ -27,9 +27,9 @@ import Foundation import CoreData -// MARK: - RawKeyPath +// MARK: - KeyPathString -public typealias RawKeyPath = String +public typealias KeyPathString = String // MARK: - SortKey @@ -40,14 +40,14 @@ public typealias RawKeyPath = String public enum SortKey { /** - Indicates that the `RawKeyPath` should be sorted in ascending order + Indicates that the `KeyPathString` should be sorted in ascending order */ - case ascending(RawKeyPath) + case ascending(KeyPathString) /** - Indicates that the `RawKeyPath` should be sorted in descending order + Indicates that the `KeyPathString` should be sorted in descending order */ - case descending(RawKeyPath) + case descending(KeyPathString) } diff --git a/Sources/Relationship.swift b/Sources/Relationship.swift index 59807dd..2dacf7f 100644 --- a/Sources/Relationship.swift +++ b/Sources/Relationship.swift @@ -95,7 +95,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, deleteRule: DeleteRule = .nullify, versionHashModifier: String? = nil, renamingIdentifier: String? = nil, @@ -129,7 +129,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, inverse: @escaping (D) -> RelationshipContainer.ToOne, deleteRule: DeleteRule = .nullify, versionHashModifier: String? = nil, @@ -164,7 +164,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, inverse: @escaping (D) -> RelationshipContainer.ToManyOrdered, deleteRule: DeleteRule = .nullify, versionHashModifier: String? = nil, @@ -199,7 +199,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, inverse: @escaping (D) -> RelationshipContainer.ToManyUnordered, deleteRule: DeleteRule = .nullify, versionHashModifier: String? = nil, @@ -234,14 +234,14 @@ public enum RelationshipContainer { // MARK: RelationshipProtocol - public let keyPath: RawKeyPath + public let keyPath: KeyPathString internal let isToMany = false internal let isOrdered = false internal let deleteRule: NSDeleteRule internal let minCount: Int = 0 internal let maxCount: Int = 1 - internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> RawKeyPath?) + internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPathString?) internal let versionHashModifier: String? internal let renamingIdentifier: String? internal let affectedByKeyPaths: () -> Set @@ -294,7 +294,7 @@ public enum RelationshipContainer { // MARK: Private - private init(keyPath: RawKeyPath, inverseKeyPath: @escaping () -> RawKeyPath?, deleteRule: DeleteRule, versionHashModifier: String?, renamingIdentifier: String?, affectedByKeyPaths: @autoclosure @escaping () -> Set) { + private init(keyPath: KeyPathString, inverseKeyPath: @escaping () -> KeyPathString?, deleteRule: DeleteRule, versionHashModifier: String?, renamingIdentifier: String?, affectedByKeyPaths: @autoclosure @escaping () -> Set) { self.keyPath = keyPath self.deleteRule = deleteRule.nativeValue @@ -341,7 +341,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, minCount: Int = 0, maxCount: Int = 0, deleteRule: DeleteRule = .nullify, @@ -381,7 +381,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, minCount: Int = 0, maxCount: Int = 0, inverse: @escaping (D) -> RelationshipContainer.ToOne, @@ -422,7 +422,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, minCount: Int = 0, maxCount: Int = 0, inverse: @escaping (D) -> RelationshipContainer.ToManyOrdered, @@ -463,7 +463,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, minCount: Int = 0, maxCount: Int = 0, inverse: @escaping (D) -> RelationshipContainer.ToManyUnordered, @@ -502,7 +502,7 @@ public enum RelationshipContainer { // MARK: RelationshipProtocol - public let keyPath: RawKeyPath + public let keyPath: KeyPathString internal let isToMany = true internal let isOptional = true @@ -510,7 +510,7 @@ public enum RelationshipContainer { internal let deleteRule: NSDeleteRule internal let minCount: Int internal let maxCount: Int - internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> RawKeyPath?) + internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPathString?) internal let versionHashModifier: String? internal let renamingIdentifier: String? internal let affectedByKeyPaths: () -> Set @@ -615,7 +615,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, deleteRule: DeleteRule = .nullify, minCount: Int = 0, maxCount: Int = 0, @@ -655,7 +655,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, inverse: @escaping (D) -> RelationshipContainer.ToOne, deleteRule: DeleteRule = .nullify, minCount: Int = 0, @@ -696,7 +696,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, inverse: @escaping (D) -> RelationshipContainer.ToManyOrdered, deleteRule: DeleteRule = .nullify, minCount: Int = 0, @@ -737,7 +737,7 @@ public enum RelationshipContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, inverse: @escaping (D) -> RelationshipContainer.ToManyUnordered, deleteRule: DeleteRule = .nullify, minCount: Int = 0, @@ -776,7 +776,7 @@ public enum RelationshipContainer { // MARK: RelationshipProtocol - public let keyPath: RawKeyPath + public let keyPath: KeyPathString internal let isToMany = true internal let isOptional = true @@ -784,7 +784,7 @@ public enum RelationshipContainer { internal let deleteRule: NSDeleteRule internal let minCount: Int internal let maxCount: Int - internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> RawKeyPath?) + internal let inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPathString?) internal let versionHashModifier: String? internal let renamingIdentifier: String? internal let affectedByKeyPaths: () -> Set @@ -837,7 +837,7 @@ public enum RelationshipContainer { // MARK: Private - private init(keyPath: RawKeyPath, inverseKeyPath: @escaping () -> RawKeyPath?, deleteRule: DeleteRule, minCount: Int, maxCount: Int, versionHashModifier: String?, renamingIdentifier: String?, affectedByKeyPaths: @autoclosure @escaping () -> Set) { + private init(keyPath: KeyPathString, inverseKeyPath: @escaping () -> KeyPathString?, deleteRule: DeleteRule, minCount: Int, maxCount: Int, versionHashModifier: String?, renamingIdentifier: String?, affectedByKeyPaths: @autoclosure @escaping () -> Set) { self.keyPath = keyPath self.deleteRule = deleteRule.nativeValue @@ -1210,11 +1210,11 @@ extension RelationshipContainer.ToManyUnordered { internal protocol RelationshipProtocol: class { - var keyPath: RawKeyPath { get } + var keyPath: KeyPathString { get } var isToMany: Bool { get } var isOrdered: Bool { get } var deleteRule: NSDeleteRule { get } - var inverse: (type: CoreStoreObject.Type, keyPath: () -> RawKeyPath?) { get } + var inverse: (type: CoreStoreObject.Type, keyPath: () -> KeyPathString?) { get } var affectedByKeyPaths: () -> Set { get } weak var parentObject: CoreStoreObject? { get set } var versionHashModifier: String? { get } diff --git a/Sources/SectionBy.swift b/Sources/SectionBy.swift index c847ac7..f9ac2b7 100644 --- a/Sources/SectionBy.swift +++ b/Sources/SectionBy.swift @@ -47,7 +47,7 @@ public struct SectionBy { - parameter sectionKeyPath: the key path to use to group the objects into sections */ - public init(_ sectionKeyPath: RawKeyPath) { + public init(_ sectionKeyPath: KeyPathString) { self.init(sectionKeyPath, { $0 }) } @@ -59,7 +59,7 @@ public struct SectionBy { - parameter sectionKeyPath: the key path to use to group the objects into sections - parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name */ - public init(_ sectionKeyPath: RawKeyPath, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) { + public init(_ sectionKeyPath: KeyPathString, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) { self.sectionKeyPath = sectionKeyPath self.sectionIndexTransformer = sectionIndexTransformer @@ -68,6 +68,6 @@ public struct SectionBy { // MARK: Internal - internal let sectionKeyPath: RawKeyPath + internal let sectionKeyPath: KeyPathString internal let sectionIndexTransformer: (_ sectionName: String?) -> String? } diff --git a/Sources/Select.swift b/Sources/Select.swift index 10a9149..7fe2efd 100644 --- a/Sources/Select.swift +++ b/Sources/Select.swift @@ -74,7 +74,7 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { - parameter keyPath: the attribute name - returns: a `SelectTerm` to a `Select` clause for querying an entity attribute */ - public static func attribute(_ keyPath: RawKeyPath) -> SelectTerm { + public static func attribute(_ keyPath: KeyPathString) -> SelectTerm { return ._attribute(keyPath) } @@ -91,7 +91,7 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { - parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "average()" is used - returns: a `SelectTerm` to a `Select` clause for querying the average value of an attribute */ - public static func average(_ keyPath: RawKeyPath, as alias: RawKeyPath? = nil) -> SelectTerm { + public static func average(_ keyPath: KeyPathString, as alias: KeyPathString? = nil) -> SelectTerm { return ._aggregate( function: "average:", @@ -113,7 +113,7 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { - parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "count()" is used - returns: a `SelectTerm` to a `Select` clause for a count query */ - public static func count(_ keyPath: RawKeyPath, as alias: RawKeyPath? = nil) -> SelectTerm { + public static func count(_ keyPath: KeyPathString, as alias: KeyPathString? = nil) -> SelectTerm { return ._aggregate( function: "count:", @@ -135,7 +135,7 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { - parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "max()" is used - returns: a `SelectTerm` to a `Select` clause for querying the maximum value for an attribute */ - public static func maximum(_ keyPath: RawKeyPath, as alias: RawKeyPath? = nil) -> SelectTerm { + public static func maximum(_ keyPath: KeyPathString, as alias: KeyPathString? = nil) -> SelectTerm { return ._aggregate( function: "max:", @@ -157,7 +157,7 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { - parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "min()" is used - returns: a `SelectTerm` to a `Select` clause for querying the minimum value for an attribute */ - public static func minimum(_ keyPath: RawKeyPath, as alias: RawKeyPath? = nil) -> SelectTerm { + public static func minimum(_ keyPath: KeyPathString, as alias: KeyPathString? = nil) -> SelectTerm { return ._aggregate( function: "min:", @@ -179,7 +179,7 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { - parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "sum()" is used - returns: a `SelectTerm` to a `Select` clause for querying the sum value for an attribute */ - public static func sum(_ keyPath: RawKeyPath, as alias: RawKeyPath? = nil) -> SelectTerm { + public static func sum(_ keyPath: KeyPathString, as alias: KeyPathString? = nil) -> SelectTerm { return ._aggregate( function: "sum:", @@ -202,7 +202,7 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { - parameter alias: the dictionary key to use to access the result. Ignored when the query return value is not an `NSDictionary`. If `nil`, the default key "objecID" is used - returns: a `SelectTerm` to a `Select` clause for querying the sum value for an attribute */ - public static func objectID(as alias: RawKeyPath? = nil) -> SelectTerm { + public static func objectID(as alias: KeyPathString? = nil) -> SelectTerm { return ._identity( alias: alias ?? "objectID", @@ -213,17 +213,17 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { // MARK: ExpressibleByStringLiteral - public init(stringLiteral value: RawKeyPath) { + public init(stringLiteral value: KeyPathString) { self = ._attribute(value) } - public init(unicodeScalarLiteral value: RawKeyPath) { + public init(unicodeScalarLiteral value: KeyPathString) { self = ._attribute(value) } - public init(extendedGraphemeClusterLiteral value: RawKeyPath) { + public init(extendedGraphemeClusterLiteral value: KeyPathString) { self = ._attribute(value) } @@ -274,8 +274,8 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { // MARK: Internal - case _attribute(RawKeyPath) - case _aggregate(function: String, keyPath: RawKeyPath, alias: String, nativeType: NSAttributeType) + case _attribute(KeyPathString) + case _aggregate(function: String, keyPath: KeyPathString, alias: String, nativeType: NSAttributeType) case _identity(alias: String, nativeType: NSAttributeType) } @@ -479,7 +479,7 @@ internal extension Collection where Iterator.Element == SelectTerm { fetchRequest.propertiesToFetch = propertiesToFetch } - internal func keyPathForFirstSelectTerm() -> RawKeyPath { + internal func keyPathForFirstSelectTerm() -> KeyPathString { switch self.first! { diff --git a/Sources/Value.swift b/Sources/Value.swift index b7e8f4c..3ba55ab 100644 --- a/Sources/Value.swift +++ b/Sources/Value.swift @@ -124,7 +124,7 @@ public enum ValueContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, `default`: @autoclosure @escaping () -> V, isIndexed: Bool = false, isTransient: Bool = false, @@ -207,7 +207,7 @@ public enum ValueContainer { return V.cs_rawAttributeType } - public let keyPath: RawKeyPath + public let keyPath: KeyPathString internal let isOptional = false internal let isIndexed: Bool @@ -323,7 +323,7 @@ public enum ValueContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, `default`: @autoclosure @escaping () -> V? = nil, isIndexed: Bool = false, isTransient: Bool = false, @@ -405,7 +405,7 @@ public enum ValueContainer { return V.cs_rawAttributeType } - public let keyPath: RawKeyPath + public let keyPath: KeyPathString internal let isOptional = true internal let isIndexed: Bool internal let isTransient: Bool @@ -485,7 +485,7 @@ public extension ValueContainer.Required where V: EmptyableAttributeType { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public convenience init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, isIndexed: Bool = false, isTransient: Bool = false, versionHashModifier: String? = nil, @@ -579,7 +579,7 @@ public enum TransformableContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, `default`: @autoclosure @escaping () -> V, isIndexed: Bool = false, isTransient: Bool = false, @@ -660,7 +660,7 @@ public enum TransformableContainer { return .transformableAttributeType } - public let keyPath: RawKeyPath + public let keyPath: KeyPathString internal let isOptional = false internal let isIndexed: Bool @@ -774,7 +774,7 @@ public enum TransformableContainer { - parameter affectedByKeyPaths: a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`. */ public init( - _ keyPath: RawKeyPath, + _ keyPath: KeyPathString, `default`: @autoclosure @escaping () -> V? = nil, isIndexed: Bool = false, isTransient: Bool = false, @@ -855,7 +855,7 @@ public enum TransformableContainer { return .transformableAttributeType } - public let keyPath: RawKeyPath + public let keyPath: KeyPathString internal let isOptional = true internal let isIndexed: Bool @@ -1211,7 +1211,7 @@ internal protocol AttributeProtocol: class { static var attributeType: NSAttributeType { get } - var keyPath: RawKeyPath { get } + var keyPath: KeyPathString { get } var isOptional: Bool { get } var isIndexed: Bool { get } var isTransient: Bool { get } diff --git a/Sources/Where.swift b/Sources/Where.swift index 49fbf36..65cea6a 100644 --- a/Sources/Where.swift +++ b/Sources/Where.swift @@ -177,7 +177,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable { - parameter keyPath: the keyPath to compare with - parameter value: the arguments for the `==` operator */ - public init(_ keyPath: RawKeyPath, isEqualTo value: Void?) { + public init(_ keyPath: KeyPathString, isEqualTo value: Void?) { self.init(NSPredicate(format: "\(keyPath) == nil")) } @@ -188,7 +188,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable { - parameter keyPath: the keyPath to compare with - parameter value: the arguments for the `==` operator */ - public init(_ keyPath: RawKeyPath, isEqualTo value: T?) { + public init(_ keyPath: KeyPathString, isEqualTo value: T?) { switch value { @@ -207,7 +207,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable { - parameter keyPath: the keyPath to compare with - parameter object: the arguments for the `==` operator */ - public init(_ keyPath: RawKeyPath, isEqualTo object: T?) { + public init(_ keyPath: KeyPathString, isEqualTo object: T?) { switch object { @@ -226,7 +226,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable { - parameter keyPath: the keyPath to compare with - parameter list: the sequence to check membership of */ - public init(_ keyPath: RawKeyPath, isMemberOf list: S) where S.Iterator.Element: QueryableAttributeType { + public init(_ keyPath: KeyPathString, isMemberOf list: S) where S.Iterator.Element: QueryableAttributeType { self.init(NSPredicate(format: "\(keyPath) IN %@", list.map({ $0.cs_toQueryableNativeType() }) as NSArray)) } @@ -237,7 +237,7 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable { - parameter keyPath: the keyPath to compare with - parameter list: the sequence to check membership of */ - public init(_ keyPath: RawKeyPath, isMemberOf list: S) where S.Iterator.Element: DynamicObject { + public init(_ keyPath: KeyPathString, isMemberOf list: S) where S.Iterator.Element: DynamicObject { self.init(NSPredicate(format: "\(keyPath) IN %@", list.map({ $0.cs_id() }) as NSArray)) }