Skip to content

Commit 5779429

Browse files
committed
Removed StringKeyPaths
Since `KeyPath`s have now a description, it's unneeded to use `NSExpression`, and it's possible to directly use key path to computed properties.
1 parent c1e711b commit 5779429

15 files changed

+72
-370
lines changed

Sources/SafeFetching/Extensions/KeyPath+Extensions.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import Foundation
77

88
extension KeyPath where Root: NSObject {
99

10-
/// Name of the property pointed at
11-
///
12-
/// - important: Will exit the program if the property is a computed one. The language allows
13-
/// to use a `KeyPath` with a computed property, but the 'Foundation' key paths will fail in that case.
14-
var label: String { NSExpression(forKeyPath: self).keyPath }
10+
/// Name of the property pointed at.
11+
var label: Substring {
12+
let description = String(describing: self)
13+
let prefixToDrop = "\\\(Root.self)."
14+
return description.dropFirst(prefixToDrop.count)
15+
}
1516
}

Sources/SafeFetching/Predicate/DatabaseValue.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
import Foundation
77
import CoreData
88

9+
// MARK: - DatabaseValueIdentification
10+
911
/// Stub type with internal init to ensure no conformance to ``DatabaseValue`` can be added outside of the package
1012
public struct DatabaseValueIdentification {
1113
init() {}
1214
}
1315

16+
// MARK: - DatabaseValue
17+
1418
/// A type that can be used for an attribute in a predicate when fetching a CoreData store
1519
///
1620
/// Used to constraint some fetch functions to use only a valid storable type
@@ -19,7 +23,7 @@ public protocol DatabaseValue {
1923
static var identification: DatabaseValueIdentification { get }
2024
}
2125

22-
// MARK: Field
26+
// MARK: - Field
2327

2428
extension String: DatabaseValue {
2529
public static let identification = DatabaseValueIdentification()
@@ -69,7 +73,13 @@ extension URL: DatabaseValue {
6973
public static let identification = DatabaseValueIdentification()
7074
}
7175

72-
// MARK: Relationship
76+
// MARK: - RawRepresentable
77+
78+
extension RawRepresentable where RawValue: DatabaseValue {
79+
public static var identification: DatabaseValueIdentification { DatabaseValueIdentification() }
80+
}
81+
82+
// MARK: - Relationship
7383

7484
extension NSManagedObject: DatabaseValue {
7585
public static let identification = DatabaseValueIdentification()
@@ -89,7 +99,7 @@ extension NSOrderedSet: DatabaseValue {
8999
public static let identification = DatabaseValueIdentification()
90100
}
91101

92-
// MARK: Optional
102+
// MARK: - Optional
93103

94104
extension Optional: DatabaseValue where Wrapped: DatabaseValue {
95105
public static var identification: DatabaseValueIdentification {

Sources/SafeFetching/Predicate/Declarations/BooleanStringKeyPathPredicate+Comparison.swift

Lines changed: 0 additions & 56 deletions
This file was deleted.

Sources/SafeFetching/Predicate/Declarations/StringKeyPredicateRightValue+DatabaseValue.swift

Lines changed: 0 additions & 27 deletions
This file was deleted.

Sources/SafeFetching/Predicate/Declarations/StringKeyPredicateRightValue+OptionSet.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@
33
// Copyright © 2021-present Alexis Bridoux.
44
// MIT license, see LICENSE file for details
55

6-
public extension Builders.StringKeyPathPredicateRightValue where Value: OptionSet, Value.RawValue: BinaryInteger, Value: DatabaseTestValue {
6+
public extension Builders.KeyPathPredicateRightValue where Value: OptionSet, Value.RawValue: BinaryInteger, Value: DatabaseTestValue {
77

88
/// - important: Should be used only with options set types that are converted from/to a primitive value
9-
static func intersects(_ value: Value) -> Builders.StringKeyPathPredicateRightValue<Entity, Value, Value> {
10-
.init { keyPathString in
11-
.init(keyPathString: keyPathString.key) { "\($0) & \(value.testValue) == \(value.testValue)" }
9+
static func intersects(_ value: Value) -> Builders.KeyPathPredicateRightValue<Entity, Value, Value> {
10+
.init { keyPath in
11+
.init(keyPathString: keyPath.label) { "\($0) & \(value.testValue) == \(value.testValue)" }
1212
}
1313
}
1414

1515
/// - important: Should be used only with options set types that are converted from/to a primitive value
16-
static func doesNotIntersect(_ value: Value) -> Builders.StringKeyPathPredicateRightValue<Entity, Value, Value> {
17-
.init { keyPathString in
18-
.init(keyPathString: keyPathString.key) { "\($0) & \(value.testValue) != \(value.testValue)" }
16+
static func doesNotIntersect(_ value: Value) -> Builders.KeyPathPredicateRightValue<Entity, Value, Value> {
17+
.init { keyPath in
18+
.init(keyPathString: keyPath.label) { "\($0) & \(value.testValue) != \(value.testValue)" }
1919
}
2020
}
2121
}
2222

23-
public extension Builders.StringKeyPathPredicateRightValue {
23+
public extension Builders.KeyPathPredicateRightValue {
2424

2525
/// - important: Should be used only with options set types that are converted from/to a primitive value
26-
static func intersects<W: OptionSet & DatabaseTestValue>(_ value: W) -> Builders.StringKeyPathPredicateRightValue<Entity, Value, Value>
26+
static func intersects<W: OptionSet & DatabaseTestValue>(_ value: W) -> Builders.KeyPathPredicateRightValue<Entity, Value, Value>
2727
where Value == W?, W.RawValue: BinaryInteger {
28-
.init { keyPathString in
29-
.init(keyPathString: keyPathString.key) { "\($0) & \(value.testValue) == \(value.testValue)" }
28+
.init { keyPath in
29+
.init(keyPathString: keyPath.label) { "\($0) & \(value.testValue) == \(value.testValue)" }
3030
}
3131
}
3232

3333
/// - important: Should be used only with options set types that are converted from/to a primitive value
34-
static func doesNotIntersect<W: OptionSet & DatabaseTestValue>(_ value: W) -> Builders.StringKeyPathPredicateRightValue<Entity, Value, Value>
34+
static func doesNotIntersect<W: OptionSet & DatabaseTestValue>(_ value: W) -> Builders.KeyPathPredicateRightValue<Entity, Value, Value>
3535
where Value == W?, W.RawValue: BinaryInteger {
36-
.init { keyPathString in
37-
.init(keyPathString: keyPathString.key) { "\($0) & \(value.testValue) != \(value.testValue)" }
36+
.init { keyPath in
37+
.init(keyPathString: keyPath.label) { "\($0) & \(value.testValue) != \(value.testValue)" }
3838
}
3939
}
4040
}

Sources/SafeFetching/Predicate/StringKeyPath.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

Sources/SafeFetching/Predicate/Types/CompoundPredicate.swift

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -102,51 +102,3 @@ public func || <E: NSManagedObject>(
102102
rightPredicate: .init(keyPath: rhs)
103103
)
104104
}
105-
106-
// MARK: - StringKeyPath <-> Predicate
107-
108-
public func && <E: NSManagedObject>(
109-
lhs: StringKeyPath<E, Bool>,
110-
rhs: Builders.Predicate<E>
111-
) -> Builders.CompoundPredicate<E> {
112-
Builders.CompoundPredicate(
113-
joinOperator: .and,
114-
leftPredicate: .init(keyPathString: lhs.key),
115-
rightPredicate: rhs
116-
)
117-
}
118-
119-
public func || <E: NSManagedObject>(
120-
lhs: StringKeyPath<E, Bool>,
121-
rhs: Builders.Predicate<E>
122-
) -> Builders.CompoundPredicate<E> {
123-
Builders.CompoundPredicate(
124-
joinOperator: .or,
125-
leftPredicate: .init(keyPathString: lhs.key),
126-
rightPredicate: rhs
127-
)
128-
}
129-
130-
// MARK: - Predicate <-> StringKeyPath
131-
132-
public func && <E: NSManagedObject>(
133-
lhs: Builders.Predicate<E>,
134-
rhs: StringKeyPath<E, Bool>
135-
) -> Builders.CompoundPredicate<E> {
136-
Builders.CompoundPredicate(
137-
joinOperator: .and,
138-
leftPredicate: lhs,
139-
rightPredicate: .init(keyPathString: rhs.key)
140-
)
141-
}
142-
143-
public func || <E: NSManagedObject>(
144-
lhs: Builders.Predicate<E>,
145-
rhs: StringKeyPath<E, Bool>
146-
) -> Builders.CompoundPredicate<E> {
147-
Builders.CompoundPredicate(
148-
joinOperator: .or,
149-
leftPredicate: lhs,
150-
rightPredicate: .init(keyPathString: rhs.key)
151-
)
152-
}

Sources/SafeFetching/Predicate/Types/Predicate.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extension Builders {
99

1010
public class Predicate<Entity: NSManagedObject> {
1111

12-
public typealias Formatter = (_ keyPath: String) -> String
12+
public typealias Formatter = (_ keyPath: Substring) -> String
1313

1414
public let nsValue: NSPredicate
1515

@@ -57,7 +57,7 @@ extension Builders.Predicate {
5757
extension Builders.Predicate {
5858

5959
public convenience init<TestValue: DatabaseTestValue>(
60-
keyPathString: String,
60+
keyPathString: Substring,
6161
operatorString: String,
6262
value: TestValue,
6363
isInverted: Bool = false
@@ -67,7 +67,7 @@ extension Builders.Predicate {
6767
}
6868

6969
public convenience init(
70-
keyPathString: String,
70+
keyPathString: Substring,
7171
isInverted: Bool = false,
7272
formatter: @escaping Formatter
7373
) {
@@ -77,7 +77,7 @@ extension Builders.Predicate {
7777
}
7878

7979
public convenience init(
80-
keyPathString: String,
80+
keyPathString: Substring,
8181
isInverted: Bool = false
8282
) {
8383
let testValue = isInverted ? "0" : "1"

Sources/SafeFetching/Predicate/Types/StringKeyPathPredicateRightValue.swift

Lines changed: 0 additions & 33 deletions
This file was deleted.

Sources/SafeFetching/Request/RequestBuilder+BuildingSteps.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ public extension Builders.Request where Step == TargetStep {
7474
request.predicate = Builders.Predicate<Entity>(keyPath: keyPath).nsValue
7575
return .init(request: request)
7676
}
77-
78-
/// Pass a boolean string key path
79-
///
80-
/// ### Examples
81-
/// - `.where(.isDownloaded)`
82-
/// - `.where(!.isDownloaded)`
83-
func `where`(_ keyPath: StringKeyPath<Entity, Bool>) -> Builders.Request<Entity, PredicateStep, Output> {
84-
request.predicate = Builders.Predicate<Entity>(keyPathString: keyPath.key).nsValue
85-
return .init(request: request)
86-
}
8777
}
8878

8979

0 commit comments

Comments
 (0)