@@ -36,9 +36,8 @@ public final class URLQueryEncoder {
36
36
case custom( ( Date ) -> String )
37
37
}
38
38
39
- #warning("this should be private")
40
- public fileprivate( set) var codingPath : [ CodingKey ] = [ ]
41
- public fileprivate( set) var queryItems : [ URLQueryItem ] = [ ]
39
+ fileprivate( set) var codingPath : [ CodingKey ] = [ ]
40
+ public private( set) var queryItems : [ URLQueryItem ] = [ ]
42
41
43
42
public var items : [ ( String , String ? ) ] {
44
43
queryItems. map { ( $0. name, $0. value) }
@@ -62,14 +61,19 @@ public final class URLQueryEncoder {
62
61
63
62
public init ( ) { }
64
63
65
- #warning("make throwing?")
66
- #warning("simplify how configuration is passed")
67
64
public func encode( _ value: Encodable , explode: Bool ? = nil , delimeter: String ? = nil , isDeepObject: Bool ? = nil ) {
65
+ // Temporary override the settings to the duration of the call
68
66
_explode = explode ?? self . explode
69
67
_delimeter = delimeter ?? self . delimeter
70
68
_isDeepObject = isDeepObject ?? self . isDeepObject
69
+
71
70
let encoder = _URLQueryEncoder ( encoder: self , codingPath: codingPath)
72
- try ? value. encode ( to: encoder)
71
+ do {
72
+ try value. encode ( to: encoder)
73
+ } catch {
74
+ // Assume that conversion to String never fails
75
+ assertionFailure ( " URL encoding failed with an error: \( error) " )
76
+ }
73
77
}
74
78
}
75
79
@@ -151,7 +155,6 @@ private extension URLQueryEncoder {
151
155
}
152
156
153
157
func encodeEncodable< T: Encodable > ( _ value: T , forKey codingPath: [ CodingKey ] ) throws {
154
- self . codingPath = codingPath
155
158
switch value {
156
159
case let value as String : try encode ( value, forKey: codingPath)
157
160
case let value as Bool : try encode ( value, forKey: codingPath)
@@ -247,7 +250,8 @@ private struct KeyedContainer<Key: CodingKey>: KeyedEncodingContainerProtocol {
247
250
}
248
251
249
252
func nestedContainer< NestedKey> ( keyedBy keyType: NestedKey . Type , forKey key: Key ) -> KeyedEncodingContainer < NestedKey > where NestedKey : CodingKey {
250
- KeyedEncodingContainer ( KeyedContainer < NestedKey > ( encoder: encoder, codingPath: codingPath + [ key] ) )
253
+ assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
254
+ return KeyedEncodingContainer ( KeyedContainer < NestedKey > ( encoder: encoder, codingPath: codingPath + [ key] ) )
251
255
}
252
256
253
257
func nestedUnkeyedContainer( forKey key: Key ) -> UnkeyedEncodingContainer {
@@ -277,6 +281,16 @@ private final class UnkeyedContanier: UnkeyedEncodingContainer {
277
281
self . codingPath = codingPath
278
282
}
279
283
284
+ func encodeNil( ) throws {
285
+ try encoder. encodeNil ( forKey: codingPath)
286
+ count += 1
287
+ }
288
+
289
+ func encode< T> ( _ value: T ) throws where T: Encodable {
290
+ try encoder. encodeEncodable ( value, forKey: codingPath)
291
+ count += 1
292
+ }
293
+
280
294
func nestedContainer< NestedKey> ( keyedBy keyType: NestedKey . Type ) -> KeyedEncodingContainer < NestedKey > where NestedKey : CodingKey {
281
295
assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
282
296
return KeyedEncodingContainer ( KeyedContainer < NestedKey > ( encoder: encoder, codingPath: codingPath) )
@@ -291,16 +305,6 @@ private final class UnkeyedContanier: UnkeyedEncodingContainer {
291
305
assertionFailure ( " URLQueryEncoder doesn't support nested objects " )
292
306
return _URLQueryEncoder ( encoder: encoder, codingPath: codingPath)
293
307
}
294
-
295
- func encodeNil( ) throws {
296
- try encoder. encodeNil ( forKey: codingPath)
297
- count += 1
298
- }
299
-
300
- func encode< T> ( _ value: T ) throws where T: Encodable {
301
- try encoder. encodeEncodable ( value, forKey: codingPath)
302
- count += 1
303
- }
304
308
}
305
309
306
310
private struct SingleValueContanier : SingleValueEncodingContainer {
0 commit comments