12
12
13
13
#if FOUNDATION_FRAMEWORK
14
14
15
+ // Initial API constrained to Output == Bool
16
+
15
17
@available ( FoundationPredicate 0 . 1 , * )
16
18
extension KeyedEncodingContainer {
17
19
public mutating func encodePredicateExpression< T: PredicateExpression & Encodable , each Input > ( _ expression: T , forKey key: Self . Key , variable: repeat PredicateExpressions . Variable < each Input > , predicateConfiguration: PredicateCodableConfiguration ) throws where T. Output == Bool {
@@ -25,11 +27,19 @@ extension KeyedEncodingContainer {
25
27
}
26
28
}
27
29
30
+ extension PredicateExpression {
31
+ fileprivate static var outputType : Any . Type {
32
+ Output . self
33
+ }
34
+ }
35
+
28
36
@available ( FoundationPredicate 0 . 1 , * )
29
37
extension KeyedDecodingContainer {
38
+ @_optimize ( none) // Work around swift optimizer crash (rdar://124533887)
30
39
public mutating func decodePredicateExpression< each Input > ( forKey key: Self . Key , input: repeat ( each Input ) . Type, predicateConfiguration: PredicateCodableConfiguration ) throws -> ( expression: any PredicateExpression < Bool > , variable: ( repeat PredicateExpressions . Variable < each Input > ) ) {
31
40
var container = try self . nestedContainer ( keyedBy: PredicateExpressionCodingKeys . self, forKey: key)
32
- return try container. _decode ( input: repeat each input, predicateConfiguration: predicateConfiguration)
41
+ let ( expr, variable) = try container. _decode ( input: repeat each input, output: Bool . self, predicateConfiguration: predicateConfiguration)
42
+ return ( expr, variable)
33
43
}
34
44
35
45
public mutating func decodePredicateExpressionIfPresent< each Input > ( forKey key: Self . Key , input: repeat ( each Input ) . Type, predicateConfiguration: PredicateCodableConfiguration ) throws -> ( expression: any PredicateExpression < Bool > , variable: ( repeat PredicateExpressions . Variable < each Input > ) ) ? {
@@ -56,9 +66,14 @@ extension UnkeyedEncodingContainer {
56
66
57
67
@available ( FoundationPredicate 0 . 1 , * )
58
68
extension UnkeyedDecodingContainer {
69
+ @_optimize ( none) // Work around swift optimizer crash (rdar://124533887)
59
70
public mutating func decodePredicateExpression< each Input > ( input: repeat ( each Input ) . Type, predicateConfiguration: PredicateCodableConfiguration ) throws -> ( expression: any PredicateExpression < Bool > , variable: ( repeat PredicateExpressions . Variable < each Input > ) ) {
60
71
var container = try self . nestedContainer ( keyedBy: PredicateExpressionCodingKeys . self)
61
- return try container. _decode ( input: repeat each input, predicateConfiguration: predicateConfiguration)
72
+ let ( expr, variable) = try container. _decode ( input: repeat each input, output: Bool . self, predicateConfiguration: predicateConfiguration)
73
+ guard let casted = expr as? any PredicateExpression < Bool > else {
74
+ throw DecodingError . dataCorruptedError ( in: self , debugDescription: " This expression has an unsupported output type of \( _typeName ( type ( of: expr) . outputType) ) (expected Bool) " )
75
+ }
76
+ return ( casted, variable)
62
77
}
63
78
64
79
public mutating func decodePredicateExpressionIfPresent< each Input > ( input: repeat ( each Input ) . Type, predicateConfiguration: PredicateCodableConfiguration ) throws -> ( expression: any PredicateExpression < Bool > , variable: ( repeat PredicateExpressions . Variable < each Input > ) ) ? {
@@ -70,4 +85,68 @@ extension UnkeyedDecodingContainer {
70
85
}
71
86
}
72
87
88
+ // Added API without Output Constraint
89
+
90
+ @available ( FoundationPredicate 0 . 4 , * )
91
+ extension KeyedEncodingContainer {
92
+ @_disfavoredOverload
93
+ public mutating func encodePredicateExpression< T: PredicateExpression & Encodable , each Input > ( _ expression: T , forKey key: Self . Key , variable: repeat PredicateExpressions . Variable < each Input > , predicateConfiguration: PredicateCodableConfiguration ) throws {
94
+ var container = self . nestedContainer ( keyedBy: PredicateExpressionCodingKeys . self, forKey: key)
95
+ try container. _encode ( expression, variable: repeat each variable, predicateConfiguration: predicateConfiguration)
96
+ }
97
+
98
+ @_disfavoredOverload
99
+ public mutating func encodePredicateExpressionIfPresent< T: PredicateExpression & Encodable , each Input > ( _ expression: T ? , forKey key: Self . Key , variable: repeat PredicateExpressions . Variable < each Input > , predicateConfiguration: PredicateCodableConfiguration ) throws {
100
+ guard let expression else { return }
101
+ try self . encodePredicateExpression ( expression, forKey: key, variable: repeat each variable, predicateConfiguration: predicateConfiguration)
102
+ }
103
+ }
104
+
105
+ @available ( FoundationPredicate 0 . 4 , * )
106
+ extension KeyedDecodingContainer {
107
+ public mutating func decodePredicateExpression< each Input , Output> ( forKey key: Self . Key , input: repeat ( each Input ) . Type, output: Output . Type , predicateConfiguration: PredicateCodableConfiguration ) throws -> ( expression: any PredicateExpression < Output > , variable: ( repeat PredicateExpressions . Variable < each Input > ) ) {
108
+ var container = try self . nestedContainer ( keyedBy: PredicateExpressionCodingKeys . self, forKey: key)
109
+ return try container. _decode ( input: repeat each input, output: output, predicateConfiguration: predicateConfiguration)
110
+ }
111
+
112
+ public mutating func decodePredicateExpressionIfPresent< each Input , Output> ( forKey key: Self . Key , input: repeat ( each Input ) . Type, output: Output . Type , predicateConfiguration: PredicateCodableConfiguration ) throws -> ( expression: any PredicateExpression < Output > , variable: ( repeat PredicateExpressions . Variable < each Input > ) ) ? {
113
+ guard self . contains ( key) else { return nil }
114
+ return try self . decodePredicateExpression ( forKey: key, input: repeat each input, output: output, predicateConfiguration: predicateConfiguration)
115
+ }
116
+ }
117
+
118
+ @available ( FoundationPredicate 0 . 4 , * )
119
+ extension UnkeyedEncodingContainer {
120
+ @_disfavoredOverload
121
+ public mutating func encodePredicateExpression< T: PredicateExpression & Encodable , each Input > ( _ expression: T , variable: repeat PredicateExpressions . Variable < each Input > , predicateConfiguration: PredicateCodableConfiguration ) throws {
122
+ var container = self . nestedContainer ( keyedBy: PredicateExpressionCodingKeys . self)
123
+ try container. _encode ( expression, variable: repeat each variable, predicateConfiguration: predicateConfiguration)
124
+ }
125
+
126
+ @_disfavoredOverload
127
+ public mutating func encodePredicateExpressionIfPresent< T: PredicateExpression & Encodable , each Input > ( _ expression: T ? , variable: repeat PredicateExpressions . Variable < each Input > , predicateConfiguration: PredicateCodableConfiguration ) throws {
128
+ guard let expression else {
129
+ try self . encodeNil ( )
130
+ return
131
+ }
132
+ try self . encodePredicateExpression ( expression, variable: repeat each variable, predicateConfiguration: predicateConfiguration)
133
+ }
134
+ }
135
+
136
+ @available ( FoundationPredicate 0 . 4 , * )
137
+ extension UnkeyedDecodingContainer {
138
+ public mutating func decodePredicateExpression< each Input , Output> ( input: repeat ( each Input ) . Type, output: Output . Type , predicateConfiguration: PredicateCodableConfiguration ) throws -> ( expression: any PredicateExpression < Output > , variable: ( repeat PredicateExpressions . Variable < each Input > ) ) {
139
+ var container = try self . nestedContainer ( keyedBy: PredicateExpressionCodingKeys . self)
140
+ return try container. _decode ( input: repeat each input, output: output, predicateConfiguration: predicateConfiguration)
141
+ }
142
+
143
+ public mutating func decodePredicateExpressionIfPresent< each Input , Output> ( input: repeat ( each Input ) . Type, output: Output . Type , predicateConfiguration: PredicateCodableConfiguration ) throws -> ( expression: any PredicateExpression < Output > , variable: ( repeat PredicateExpressions . Variable < each Input > ) ) ? {
144
+ if try self . decodeNil ( ) {
145
+ return nil
146
+ } else {
147
+ return try self . decodePredicateExpression ( input: repeat each input, output: output, predicateConfiguration: predicateConfiguration)
148
+ }
149
+ }
150
+ }
151
+
73
152
#endif // FOUNDATION_FRAMEWORK
0 commit comments