Skip to content

Commit 3a4ad7f

Browse files
committed
Cleanup
1 parent 7eccb7a commit 3a4ad7f

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Sources/URLQueryEncoder/URLQueryEncoder.swift

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ public final class URLQueryEncoder {
3636
case custom((Date) -> String)
3737
}
3838

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] = []
4241

4342
public var items: [(String, String?)] {
4443
queryItems.map { ($0.name, $0.value) }
@@ -62,14 +61,19 @@ public final class URLQueryEncoder {
6261

6362
public init() {}
6463

65-
#warning("make throwing?")
66-
#warning("simplify how configuration is passed")
6764
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
6866
_explode = explode ?? self.explode
6967
_delimeter = delimeter ?? self.delimeter
7068
_isDeepObject = isDeepObject ?? self.isDeepObject
69+
7170
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+
}
7377
}
7478
}
7579

@@ -151,7 +155,6 @@ private extension URLQueryEncoder {
151155
}
152156

153157
func encodeEncodable<T: Encodable>(_ value: T, forKey codingPath: [CodingKey]) throws {
154-
self.codingPath = codingPath
155158
switch value {
156159
case let value as String: try encode(value, forKey: codingPath)
157160
case let value as Bool: try encode(value, forKey: codingPath)
@@ -247,7 +250,8 @@ private struct KeyedContainer<Key: CodingKey>: KeyedEncodingContainerProtocol {
247250
}
248251

249252
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]))
251255
}
252256

253257
func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
@@ -277,6 +281,16 @@ private final class UnkeyedContanier: UnkeyedEncodingContainer {
277281
self.codingPath = codingPath
278282
}
279283

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+
280294
func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
281295
assertionFailure("URLQueryEncoder doesn't support nested objects")
282296
return KeyedEncodingContainer(KeyedContainer<NestedKey>(encoder: encoder, codingPath: codingPath))
@@ -291,16 +305,6 @@ private final class UnkeyedContanier: UnkeyedEncodingContainer {
291305
assertionFailure("URLQueryEncoder doesn't support nested objects")
292306
return _URLQueryEncoder(encoder: encoder, codingPath: codingPath)
293307
}
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-
}
304308
}
305309

306310
private struct SingleValueContanier: SingleValueEncodingContainer {

0 commit comments

Comments
 (0)