-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLQueryKeyedDecodingContainer.swift
More file actions
178 lines (140 loc) · 5.9 KB
/
Copy pathURLQueryKeyedDecodingContainer.swift
File metadata and controls
178 lines (140 loc) · 5.9 KB
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
import Foundation
internal final class URLQueryKeyedDecodingContainer<Key: CodingKey>: URLQueryValueDecoding {
internal let dictionary: [String: URLQueryValue]
internal let options: URLQueryDecodingOptions
internal let userInfo: [CodingUserInfoKey: Any]
internal let codingPath: [CodingKey]
internal init(
dictionary: [String: URLQueryValue],
options: URLQueryDecodingOptions,
userInfo: [CodingUserInfoKey: Any],
codingPath: [CodingKey]
) {
switch options.keyDecodingStrategy {
case .useDefaultKeys:
self.dictionary = dictionary
case let .custom(closure):
let keysAndValues = dictionary.map { key, value in
(closure(codingPath.appending(AnyCodingKey(key))).stringValue, value)
}
self.dictionary = Dictionary(keysAndValues) { $1 }
}
self.options = options
self.userInfo = userInfo
self.codingPath = codingPath
}
private func value<T>(forKey key: Key, of type: T.Type = T.self) throws -> T {
let anyValue = dictionary[key.stringValue]
guard let value = anyValue as? T else {
throw DecodingError.invalidValue(
anyValue,
forKey: key,
at: codingPath.appending(key),
expectation: type
)
}
return value
}
private func superDecoder(forAnyKey key: CodingKey) throws -> Decoder {
URLQuerySingleValueDecodingContainer(
value: dictionary[key.stringValue],
options: options,
userInfo: userInfo,
codingPath: codingPath.appending(key)
)
}
}
extension URLQueryKeyedDecodingContainer: KeyedDecodingContainerProtocol {
internal var allKeys: [Key] {
dictionary.keys.compactMap { Key(stringValue: $0) }
}
internal func contains(_ key: Key) -> Bool {
dictionary.contains { $0.key == key.stringValue }
}
internal func decodeNil(forKey key: Key) throws -> Bool {
decodeNil(try value(forKey: key))
}
internal func decode(_ type: String.Type, forKey key: Key) throws -> String {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: Int.Type, forKey key: Key) throws -> Int {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: Double.Type, forKey key: Key) throws -> Double {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode(_ type: Float.Type, forKey key: Key) throws -> Float {
try decodeValue(try value(forKey: key), at: codingPath.appending(key))
}
internal func decode<T: Decodable>(_ type: T.Type, forKey key: Key) throws -> T {
try decodeValue(try value(forKey: key), at: codingPath.appending(key), of: type)
}
internal func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type,
forKey key: Key
) throws -> KeyedDecodingContainer<NestedKey> {
try superDecoder(forAnyKey: key).container(keyedBy: keyType)
}
internal func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {
try superDecoder(forAnyKey: key).unkeyedContainer()
}
internal func superDecoder(forKey key: Key) throws -> Decoder {
try superDecoder(forAnyKey: key)
}
internal func superDecoder() throws -> Decoder {
try superDecoder(forAnyKey: AnyCodingKey.super)
}
}
extension DecodingError {
fileprivate static func invalidValue<Key: CodingKey>(
_ value: Any?,
forKey key: Key,
at codingPath: [CodingKey],
expectation: Any.Type
) -> Self {
switch value {
case let value?:
let context = Context(
codingPath: codingPath,
debugDescription: "Expected to decode \(expectation) but found \(type(of: value)) instead."
)
return .typeMismatch(expectation, context)
case nil:
let context = Context(
codingPath: codingPath,
debugDescription: "No value associated with key \(key.stringValue)."
)
return .keyNotFound(key, context)
}
}
}