forked from apple/swift-openapi-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURIValueFromNodeDecoder.swift
323 lines (278 loc) · 13.3 KB
/
URIValueFromNodeDecoder.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
//===----------------------------------------------------------------------===//
//
// 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 allows decoding `Decodable` values from a `URIParsedNode`.
final class URIValueFromNodeDecoder {
/// The coder used for serializing Date values.
let dateTranscoder: any DateTranscoder
/// The underlying root node.
private let node: URIParsedNode
/// The key of the root value in the node.
private let rootKey: URIParsedKey
/// The variable expansion style.
private let style: URICoderConfiguration.Style
/// The explode parameter of the expansion style.
private let explode: Bool
/// The stack of nested values within the root node.
private var codingStack: [CodingStackEntry]
/// Creates a new decoder.
/// - Parameters:
/// - node: The underlying root node.
/// - rootKey: The key of the root value in the node.
/// - style: The variable expansion style.
/// - explode: The explode parameter of the expansion style.
/// - dateTranscoder: The coder used for serializing Date values.
init(
node: URIParsedNode,
rootKey: URIParsedKey,
style: URICoderConfiguration.Style,
explode: Bool,
dateTranscoder: any DateTranscoder
) {
self.node = node
self.rootKey = rootKey
self.style = style
self.explode = explode
self.dateTranscoder = dateTranscoder
self.codingStack = []
}
/// Decodes the provided type from the root node.
/// - Parameter type: The type to decode from the decoder.
/// - Returns: The decoded value.
/// - Throws: When a decoding error occurs.
func decodeRoot<T: Decodable>(_ type: T.Type = T.self) throws -> T {
precondition(codingStack.isEmpty)
defer { precondition(codingStack.isEmpty) }
// We have to catch the special values early, otherwise we fall
// back to their Codable implementations, which don't give us
// a chance to customize the coding in the containers.
let value: T
switch type {
case is Date.Type: value = try singleValueContainer().decode(Date.self) as! T
default: value = try T.init(from: self)
}
return value
}
/// Decodes the provided type from the root node.
/// - Parameter type: The type to decode from the decoder.
/// - Returns: The decoded value.
/// - Throws: When a decoding error occurs.
func decodeRootIfPresent<T: Decodable>(_ type: T.Type = T.self) throws -> T? {
// The root is only nil if the node is empty.
if style != .deepObject, try currentElementAsArray().isEmpty {
return nil
}
return try decodeRoot(type)
}
}
extension URIValueFromNodeDecoder {
/// A decoder error.
enum GeneralError: Swift.Error {
/// The decoder was asked to create a nested container.
case nestedContainersNotSupported
/// The decoder was asked for more items, but it was already at the
/// end of the unkeyed container.
case reachedEndOfUnkeyedContainer
/// The provided coding key does not have a valid integer value, but
/// it is being used for accessing items in an unkeyed container.
case codingKeyNotInt
/// The provided coding key is out of bounds of the unkeyed container.
case codingKeyOutOfBounds
/// The coding key is of a value not found in the keyed container.
case codingKeyNotFound
}
/// A node materialized by the decoder.
private enum URIDecodedNode {
/// A single value.
case single(URIParsedValue)
/// An array of values.
case array(URIParsedValueArray)
/// A dictionary of values.
case dictionary(URIParsedNode)
}
/// An entry in the coding stack for `URIValueFromNodeDecoder`.
///
/// This is used to keep track of where we are in the decode.
private struct CodingStackEntry {
/// The key at which the entry was found.
var key: URICoderCodingKey
/// The node at the key inside its parent.
var element: URIDecodedNode
}
/// The element at the current head of the coding stack.
private var currentElement: URIDecodedNode { codingStack.last?.element ?? .dictionary(node) }
/// Pushes a new container on top of the current stack, nesting into the
/// value at the provided key.
/// - Parameter codingKey: The coding key for the value that is then put
/// at the top of the stack.
/// - Throws: An error if an issue occurs during the container push operation.
func push(_ codingKey: URICoderCodingKey) throws {
let nextElement: URIDecodedNode
if let intValue = codingKey.intValue {
let value = try nestedValueInCurrentElementAsArray(at: intValue)
nextElement = .single(value)
} else {
let values = try nestedValuesInCurrentElementAsDictionary(forKey: codingKey.stringValue)
nextElement = .array(values)
}
codingStack.append(CodingStackEntry(key: codingKey, element: nextElement))
}
/// Pops the top container from the stack and restores the previously top
/// container to be the current top container.
func pop() { codingStack.removeLast() }
/// Throws a type mismatch error with the provided message.
/// - Parameter message: The message to be embedded as debug description
/// inside the thrown `DecodingError`.
/// - Throws: A `DecodingError` with a type mismatch error if this function is called.
private func throwMismatch(_ message: String) throws -> Never {
throw DecodingError.typeMismatch(String.self, .init(codingPath: codingPath, debugDescription: message))
}
/// Extracts the root value of the provided node using the root key.
/// - Parameter node: The node which to expect for the root key.
/// - Returns: The value found at the root key in the provided node.
/// - Throws: A `DecodingError` if the value is not found at the root key
private func rootValue(in node: URIParsedNode) throws -> URIParsedValueArray {
guard let value = node[rootKey] else {
if style == .simple, let valueForFallbackKey = node[""] {
// The simple style doesn't encode the key, so single values
// get encoded as a value only, and parsed under the empty
// string key.
return valueForFallbackKey
}
return []
}
return value
}
/// Extracts the node at the top of the coding stack and tries to treat it
/// as a dictionary.
/// - Returns: The value if it can be treated as a dictionary.
/// - Throws: An error if the current element cannot be treated as a dictionary.
private func currentElementAsDictionary() throws -> URIParsedNode { try nodeAsDictionary(currentElement) }
/// Checks if the provided node can be treated as a dictionary, and returns
/// it if so.
/// - Parameter node: The node to check.
/// - Returns: The value if it can be treated as a dictionary.
/// - Throws: An error if the node cannot be treated as a valid dictionary.
private func nodeAsDictionary(_ node: URIDecodedNode) throws -> URIParsedNode {
// There are multiple ways a valid dictionary is represented in a node,
// depends on the explode parameter.
// 1. exploded: Key-value pairs in the node: ["R":["100"]]
// 2. unexploded form: Flattened key-value pairs in the only top level
// key's value array: ["<root key>":["R","100"]]
// To simplify the code, when asked for a keyed container here and explode
// is false, we convert (2) to (1), and then treat everything as (1).
// The conversion only works if the number of values is even, including 0.
if explode {
guard case let .dictionary(values) = node else {
try throwMismatch("Cannot treat a single value or an array as a dictionary.")
}
return values
}
let values = try nodeAsArray(node)
if values == [""] && style == .simple {
// An unexploded simple combination produces a ["":[""]] for an
// empty string. It should be parsed as an empty dictionary.
return ["": [""]]
}
guard values.count % 2 == 0 else {
try throwMismatch("Cannot parse an unexploded dictionary an odd number of elements.")
}
let pairs = stride(from: values.startIndex, to: values.endIndex, by: 2)
.map { firstIndex in (values[firstIndex], [values[firstIndex + 1]]) }
let convertedNode = Dictionary(pairs, uniquingKeysWith: { $0 + $1 })
return convertedNode
}
/// Extracts the node at the top of the coding stack and tries to treat it
/// as an array.
/// - Returns: The value if it can be treated as an array.
/// - Throws: An error if the node cannot be treated as an array.
private func currentElementAsArray() throws -> URIParsedValueArray { try nodeAsArray(currentElement) }
/// Checks if the provided node can be treated as an array, and returns
/// it if so.
/// - Parameter node: The node to check.
/// - Returns: The value if it can be treated as an array.
/// - Throws: An error if the node cannot be treated as a valid array.
private func nodeAsArray(_ node: URIDecodedNode) throws -> URIParsedValueArray {
switch node {
case .single(let value): return [value]
case .array(let values): return values
case .dictionary(let values): return try rootValue(in: values)
}
}
/// Extracts the node at the top of the coding stack and tries to treat it
/// as a primitive value.
/// - Returns: The value if it can be treated as a primitive value.
/// - Throws: An error if the node cannot be treated as a primitive value.
func currentElementAsSingleValue() throws -> URIParsedValue { try nodeAsSingleValue(currentElement) }
/// Checks if the provided node can be treated as a primitive value, and
/// returns it if so.
/// - Parameter node: The node to check.
/// - Returns: The value if it can be treated as a primitive value.
/// - Throws: An error if the node cannot be treated as a primitive value.
private func nodeAsSingleValue(_ node: URIDecodedNode) throws -> URIParsedValue {
// A single value can be parsed from a node that:
// 1. Has a single key-value pair
// 2. The value array has a single element.
let array: URIParsedValueArray
switch node {
case .single(let value): return value
case .array(let values): array = values
case .dictionary(let values): array = try rootValue(in: values)
}
guard array.count == 1 else {
if style == .simple { return Substring(array.joined(separator: ",")) }
let reason = array.isEmpty ? "an empty node" : "a node with multiple values"
try throwMismatch("Cannot parse a value from \(reason).")
}
let value = array[0]
return value
}
/// Returns the nested value at the provided index inside the node at the
/// top of the coding stack.
/// - Parameter index: The index of the nested value.
/// - Returns: The nested value.
/// - Throws: An error if the current node is not a valid array, or if the
/// index is out of bounds.
private func nestedValueInCurrentElementAsArray(at index: Int) throws -> URIParsedValue {
let values = try currentElementAsArray()
guard index < values.count else { throw GeneralError.codingKeyOutOfBounds }
return values[index]
}
/// Returns the nested value at the provided key inside the node at the
/// top of the coding stack.
/// - Parameter key: The key of the nested value.
/// - Returns: The nested value.
/// - Throws: An error if the current node is not a valid dictionary, or
/// if no value exists for the key.
private func nestedValuesInCurrentElementAsDictionary(forKey key: String) throws -> URIParsedValueArray {
let values = try currentElementAsDictionary()
guard let value = values[key[...]] else { throw GeneralError.codingKeyNotFound }
return value
}
}
extension URIValueFromNodeDecoder: Decoder {
var codingPath: [any CodingKey] { codingStack.map(\.key) }
var userInfo: [CodingUserInfoKey: Any] { [:] }
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key: CodingKey {
let values = try currentElementAsDictionary()
return .init(URIKeyedDecodingContainer(decoder: self, values: values))
}
func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
let values = try currentElementAsArray()
return URIUnkeyedDecodingContainer(decoder: self, values: values)
}
func singleValueContainer() throws -> any SingleValueDecodingContainer {
URISingleValueDecodingContainer(decoder: self)
}
}