Skip to content

Commit 2a651f8

Browse files
committed
Abstract KeyPath decoding
1 parent ef5b943 commit 2a651f8

5 files changed

Lines changed: 30 additions & 27 deletions

File tree

Sources/PredicateView/Expressions/Abstract/PredicateExpressionConvertible.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ extension PredicateExpressionConvertible {
2626
attribute
2727
}
2828
}
29+
30+
extension PredicateExpressionConvertible where Self: KeyPathExpression {
31+
func decoded(
32+
keyPath keyPathExpression: KeyPathPredicateExpression,
33+
attribute: @autoclosure () -> Attribute
34+
) -> Self? {
35+
guard keyPathExpression.keyPath == keyPath else { return nil }
36+
var copy = self
37+
copy.attribute = attribute()
38+
return copy
39+
}
40+
}

Sources/PredicateView/Expressions/BoolExpression.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,13 @@ struct BoolExpression<Root>: ContentExpression, WrappablePredicateExpression {
6969
) -> (any Expression<Root>)? {
7070
switch expression {
7171
case let expression as PredicateExpressions.Equal<KeyPathPredicateExpression, ValuePredicateExpression>:
72-
BoolExpression(
73-
keyPath: expression.lhs.keyPath,
74-
title: title,
72+
decoded(
73+
keyPath: expression.lhs,
7574
attribute: .init(operator: .is, value: expression.rhs.value)
7675
)
7776
case let expression as PredicateExpressions.NotEqual<KeyPathPredicateExpression, ValuePredicateExpression>:
78-
BoolExpression(
79-
keyPath: expression.lhs.keyPath,
80-
title: title,
77+
decoded(
78+
keyPath: expression.lhs,
8179
attribute: .init(operator: .isNot, value: expression.rhs.value)
8280
)
8381
default:

Sources/PredicateView/Expressions/DateExpression.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ struct DateExpression<Root>: ContentExpression, WrappablePredicateExpression {
104104
) -> (any Expression<Root>)? {
105105
switch expression {
106106
case let expression as PredicateExpressions.Comparison<KeyPathPredicateExpression, ValuePredicateExpression>:
107-
DateExpression(
108-
keyPath: expression.lhs.keyPath,
109-
title: title,
107+
decoded(
108+
keyPath: expression.lhs,
110109
attribute: .init(operator: .init(expression.op) ?? .before, value: expression.rhs.value)
111110
)
112111
case let expression as PredicateExpressions.Conjunction<Comparison, Comparison>:

Sources/PredicateView/Expressions/NumericExpression.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,18 @@ struct NumericExpression<Root, Number>: ContentExpression, WrappablePredicateExp
9090
) -> (any Expression<Root>)? {
9191
switch expression {
9292
case let expression as PredicateExpressions.Equal<KeyPathPredicateExpression, ValuePredicateExpression>:
93-
NumericExpression(
94-
keyPath: expression.lhs.keyPath,
95-
title: title,
93+
decoded(
94+
keyPath: expression.lhs,
9695
attribute: .init(operator: .equals, value: expression.rhs.value)
9796
)
9897
case let expression as PredicateExpressions.NotEqual<KeyPathPredicateExpression, ValuePredicateExpression>:
99-
NumericExpression(
100-
keyPath: expression.lhs.keyPath,
101-
title: title,
98+
decoded(
99+
keyPath: expression.lhs,
102100
attribute: .init(operator: .isNotEqual, value: expression.rhs.value)
103101
)
104102
case let expression as PredicateExpressions.Comparison<KeyPathPredicateExpression, ValuePredicateExpression>:
105-
NumericExpression(
106-
keyPath: expression.lhs.keyPath,
107-
title: title,
103+
decoded(
104+
keyPath: expression.lhs,
108105
attribute: .init(operator: .init(expression.op) ?? .equals, value: expression.rhs.value)
109106
)
110107
default:

Sources/PredicateView/Expressions/StringExpression.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,18 @@ struct StringExpression<Root>: ContentExpression, WrappablePredicateExpression {
6262
) -> (any Expression<Root>)? {
6363
switch expression {
6464
case let expression as PredicateExpressions.Equal<KeyPathPredicateExpression, ValuePredicateExpression>:
65-
StringExpression(
66-
keyPath: expression.lhs.keyPath,
67-
title: title,
65+
decoded(
66+
keyPath: expression.lhs,
6867
attribute: .init(operator: .equals, value: expression.rhs.value)
6968
)
7069
case let expression as PredicateExpressions.StringLocalizedStandardContains<KeyPathPredicateExpression, ValuePredicateExpression>:
71-
StringExpression(
72-
keyPath: expression.root.keyPath,
73-
title: title,
70+
decoded(
71+
keyPath: expression.root,
7472
attribute: .init(operator: .contains, value: expression.other.value)
7573
)
7674
case let expression as PredicateExpressions.SequenceStartsWith<KeyPathPredicateExpression, ValuePredicateExpression>:
77-
StringExpression(
78-
keyPath: expression.base.keyPath,
79-
title: title,
75+
decoded(
76+
keyPath: expression.base,
8077
attribute: .init(operator: .equals, value: expression.prefix.value)
8178
)
8279
default:

0 commit comments

Comments
 (0)