Skip to content

Commit ff91e86

Browse files
committed
Hide Encoder conformance
1 parent 69a7588 commit ff91e86

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

Sources/URLQueryEncoder/URLQueryEncoder.swift

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
import Foundation
66

7-
#warning("should this be a struct?")
8-
public class URLQueryEncoder {
7+
public final class URLQueryEncoder {
98
public var explode = true
10-
private var _explode = true
11-
129
public var delimeter = ","
13-
private var _delimeter = ","
14-
1510
public var isDeepObject = false
11+
12+
private var _explode = true
13+
private var _delimeter = ","
1614
private var _isDeepObject = false
1715

1816
/// By default, `.iso8601`.
@@ -70,7 +68,8 @@ public class URLQueryEncoder {
7068
_explode = explode ?? self.explode
7169
_delimeter = delimeter ?? self.delimeter
7270
_isDeepObject = isDeepObject ?? self.isDeepObject
73-
try? value.encode(to: self)
71+
let encoder = _URLQueryEncoder(encoder: self, codingPath: codingPath)
72+
try? value.encode(to: encoder)
7473
}
7574

7675
public static func data(for queryItems: [URLQueryItem]) -> Data {
@@ -176,7 +175,7 @@ private extension URLQueryEncoder {
176175
case let value as Float: try encode(value, forKey: codingPath)
177176
case let value as Date: try encode(value, forKey: codingPath)
178177
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))
180179
}
181180
}
182181

@@ -217,20 +216,21 @@ private extension URLQueryEncoder {
217216
}
218217
}
219218

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 [:] }
223223

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))
226226
}
227227

228-
public func unkeyedContainer() -> UnkeyedEncodingContainer {
229-
return UnkeyedContanier(encoder: self, codingPath: codingPath)
228+
func unkeyedContainer() -> UnkeyedEncodingContainer {
229+
UnkeyedContanier(encoder: encoder, codingPath: encoder.codingPath)
230230
}
231231

232-
public func singleValueContainer() -> SingleValueEncodingContainer {
233-
return SingleValueContanier(encoder: self, codingPath: codingPath)
232+
func singleValueContainer() -> SingleValueEncodingContainer {
233+
SingleValueContanier(encoder: encoder, codingPath: encoder.codingPath)
234234
}
235235
}
236236

@@ -253,19 +253,22 @@ private struct KeyedContainer<Key: CodingKey>: KeyedEncodingContainerProtocol {
253253
}
254254

255255
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]))
257257
}
258258

259259
func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
260+
assertionFailure("URLQueryEncoder doesn't support nested objects")
260261
return UnkeyedContanier(encoder: encoder, codingPath: codingPath + [key])
261262
}
262263

263264
func superEncoder() -> Encoder {
264-
encoder
265+
assertionFailure("URLQueryEncoder doesn't support nested objects")
266+
return _URLQueryEncoder(encoder: encoder, codingPath: codingPath)
265267
}
266268

267269
func superEncoder(forKey key: Key) -> Encoder {
268-
encoder
270+
assertionFailure("URLQueryEncoder doesn't support nested objects")
271+
return _URLQueryEncoder(encoder: encoder, codingPath: codingPath + [key])
269272
}
270273
}
271274

@@ -281,15 +284,18 @@ private final class UnkeyedContanier: UnkeyedEncodingContainer {
281284
}
282285

283286
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))
285289
}
286290

287291
func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
288-
self
292+
assertionFailure("URLQueryEncoder doesn't support nested objects")
293+
return self
289294
}
290295

291296
func superEncoder() -> Encoder {
292-
encoder
297+
assertionFailure("URLQueryEncoder doesn't support nested objects")
298+
return _URLQueryEncoder(encoder: encoder, codingPath: codingPath)
293299
}
294300

295301
func encodeNil() throws {

0 commit comments

Comments
 (0)