4
4
5
5
import Foundation
6
6
7
- #warning("should this be a struct?")
8
- public class URLQueryEncoder {
7
+ public final class URLQueryEncoder {
9
8
public var explode = true
10
- private var _explode = true
11
-
12
9
public var delimeter = " , "
13
- private var _delimeter = " , "
14
-
15
10
public var isDeepObject = false
11
+
12
+ private var _explode = true
13
+ private var _delimeter = " , "
16
14
private var _isDeepObject = false
17
15
18
16
/// By default, `.iso8601`.
@@ -70,7 +68,8 @@ public class URLQueryEncoder {
70
68
_explode = explode ?? self . explode
71
69
_delimeter = delimeter ?? self . delimeter
72
70
_isDeepObject = isDeepObject ?? self . isDeepObject
73
- try ? value. encode ( to: self )
71
+ let encoder = _URLQueryEncoder ( encoder: self , codingPath: codingPath)
72
+ try ? value. encode ( to: encoder)
74
73
}
75
74
76
75
public static func data( for queryItems: [ URLQueryItem ] ) -> Data {
@@ -176,7 +175,7 @@ private extension URLQueryEncoder {
176
175
case let value as Float : try encode ( value, forKey: codingPath)
177
176
case let value as Date : try encode ( value, forKey: codingPath)
178
177
case let value as URL : try encode ( value, forKey: codingPath)
179
- case let value: try value. encode ( to: self )
178
+ case let value: try value. encode ( to: _URLQueryEncoder ( encoder : self , codingPath : codingPath ) )
180
179
}
181
180
}
182
181
@@ -217,20 +216,21 @@ private extension URLQueryEncoder {
217
216
}
218
217
}
219
218
220
- #warning("TODO: remove from extension")
221
- extension URLQueryEncoder : Encoder {
222
- public var userInfo : [ CodingUserInfoKey : Any ] { return [ : ] }
219
+ private struct _URLQueryEncoder : Encoder {
220
+ let encoder : URLQueryEncoder
221
+ var codingPath : [ CodingKey ]
222
+ var userInfo : [ CodingUserInfoKey : Any ] { return [ : ] }
223
223
224
- public func container< Key> ( keyedBy type: Key . Type ) -> KeyedEncodingContainer < Key > where Key : CodingKey {
225
- return KeyedEncodingContainer ( KeyedContainer < Key > ( encoder: self , codingPath: codingPath) )
224
+ func container< Key> ( keyedBy type: Key . Type ) -> KeyedEncodingContainer < Key > where Key : CodingKey {
225
+ KeyedEncodingContainer ( KeyedContainer < Key > ( encoder: encoder , codingPath: encoder . codingPath) )
226
226
}
227
227
228
- public func unkeyedContainer( ) -> UnkeyedEncodingContainer {
229
- return UnkeyedContanier ( encoder: self , codingPath: codingPath)
228
+ func unkeyedContainer( ) -> UnkeyedEncodingContainer {
229
+ UnkeyedContanier ( encoder: encoder , codingPath: encoder . codingPath)
230
230
}
231
231
232
- public func singleValueContainer( ) -> SingleValueEncodingContainer {
233
- return SingleValueContanier ( encoder: self , codingPath: codingPath)
232
+ func singleValueContainer( ) -> SingleValueEncodingContainer {
233
+ SingleValueContanier ( encoder: encoder , codingPath: encoder . codingPath)
234
234
}
235
235
}
236
236
@@ -253,19 +253,22 @@ private struct KeyedContainer<Key: CodingKey>: KeyedEncodingContainerProtocol {
253
253
}
254
254
255
255
func nestedContainer< NestedKey> ( keyedBy keyType: NestedKey . Type , forKey key: Key ) -> KeyedEncodingContainer < NestedKey > where NestedKey : CodingKey {
256
- return KeyedEncodingContainer ( KeyedContainer < NestedKey > ( encoder: encoder, codingPath: codingPath + [ key] ) )
256
+ KeyedEncodingContainer ( KeyedContainer < NestedKey > ( encoder: encoder, codingPath: codingPath + [ key] ) )
257
257
}
258
258
259
259
func nestedUnkeyedContainer( forKey key: Key ) -> UnkeyedEncodingContainer {
260
+ assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
260
261
return UnkeyedContanier ( encoder: encoder, codingPath: codingPath + [ key] )
261
262
}
262
263
263
264
func superEncoder( ) -> Encoder {
264
- encoder
265
+ assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
266
+ return _URLQueryEncoder ( encoder: encoder, codingPath: codingPath)
265
267
}
266
268
267
269
func superEncoder( forKey key: Key ) -> Encoder {
268
- encoder
270
+ assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
271
+ return _URLQueryEncoder ( encoder: encoder, codingPath: codingPath + [ key] )
269
272
}
270
273
}
271
274
@@ -281,15 +284,18 @@ private final class UnkeyedContanier: UnkeyedEncodingContainer {
281
284
}
282
285
283
286
func nestedContainer< NestedKey> ( keyedBy keyType: NestedKey . Type ) -> KeyedEncodingContainer < NestedKey > where NestedKey : CodingKey {
284
- KeyedEncodingContainer ( KeyedContainer < NestedKey > ( encoder: encoder, codingPath: codingPath) )
287
+ assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
288
+ return KeyedEncodingContainer ( KeyedContainer < NestedKey > ( encoder: encoder, codingPath: codingPath) )
285
289
}
286
290
287
291
func nestedUnkeyedContainer( ) -> UnkeyedEncodingContainer {
288
- self
292
+ assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
293
+ return self
289
294
}
290
295
291
296
func superEncoder( ) -> Encoder {
292
- encoder
297
+ assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
298
+ return _URLQueryEncoder ( encoder: encoder, codingPath: codingPath)
293
299
}
294
300
295
301
func encodeNil( ) throws {
0 commit comments