-
Notifications
You must be signed in to change notification settings - Fork 33
/
URISerializer.swift
259 lines (234 loc) · 10.6 KB
/
URISerializer.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
/// A type that serializes a `URIEncodedNode` to a URI-encoded string.
struct URISerializer {
/// The configuration instructing the serializer how to format the raw
/// string.
private let configuration: URICoderConfiguration
/// The underlying raw string storage.
private var data: String
/// Creates a new serializer.
/// - Parameter configuration: The configuration instructing the serializer
/// how to format the raw string.
init(configuration: URICoderConfiguration) {
self.configuration = configuration
self.data = ""
}
/// Serializes the provided node into the underlying string.
/// - Parameters:
/// - value: The node to serialize.
/// - key: The key to serialize the node under (details depend on the
/// style and explode parameters in the configuration).
/// - Returns: The URI-encoded data for the provided node.
/// - Throws: An error if serialization of the node fails.
mutating func serializeNode(_ value: URIEncodedNode, forKey key: String) throws -> String {
defer { data.removeAll(keepingCapacity: true) }
try serializeTopLevelNode(value, forKey: key)
return data
}
}
extension CharacterSet {
/// A character set of unreserved symbols only from RFC 6570 (excludes
/// alphanumeric characters).
fileprivate static let unreservedSymbols: CharacterSet = .init(charactersIn: "-._~")
/// A character set of unreserved characters from RFC 6570.
fileprivate static let unreserved: CharacterSet = .alphanumerics.union(unreservedSymbols)
/// A character set with only the space character.
fileprivate static let space: CharacterSet = .init(charactersIn: " ")
/// A character set of unreserved characters and a space.
fileprivate static let unreservedAndSpace: CharacterSet = .unreserved.union(space)
}
extension URISerializer {
/// A serializer error.
private enum SerializationError: Swift.Error {
/// Nested containers are not supported.
case nestedContainersNotSupported
}
/// Computes an escaped version of the provided string.
/// - Parameter unsafeString: A string that needs escaping.
/// - Returns: The provided string with percent-escaping applied.
private func computeSafeString(_ unsafeString: String) -> String {
// The space character needs to be encoded based on the config,
// so first allow it to be unescaped, and then we'll do a second
// pass and only encode the space based on the config.
let partiallyEncoded = unsafeString.addingPercentEncoding(withAllowedCharacters: .unreservedAndSpace) ?? ""
let fullyEncoded = partiallyEncoded.replacingOccurrences(
of: " ",
with: configuration.spaceEscapingCharacter.rawValue
)
return fullyEncoded
}
/// Provides a raw string value for the provided key.
/// - Parameter key: The key to stringify.
/// - Returns: The escaped version of the provided key.
/// - Throws: An error if the key cannot be converted to an escaped string.
private func stringifiedKey(_ key: String) throws -> String {
// The root key is handled separately.
guard !key.isEmpty else { return "" }
let safeTopLevelKey = computeSafeString(key)
return safeTopLevelKey
}
/// Serializes the provided value into the underlying string.
/// - Parameters:
/// - value: The value to serialize.
/// - key: The key to serialize the value under (details depend on the
/// style and explode parameters in the configuration).
/// - Throws: An error if serialization of the value fails.
private mutating func serializeTopLevelNode(_ value: URIEncodedNode, forKey key: String) throws {
func unwrapPrimitiveValue(_ node: URIEncodedNode) throws -> URIEncodedNode.Primitive {
guard case let .primitive(primitive) = node else { throw SerializationError.nestedContainersNotSupported }
return primitive
}
switch value {
case .unset:
// Nothing to serialize.
break
case .primitive(let primitive):
let keyAndValueSeparator: String?
switch configuration.style {
case .form: keyAndValueSeparator = "="
case .simple: keyAndValueSeparator = nil
}
try serializePrimitiveKeyValuePair(primitive, forKey: key, separator: keyAndValueSeparator)
case .array(let array): try serializeArray(array.map(unwrapPrimitiveValue), forKey: key)
case .dictionary(let dictionary):
try serializeDictionary(dictionary.mapValues(unwrapPrimitiveValue), forKey: key)
}
}
/// Serializes the provided value into the underlying string.
/// - Parameter value: The primitive value to serialize.
/// - Throws: An error if serialization of the primitive value fails.
private mutating func serializePrimitiveValue(_ value: URIEncodedNode.Primitive) throws {
let stringValue: String
switch value {
case .bool(let bool): stringValue = bool.description
case .string(let string): stringValue = computeSafeString(string)
case .integer(let int): stringValue = int.description
case .double(let double): stringValue = double.description
case .date(let date): stringValue = try computeSafeString(configuration.dateTranscoder.encode(date))
}
data.append(stringValue)
}
/// Serializes the provided key-value pair into the underlying string.
/// - Parameters:
/// - value: The value to serialize.
/// - key: The key to serialize the value under (details depend on the
/// style and explode parameters in the configuration).
/// - separator: The separator to use, if nil, the key is not serialized,
/// only the value.
/// - Throws: An error if serialization of the key-value pair fails.
private mutating func serializePrimitiveKeyValuePair(
_ value: URIEncodedNode.Primitive,
forKey key: String,
separator: String?
) throws {
if let separator {
data.append(try stringifiedKey(key))
data.append(separator)
}
try serializePrimitiveValue(value)
}
/// Serializes the provided array into the underlying string.
/// - Parameters:
/// - array: The value to serialize.
/// - key: The key to serialize the value under (details depend on the
/// style and explode parameters in the configuration).
/// - Throws: An error if serialization of the array fails.
private mutating func serializeArray(_ array: [URIEncodedNode.Primitive], forKey key: String) throws {
guard !array.isEmpty else { return }
let keyAndValueSeparator: String?
let pairSeparator: String
switch (configuration.style, configuration.explode) {
case (.form, true):
keyAndValueSeparator = "="
pairSeparator = "&"
case (.form, false):
keyAndValueSeparator = nil
pairSeparator = ","
case (.simple, _):
keyAndValueSeparator = nil
pairSeparator = ","
}
func serializeNext(_ element: URIEncodedNode.Primitive) throws {
if let keyAndValueSeparator {
try serializePrimitiveKeyValuePair(element, forKey: key, separator: keyAndValueSeparator)
} else {
try serializePrimitiveValue(element)
}
}
if let containerKeyAndValue = configuration.containerKeyAndValueSeparator {
data.append(try stringifiedKey(key))
data.append(containerKeyAndValue)
}
for element in array.dropLast() {
try serializeNext(element)
data.append(pairSeparator)
}
if let element = array.last { try serializeNext(element) }
}
/// Serializes the provided dictionary into the underlying string.
/// - Parameters:
/// - dictionary: The value to serialize.
/// - key: The key to serialize the value under (details depend on the
/// style and explode parameters in the configuration).
/// - Throws: An error if serialization of the dictionary fails.
private mutating func serializeDictionary(_ dictionary: [String: URIEncodedNode.Primitive], forKey key: String)
throws
{
guard !dictionary.isEmpty else { return }
let sortedDictionary = dictionary.sorted { a, b in
a.key.localizedCaseInsensitiveCompare(b.key) == .orderedAscending
}
let keyAndValueSeparator: String
let pairSeparator: String
switch (configuration.style, configuration.explode) {
case (.form, true):
keyAndValueSeparator = "="
pairSeparator = "&"
case (.form, false):
keyAndValueSeparator = ","
pairSeparator = ","
case (.simple, true):
keyAndValueSeparator = "="
pairSeparator = ","
case (.simple, false):
keyAndValueSeparator = ","
pairSeparator = ","
}
func serializeNext(_ element: URIEncodedNode.Primitive, forKey elementKey: String) throws {
try serializePrimitiveKeyValuePair(element, forKey: elementKey, separator: keyAndValueSeparator)
}
if let containerKeyAndValue = configuration.containerKeyAndValueSeparator {
data.append(try stringifiedKey(key))
data.append(containerKeyAndValue)
}
for (elementKey, element) in sortedDictionary.dropLast() {
try serializeNext(element, forKey: elementKey)
data.append(pairSeparator)
}
if let (elementKey, element) = sortedDictionary.last { try serializeNext(element, forKey: elementKey) }
}
}
extension URICoderConfiguration {
/// Returns the separator of a key and value in a pair for
/// the configuration. Can be nil, in which case no key should be
/// serialized, only the value.
fileprivate var containerKeyAndValueSeparator: String? {
switch (style, explode) {
case (.form, false): return "="
default: return nil
}
}
}