From 97242d9726229d0a0afe94c69585c4f33855276c Mon Sep 17 00:00:00 2001 From: John Estropia Date: Mon, 2 Oct 2017 12:00:45 +0900 Subject: [PATCH] added type-erasers for CoreStoreObject property containers --- CoreStore.xcodeproj/project.pbxproj | 10 ++++ Sources/AnyCoreStoreKeyPath.swift | 83 +++++++++++++++++++++++++++++ Sources/OrderBy.swift | 56 ++----------------- 3 files changed, 97 insertions(+), 52 deletions(-) create mode 100644 Sources/AnyCoreStoreKeyPath.swift diff --git a/CoreStore.xcodeproj/project.pbxproj b/CoreStore.xcodeproj/project.pbxproj index c7c4c1e..8967c1e 100644 --- a/CoreStore.xcodeproj/project.pbxproj +++ b/CoreStore.xcodeproj/project.pbxproj @@ -520,6 +520,10 @@ B5CA2B091F7E5ACA004B1936 /* WhereClauseType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B071F7E5ACA004B1936 /* WhereClauseType.swift */; }; B5CA2B0A1F7E5ACA004B1936 /* WhereClauseType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B071F7E5ACA004B1936 /* WhereClauseType.swift */; }; B5CA2B0B1F7E5ACA004B1936 /* WhereClauseType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B071F7E5ACA004B1936 /* WhereClauseType.swift */; }; + B5CA2B121F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */; }; + B5CA2B131F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */; }; + B5CA2B141F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */; }; + B5CA2B151F81DBFF004B1936 /* AnyCoreStoreKeyPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */; }; B5D1E22C19FA9FBC003B2874 /* CoreStoreError.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D1E22B19FA9FBC003B2874 /* CoreStoreError.swift */; }; B5D339B41E925C2B00C880DE /* DynamicModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D339B31E925C2B00C880DE /* DynamicModelTests.swift */; }; B5D339B51E925C2B00C880DE /* DynamicModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5D339B31E925C2B00C880DE /* DynamicModelTests.swift */; }; @@ -862,6 +866,7 @@ B5C976E21C6C9F6A00B1AF90 /* UnsafeDataTransaction+Observing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UnsafeDataTransaction+Observing.swift"; sourceTree = ""; }; B5C976E61C6E3A5900B1AF90 /* CoreStoreFetchedResultsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreStoreFetchedResultsController.swift; sourceTree = ""; }; B5CA2B071F7E5ACA004B1936 /* WhereClauseType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WhereClauseType.swift; sourceTree = ""; }; + B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyCoreStoreKeyPath.swift; sourceTree = ""; }; B5D1E22B19FA9FBC003B2874 /* CoreStoreError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreStoreError.swift; sourceTree = ""; }; B5D339B31E925C2B00C880DE /* DynamicModelTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicModelTests.swift; sourceTree = ""; }; B5D339D71E9489AB00C880DE /* CoreStoreObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreStoreObject.swift; sourceTree = ""; }; @@ -1290,6 +1295,7 @@ children = ( B5A1DAC71F111BFA003CF369 /* KeyPath+Querying.swift */, B5D339EB1E9495E500C880DE /* CoreStoreObject+Querying.swift */, + B5CA2B111F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift */, ); name = "KeyPath Utilities"; sourceTree = ""; @@ -1968,6 +1974,7 @@ B549F65E1E569C7400FBAB2D /* QueryableAttributeType.swift in Sources */, B5E84F211AFF84860064E85B /* CoreStore+Observing.swift in Sources */, B559CD431CAA8B6300E4D58B /* CSSetupResult.swift in Sources */, + B5CA2B121F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */, B5A991EC1E9DC2CE0091A2E3 /* VersionLock.swift in Sources */, B5FE4DA71C84FB4400FA6A91 /* InMemoryStore.swift in Sources */, B52F743D1E9B8724005F3DAC /* DynamicSchema.swift in Sources */, @@ -2160,6 +2167,7 @@ B549F65F1E569C7400FBAB2D /* QueryableAttributeType.swift in Sources */, B559CD451CAA8B6300E4D58B /* CSSetupResult.swift in Sources */, 82BA18B81C4BBD4200A0916E /* TypeErasedClauses.swift in Sources */, + B5CA2B131F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */, B5A991ED1E9DC2CE0091A2E3 /* VersionLock.swift in Sources */, B5ECDBEE1CA6BF2000C7F112 /* CSFrom.swift in Sources */, B52F743E1E9B8724005F3DAC /* DynamicSchema.swift in Sources */, @@ -2352,6 +2360,7 @@ B549F6611E569C7400FBAB2D /* QueryableAttributeType.swift in Sources */, B52DD19B1BE1F92800949AFE /* CoreStoreLogger.swift in Sources */, B52DD1991BE1F92800949AFE /* DefaultLogger.swift in Sources */, + B5CA2B151F81DBFF004B1936 /* AnyCoreStoreKeyPath.swift in Sources */, B5A991EF1E9DC2CE0091A2E3 /* VersionLock.swift in Sources */, B5220E201D130813009BC71E /* CSObjectMonitor.swift in Sources */, B52F74401E9B8724005F3DAC /* DynamicSchema.swift in Sources */, @@ -2544,6 +2553,7 @@ B549F6601E569C7400FBAB2D /* QueryableAttributeType.swift in Sources */, B559CD461CAA8B6300E4D58B /* CSSetupResult.swift in Sources */, B56321A61BD65216006C9394 /* MigrationType.swift in Sources */, + B5CA2B141F81DBFE004B1936 /* AnyCoreStoreKeyPath.swift in Sources */, B5A991EE1E9DC2CE0091A2E3 /* VersionLock.swift in Sources */, B5ECDBEF1CA6BF2000C7F112 /* CSFrom.swift in Sources */, B52F743F1E9B8724005F3DAC /* DynamicSchema.swift in Sources */, diff --git a/Sources/AnyCoreStoreKeyPath.swift b/Sources/AnyCoreStoreKeyPath.swift new file mode 100644 index 0000000..36b4add --- /dev/null +++ b/Sources/AnyCoreStoreKeyPath.swift @@ -0,0 +1,83 @@ +// +// AnyCoreStoreKeyPath.swift +// CoreStore +// +// Created by John Estropia on 2017/10/02. +// Copyright © 2017 John Rommel Estropia. All rights reserved. +// + +import Foundation + + +// MARK: - AnyCoreStoreKeyPath + +public protocol AnyCoreStoreKeyPath { + + var cs_keyPathString: String { get } +} + +// SE-0143 is not implemented: https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md +//extension KeyPath: AnyCoreStoreKeyPath where Root: NSManagedObject, Value: ImportableAttributeType { +// +// public var cs_keyPathString: String { +// +// return self._kvcKeyPathString! +// } +//} + +extension ValueContainer.Required: AnyCoreStoreKeyPath { + + public var cs_keyPathString: String { + + return self.keyPath + } +} + +extension ValueContainer.Optional: AnyCoreStoreKeyPath { + + public var cs_keyPathString: String { + + return self.keyPath + } +} + +extension TransformableContainer.Required: AnyCoreStoreKeyPath { + + public var cs_keyPathString: String { + + return self.keyPath + } +} + +extension TransformableContainer.Optional: AnyCoreStoreKeyPath { + + public var cs_keyPathString: String { + + return self.keyPath + } +} + +extension RelationshipContainer.ToOne: AnyCoreStoreKeyPath { + + public var cs_keyPathString: String { + + return self.keyPath + } +} + +extension RelationshipContainer.ToManyOrdered: AnyCoreStoreKeyPath { + + public var cs_keyPathString: String { + + return self.keyPath + } +} + +extension RelationshipContainer.ToManyUnordered: AnyCoreStoreKeyPath { + + public var cs_keyPathString: String { + + return self.keyPath + } +} + diff --git a/Sources/OrderBy.swift b/Sources/OrderBy.swift index 39e92ee..96e575d 100644 --- a/Sources/OrderBy.swift +++ b/Sources/OrderBy.swift @@ -262,65 +262,17 @@ public extension OrderBy.SortKey where D: CoreStoreObject { /** Indicates that the `KeyPathString` should be sorted in ascending order */ - public static func ascending(_ attribute: (D) -> A) -> OrderBy.SortKey where A: ValueContainer.Required { + public static func ascending(_ attribute: (D) -> T) -> OrderBy.SortKey { - return .ascending(attribute(D.meta).keyPath) - } - - /** - Indicates that the `KeyPathString` should be sorted in ascending order - */ - public static func ascending(_ attribute: (D) -> A) -> OrderBy.SortKey where A: ValueContainer.Optional { - - return .ascending(attribute(D.meta).keyPath) - } - - /** - Indicates that the `KeyPathString` should be sorted in ascending order - */ - public static func ascending(_ attribute: (D) -> A) -> OrderBy.SortKey where A: TransformableContainer.Required { - - return .ascending(attribute(D.meta).keyPath) - } - - /** - Indicates that the `KeyPathString` should be sorted in ascending order - */ - public static func ascending(_ attribute: (D) -> A) -> OrderBy.SortKey where A: TransformableContainer.Optional { - - return .ascending(attribute(D.meta).keyPath) + return .ascending(attribute(D.meta).cs_keyPathString) } /** Indicates that the `KeyPathString` should be sorted in descending order */ - public static func descending(_ attribute: (D) -> A) -> OrderBy.SortKey where A: ValueContainer.Required { + public static func descending(_ attribute: (D) -> T) -> OrderBy.SortKey { - return .descending(attribute(D.meta).keyPath) - } - - /** - Indicates that the `KeyPathString` should be sorted in descending order - */ - public static func descending(_ attribute: (D) -> A) -> OrderBy.SortKey where A: ValueContainer.Optional { - - return .descending(attribute(D.meta).keyPath) - } - - /** - Indicates that the `KeyPathString` should be sorted in descending order - */ - public static func descending(_ attribute: (D) -> A) -> OrderBy.SortKey where A: TransformableContainer.Required { - - return .descending(attribute(D.meta).keyPath) - } - - /** - Indicates that the `KeyPathString` should be sorted in descending order - */ - public static func descending(_ attribute: (D) -> A) -> OrderBy.SortKey where A: TransformableContainer.Optional { - - return .descending(attribute(D.meta).keyPath) + return .descending(attribute(D.meta).cs_keyPathString) } }