Skip to content

Commit 8d6667f

Browse files
committed
Fix Swift query values
1 parent 5f7b1b1 commit 8d6667f

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

templates/swift/Sources/Query.swift.twig

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ enum QueryValue: Codable {
55
case int(Int)
66
case double(Double)
77
case bool(Bool)
8-
case query(Query)
98

109
init(from decoder: Decoder) throws {
1110
let container = try decoder.singleValueContainer()
@@ -18,8 +17,6 @@ enum QueryValue: Codable {
1817
self = .double(doubleValue)
1918
} else if let boolValue = try? container.decode(Bool.self) {
2019
self = .bool(boolValue)
21-
} else if let queryValue = try? container.decode(Query.self) {
22-
self = .query(queryValue)
2320
} else {
2421
throw DecodingError.dataCorruptedError(in: container, debugDescription: "QueryValue cannot be decoded")
2522
}
@@ -36,8 +33,6 @@ enum QueryValue: Codable {
3633
try container.encode(value)
3734
case .bool(let value):
3835
try container.encode(value)
39-
case .query(let value):
40-
try container.encode(value)
4136
}
4237
}
4338
}
@@ -66,15 +61,13 @@ public struct Query : Codable, CustomStringConvertible {
6661
case let valueArray as [QueryValue]:
6762
return valueArray
6863
case let stringArray as [String]:
69-
return stringArray.map { QueryValue.string($0) }
64+
return stringArray.map { .string($0) }
7065
case let intArray as [Int]:
71-
return intArray.map { QueryValue.int($0) }
66+
return intArray.map { .int($0) }
7267
case let doubleArray as [Double]:
73-
return doubleArray.map { QueryValue.double($0) }
68+
return doubleArray.map { .double($0) }
7469
case let boolArray as [Bool]:
75-
return boolArray.map { QueryValue.bool($0) }
76-
case let queryArray as [Query]:
77-
return queryArray.map { QueryValue.query($0) }
70+
return boolArray.map { .bool($0) }
7871
case let stringValue as String:
7972
return [.string(stringValue)]
8073
case let intValue as Int:
@@ -83,13 +76,11 @@ public struct Query : Codable, CustomStringConvertible {
8376
return [.double(doubleValue)]
8477
case let boolValue as Bool:
8578
return [.bool(boolValue)]
86-
case let queryValue as Query:
87-
return [.query(queryValue)]
8879
default:
8980
return nil
9081
}
9182
}
92-
83+
9384
enum CodingKeys: String, CodingKey {
9485
case method
9586
case attribute
@@ -121,47 +112,47 @@ public struct Query : Codable, CustomStringConvertible {
121112
return Query(
122113
method: "equal",
123114
attribute: attribute,
124-
values: [value]
115+
values: Query.parseValue(value)
125116
).description
126117
}
127118

128119
public static func notEqual(_ attribute: String, value: Any) -> String {
129120
return Query(
130121
method: "notEqual",
131122
attribute: attribute,
132-
values: [value]
123+
values: Query.parseValue(value)
133124
).description
134125
}
135126

136127
public static func lessThan(_ attribute: String, value: Any) -> String {
137128
return Query(
138129
method: "lessThan",
139130
attribute: attribute,
140-
values: [value]
131+
values: Query.parseValue(value)
141132
).description
142133
}
143134

144135
public static func lessThanEqual(attribute: String, value: Any) -> String {
145136
return Query(
146137
method: "lessThanEqual",
147138
attribute: attribute,
148-
values: [value]
139+
values: Query.parseValue(value)
149140
).description
150141
}
151142

152143
public static func greaterThan(_ attribute: String, value: Any) -> String {
153144
return Query(
154145
method: "greaterThan",
155146
attribute: attribute,
156-
values: [value]
147+
values: Query.parseValue(value)
157148
).description
158149
}
159150

160151
public static func greaterThanEqual(_ attribute: String, value: Any) -> String {
161152
return Query(
162153
method: "greaterThanEqual",
163154
attribute: attribute,
164-
values: [value]
155+
values: Query.parseValue(value)
165156
).description
166157
}
167158

@@ -280,7 +271,7 @@ public struct Query : Codable, CustomStringConvertible {
280271
return Query(
281272
method: "contains",
282273
attribute: attribute,
283-
values: value
274+
values: Query.parseValue(value)
284275
).description
285276
}
286277

@@ -313,4 +304,12 @@ public struct Query : Codable, CustomStringConvertible {
313304
values: decodedQueries
314305
).description
315306
}
316-
}
307+
308+
private static func parseValue(_ value: Any) -> [Any] {
309+
if let value = value as? [Any] {
310+
return value
311+
} else {
312+
return [value]
313+
}
314+
}
315+
}

0 commit comments

Comments
 (0)