forked from apple/swift-openapi-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURIValueFromNodeDecoder+Keyed.swift
170 lines (138 loc) · 7.84 KB
/
URIValueFromNodeDecoder+Keyed.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
//===----------------------------------------------------------------------===//
//
// 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 keyed container used by `URIValueFromNodeDecoder`.
struct URIKeyedDecodingContainer<Key: CodingKey> {
/// The associated decoder.
let decoder: URIValueFromNodeDecoder
}
extension URIKeyedDecodingContainer {
/// Returns the value found for the provided key in the underlying
/// dictionary.
/// - Parameter key: The key for which to return the value.
/// - Returns: The value found for the provided key.
/// - Throws: An error if no value for the key was found.
private func _decodeValue(forKey key: Key) throws -> URIParsedValue {
guard let value = try decoder.nestedElementInCurrentDictionary(forKey: key.stringValue) else {
throw DecodingError.keyNotFound(key, .init(codingPath: codingPath, debugDescription: "Key not found."))
}
return value
}
/// Returns the value found for the provided key in the underlying
/// dictionary converted to the provided type.
/// - Parameters:
/// - _: The `BinaryFloatingPoint` type to convert the value to.
/// - key: The key for which to return the value.
/// - Returns: The converted value found for the provided key.
/// - Throws: An error if no value for the key was found or if the
/// conversion failed.
private func _decodeBinaryFloatingPoint<T: BinaryFloatingPoint>(_: T.Type = T.self, forKey key: Key) throws -> T {
guard let double = Double(try _decodeValue(forKey: key)) else {
throw DecodingError.typeMismatch(
T.self,
.init(codingPath: codingPath, debugDescription: "Failed to convert to Double.")
)
}
return T(double)
}
/// Returns the value found for the provided key in the underlying
/// dictionary converted to the provided type.
/// - Parameters:
/// - _: The fixed-width integer type to convert the value to.
/// - key: The key for which to return the value.
/// - Returns: The converted value found for the provided key.
/// - Throws: An error if no value for the key was found or if the
/// conversion failed.
private func _decodeFixedWidthInteger<T: FixedWidthInteger>(_: T.Type = T.self, forKey key: Key) throws -> T {
guard let parsedValue = T(try _decodeValue(forKey: key)) else {
throw DecodingError.typeMismatch(
T.self,
.init(codingPath: codingPath, debugDescription: "Failed to convert to the requested type.")
)
}
return parsedValue
}
/// Returns the value found for the provided key in the underlying
/// dictionary converted to the provided type.
/// - Parameters:
/// - _: The type to convert the value to.
/// - key: The key for which to return the value.
/// - Returns: The converted value found for the provided key.
/// - Throws: An error if no value for the key was found or if the
/// conversion failed.
private func _decodeNextLosslessStringConvertible<T: LosslessStringConvertible>(_: T.Type = T.self, forKey key: Key)
throws -> T
{
guard let parsedValue = T(String(try _decodeValue(forKey: key))) else {
throw DecodingError.typeMismatch(
T.self,
.init(codingPath: codingPath, debugDescription: "Failed to convert to the requested type.")
)
}
return parsedValue
}
}
extension URIKeyedDecodingContainer: KeyedDecodingContainerProtocol {
var allKeys: [Key] { decoder.elementKeysInCurrentDictionary().compactMap { .init(stringValue: $0) } }
func contains(_ key: Key) -> Bool { decoder.containsElementInCurrentDictionary(forKey: key.stringValue) }
var codingPath: [any CodingKey] { decoder.codingPath }
func decodeNil(forKey key: Key) -> Bool { false }
func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
try _decodeNextLosslessStringConvertible(forKey: key)
}
func decode(_ type: String.Type, forKey key: Key) throws -> String { String(try _decodeValue(forKey: key)) }
func decode(_ type: Double.Type, forKey key: Key) throws -> Double { try _decodeBinaryFloatingPoint(forKey: key) }
func decode(_ type: Float.Type, forKey key: Key) throws -> Float { try _decodeBinaryFloatingPoint(forKey: key) }
func decode(_ type: Int.Type, forKey key: Key) throws -> Int { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 { try _decodeFixedWidthInteger(forKey: key) }
func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 { try _decodeFixedWidthInteger(forKey: key) }
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T: Decodable {
switch type {
case is Bool.Type: return try decode(Bool.self, forKey: key) as! T
case is String.Type: return try decode(String.self, forKey: key) as! T
case is Double.Type: return try decode(Double.self, forKey: key) as! T
case is Float.Type: return try decode(Float.self, forKey: key) as! T
case is Int.Type: return try decode(Int.self, forKey: key) as! T
case is Int8.Type: return try decode(Int8.self, forKey: key) as! T
case is Int16.Type: return try decode(Int16.self, forKey: key) as! T
case is Int32.Type: return try decode(Int32.self, forKey: key) as! T
case is Int64.Type: return try decode(Int64.self, forKey: key) as! T
case is UInt.Type: return try decode(UInt.self, forKey: key) as! T
case is UInt8.Type: return try decode(UInt8.self, forKey: key) as! T
case is UInt16.Type: return try decode(UInt16.self, forKey: key) as! T
case is UInt32.Type: return try decode(UInt32.self, forKey: key) as! T
case is UInt64.Type: return try decode(UInt64.self, forKey: key) as! T
case is Date.Type: return try decoder.dateTranscoder.decode(String(_decodeValue(forKey: key))) as! T
default:
decoder.push(.init(key))
defer { decoder.pop() }
return try type.init(from: decoder)
}
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<
NestedKey
> where NestedKey: CodingKey { throw URIValueFromNodeDecoder.GeneralError.nestedContainersNotSupported }
func nestedUnkeyedContainer(forKey key: Key) throws -> any UnkeyedDecodingContainer {
throw URIValueFromNodeDecoder.GeneralError.nestedContainersNotSupported
}
func superDecoder(forKey key: Key) throws -> any Decoder { decoder }
func superDecoder() throws -> any Decoder { decoder }
}