/
URLEncodedFormEncoder.swift
393 lines (326 loc) · 15.8 KB
/
URLEncodedFormEncoder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if os(Linux)
@preconcurrency import Foundation
#else
import Foundation
#endif
/// The wrapper struct for encoding Codable classes to URL encoded form data
public struct URLEncodedFormEncoder: Sendable {
/// The strategy to use for encoding `Date` values.
public enum DateEncodingStrategy: Sendable {
/// Defer to `Date` for encoding. This is the default strategy.
case deferredToDate
/// Encode the `Date` as a UNIX timestamp from a JSON number.
case secondsSince1970
/// Encode the `Date` as UNIX millisecond timestamp from a JSON number.
case millisecondsSince1970
/// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
case iso8601
/// Encode the `Date` as a string parsed by the given formatter.
case formatted(DateFormatter)
/// Encode the `Date` as a custom value encoded by the given closure.
case custom(@Sendable (Date, Encoder) throws -> Void)
}
/// The strategy to use in Encoding dates. Defaults to `.deferredToDate`.
public var dateEncodingStrategy: DateEncodingStrategy
/// Contextual user-provided information for use during encoding.
public var userInfo: [CodingUserInfoKey: Sendable]
/// Options set on the top-level encoder to pass down the encoding hierarchy.
fileprivate struct _Options {
let dateEncodingStrategy: DateEncodingStrategy
let userInfo: [CodingUserInfoKey: Any]
}
/// The options set on the top-level encoder.
fileprivate var options: _Options {
return _Options(
dateEncodingStrategy: self.dateEncodingStrategy,
userInfo: self.userInfo
)
}
/// Create URLEncodedFormEncoder
/// - Parameters:
/// - dateEncodingStrategy: date encoding strategy
/// - userInfo: user info to supply to encoder
public init(
dateEncodingStrategy: URLEncodedFormEncoder.DateEncodingStrategy = .deferredToDate,
userInfo: [CodingUserInfoKey: Sendable] = [:],
additionalKeys: [String: String] = [:]
) {
self.dateEncodingStrategy = dateEncodingStrategy
self.userInfo = userInfo
}
/// Encode object into URL encoded form data
/// - Parameters:
/// - value: Value to encode
/// - Returns: URL encoded form data
public func encode(_ value: some Encodable) throws -> String {
let encoder = _URLEncodedFormEncoder(options: options)
try value.encode(to: encoder)
guard let result = encoder.result else {
throw EncodingError.invalidValue(value, .init(codingPath: [], debugDescription: "URLEncodedFormEncoder cannot be used to encode arrays"))
}
return result.description
}
}
/// Internal QueryEncoder class. Does all the heavy lifting
private class _URLEncodedFormEncoder: Encoder {
var codingPath: [CodingKey]
/// the encoder's storage
var storage: URLEncodedFormEncoderStorage
/// options
var options: URLEncodedFormEncoder._Options
/// resultant url encoded array
var result: URLEncodedFormNode?
/// Contextual user-provided information for use during encoding.
public var userInfo: [CodingUserInfoKey: Any] {
return self.options.userInfo
}
/// Initialization
/// - Parameters:
/// - options: options
/// - containerCodingMapType: Container encoding for the top level object
init(options: URLEncodedFormEncoder._Options) {
self.storage = URLEncodedFormEncoderStorage()
self.options = options
self.codingPath = []
self.result = nil
}
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key: CodingKey {
let keyedContainer = self.storage.pushKeyedContainer()
if self.result == nil {
self.result = .map(keyedContainer)
}
return KeyedEncodingContainer(KEC(referencing: self, container: keyedContainer))
}
struct KEC<Key: CodingKey>: KeyedEncodingContainerProtocol {
var codingPath: [CodingKey] { return self.encoder.codingPath }
let container: URLEncodedFormNode.Map
let encoder: _URLEncodedFormEncoder
/// Initialization
/// - Parameter referencing: encoder that created this
init(referencing: _URLEncodedFormEncoder, container: URLEncodedFormNode.Map) {
self.encoder = referencing
self.container = container
}
mutating func encode(_ value: URLEncodedFormNode, key: String) {
self.container.addChild(key: key, value: value)
}
mutating func encode(_ value: LosslessStringConvertible, key: String) {
self.encode(.leaf(.init(value)), key: key)
}
mutating func encodeNil(forKey key: Key) throws { self.encode("", key: key.stringValue) }
mutating func encode(_ value: Bool, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: String, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: Double, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: Float, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: Int, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: Int8, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: Int16, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: Int32, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: Int64, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: UInt, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: UInt8, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: UInt16, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: UInt32, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: UInt64, forKey key: Key) throws { self.encode(value, key: key.stringValue) }
mutating func encode(_ value: some Encodable, forKey key: Key) throws {
self.encoder.codingPath.append(key)
defer { self.encoder.codingPath.removeLast() }
let childContainer = try encoder.box(value)
self.container.addChild(key: key.stringValue, value: childContainer)
}
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey {
self.encoder.codingPath.append(key)
defer { self.encoder.codingPath.removeLast() }
let keyedContainer = URLEncodedFormNode.Map()
self.container.addChild(key: key.stringValue, value: .map(keyedContainer))
let kec = KEC<NestedKey>(referencing: self.encoder, container: keyedContainer)
return KeyedEncodingContainer(kec)
}
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
self.encoder.codingPath.append(key)
defer { self.encoder.codingPath.removeLast() }
let unkeyedContainer = URLEncodedFormNode.Array()
self.container.addChild(key: key.stringValue, value: .array(unkeyedContainer))
return UKEC(referencing: self.encoder, container: unkeyedContainer)
}
mutating func superEncoder() -> Encoder {
return self.encoder
}
mutating func superEncoder(forKey key: Key) -> Encoder {
return self.encoder
}
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
let container = self.storage.pushUnkeyedContainer()
return UKEC(referencing: self, container: container)
}
struct UKEC: UnkeyedEncodingContainer {
var codingPath: [CodingKey] { return self.encoder.codingPath }
let container: URLEncodedFormNode.Array
let encoder: _URLEncodedFormEncoder
var count: Int
init(referencing: _URLEncodedFormEncoder, container: URLEncodedFormNode.Array) {
self.encoder = referencing
self.container = container
self.count = 0
}
mutating func encodeResult(_ value: URLEncodedFormNode) {
self.count += 1
self.container.addChild(value: value)
}
mutating func encodeResult(_ value: LosslessStringConvertible) {
self.encodeResult(.leaf(.init(value)))
}
mutating func encodeNil() throws { self.encodeResult("") }
mutating func encode(_ value: Bool) throws { self.encodeResult(value) }
mutating func encode(_ value: String) throws { self.encodeResult(value) }
mutating func encode(_ value: Double) throws { self.encodeResult(value) }
mutating func encode(_ value: Float) throws { self.encodeResult(value) }
mutating func encode(_ value: Int) throws { self.encodeResult(value) }
mutating func encode(_ value: Int8) throws { self.encodeResult(value) }
mutating func encode(_ value: Int16) throws { self.encodeResult(value) }
mutating func encode(_ value: Int32) throws { self.encodeResult(value) }
mutating func encode(_ value: Int64) throws { self.encodeResult(value) }
mutating func encode(_ value: UInt) throws { self.encodeResult(value) }
mutating func encode(_ value: UInt8) throws { self.encodeResult(value) }
mutating func encode(_ value: UInt16) throws { self.encodeResult(value) }
mutating func encode(_ value: UInt32) throws { self.encodeResult(value) }
mutating func encode(_ value: UInt64) throws { self.encodeResult(value) }
mutating func encode(_ value: some Encodable) throws {
self.count += 1
self.encoder.codingPath.append(URLEncodedForm.Key(index: self.count))
defer { self.encoder.codingPath.removeLast() }
let childContainer = try encoder.box(value)
self.container.addChild(value: childContainer)
}
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey {
self.count += 1
self.encoder.codingPath.append(URLEncodedForm.Key(index: self.count))
defer { self.encoder.codingPath.removeLast() }
let keyedContainer = URLEncodedFormNode.Map()
self.container.addChild(value: .map(keyedContainer))
let kec = KEC<NestedKey>(referencing: self.encoder, container: keyedContainer)
return KeyedEncodingContainer(kec)
}
mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
self.count += 1
let unkeyedContainer = URLEncodedFormNode.Array()
self.container.addChild(value: .array(unkeyedContainer))
return UKEC(referencing: self.encoder, container: unkeyedContainer)
}
mutating func superEncoder() -> Encoder {
return self.encoder
}
}
}
extension _URLEncodedFormEncoder: SingleValueEncodingContainer {
func encodeResult(_ value: URLEncodedFormNode) {
self.storage.push(container: value)
}
func encodeResult(_ value: LosslessStringConvertible) {
self.storage.push(container: .leaf(.init(value)))
}
func encodeNil() throws {
self.encodeResult("")
}
func encode(_ value: Bool) throws { self.encodeResult(value) }
func encode(_ value: String) throws { self.encodeResult(value) }
func encode(_ value: Double) throws { self.encodeResult(value) }
func encode(_ value: Float) throws { self.encodeResult(value) }
func encode(_ value: Int) throws { self.encodeResult(value) }
func encode(_ value: Int8) throws { self.encodeResult(value) }
func encode(_ value: Int16) throws { self.encodeResult(value) }
func encode(_ value: Int32) throws { self.encodeResult(value) }
func encode(_ value: Int64) throws { self.encodeResult(value) }
func encode(_ value: UInt) throws { self.encodeResult(value) }
func encode(_ value: UInt8) throws { self.encodeResult(value) }
func encode(_ value: UInt16) throws { self.encodeResult(value) }
func encode(_ value: UInt32) throws { self.encodeResult(value) }
func encode(_ value: UInt64) throws { self.encodeResult(value) }
func encode(_ value: some Encodable) throws {
try value.encode(to: self)
}
func singleValueContainer() -> SingleValueEncodingContainer {
return self
}
}
extension _URLEncodedFormEncoder {
func box(_ date: Date) throws -> URLEncodedFormNode {
switch self.options.dateEncodingStrategy {
case .deferredToDate:
try date.encode(to: self)
case .millisecondsSince1970:
try self.encode(Double(date.timeIntervalSince1970 * 1000).description)
case .secondsSince1970:
try self.encode(Double(date.timeIntervalSince1970).description)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
try encode(URLEncodedForm.iso8601Formatter.string(from: date))
} else {
preconditionFailure("ISO8601DateFormatter is unavailable on this platform")
}
case .formatted(let formatter):
try self.encode(formatter.string(from: date))
case .custom(let closure):
try closure(date, self)
}
return self.storage.popContainer()
}
func box(_ data: Data) throws -> URLEncodedFormNode {
try self.encode(data.base64EncodedString())
return self.storage.popContainer()
}
func box(_ value: Encodable) throws -> URLEncodedFormNode {
let type = Swift.type(of: value)
if type == Data.self {
return try self.box(value as! Data)
} else if type == Date.self {
return try self.box(value as! Date)
} else {
try value.encode(to: self)
return self.storage.popContainer()
}
}
}
/// storage for Query Encoder. Stores a stack of QueryEncoder containers, plus leaf objects
private struct URLEncodedFormEncoderStorage {
/// the container stack
private var containers: [URLEncodedFormNode] = []
/// initializes self with no containers
init() {
// containers.append(.map(.init()))
}
/// push a new container onto the storage
mutating func pushKeyedContainer() -> URLEncodedFormNode.Map {
let map = URLEncodedFormNode.Map()
self.containers.append(.map(map))
return map
}
/// push a new container onto the storage
mutating func pushUnkeyedContainer() -> URLEncodedFormNode.Array {
let array = URLEncodedFormNode.Array()
self.containers.append(.array(array))
return array
}
mutating func push(container: URLEncodedFormNode) {
self.containers.append(container)
}
/// pop a container from the storage
@discardableResult mutating func popContainer() -> URLEncodedFormNode {
return self.containers.removeLast()
}
}