-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLQuerySingleValueDecodingContainer.swift
More file actions
177 lines (136 loc) · 5.14 KB
/
Copy pathURLQuerySingleValueDecodingContainer.swift
File metadata and controls
177 lines (136 loc) · 5.14 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
import Foundation
internal final class URLQuerySingleValueDecodingContainer: URLQueryValueDecoding {
internal let value: URLQueryValue?
internal let options: URLQueryDecodingOptions
internal let userInfo: [CodingUserInfoKey: Any]
internal let codingPath: [CodingKey]
internal init(
value: URLQueryValue?,
options: URLQueryDecodingOptions,
userInfo: [CodingUserInfoKey: Any],
codingPath: [CodingKey]
) {
self.value = value
self.options = options
self.userInfo = userInfo
self.codingPath = codingPath
}
}
extension URLQuerySingleValueDecodingContainer: SingleValueDecodingContainer {
internal func decodeNil() -> Bool {
decodeNil(value)
}
internal func decode(_ type: String.Type) throws -> String {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: Bool.Type) throws -> Bool {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: Int.Type) throws -> Int {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: Int8.Type) throws -> Int8 {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: Int16.Type) throws -> Int16 {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: Int32.Type) throws -> Int32 {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: Int64.Type) throws -> Int64 {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: UInt.Type) throws -> UInt {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: UInt8.Type) throws -> UInt8 {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: UInt16.Type) throws -> UInt16 {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: UInt32.Type) throws -> UInt32 {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: UInt64.Type) throws -> UInt64 {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: Double.Type) throws -> Double {
try decodeValue(value, at: codingPath)
}
internal func decode(_ type: Float.Type) throws -> Float {
try decodeValue(value, at: codingPath)
}
internal func decode<T: Decodable>(_ type: T.Type) throws -> T {
try decodeValue(value, at: codingPath, of: type)
}
}
extension URLQuerySingleValueDecodingContainer: Decoder {
internal func container<Key: CodingKey>(keyedBy keyType: Key.Type) throws -> KeyedDecodingContainer<Key> {
guard let dictionary = value?.dictionary else {
throw DecodingError.keyedContainerTypeMismatch(at: codingPath, value: value)
}
let container = URLQueryKeyedDecodingContainer<Key>(
dictionary: dictionary,
options: options,
userInfo: userInfo,
codingPath: codingPath
)
return KeyedDecodingContainer(container)
}
internal func unkeyedContainer() throws -> UnkeyedDecodingContainer {
guard let array = value?.array else {
throw DecodingError.unkeyedContainerTypeMismatch(at: codingPath, value: value)
}
return URLQueryUnkeyedDecodingContainer(
array: array,
options: options,
userInfo: userInfo,
codingPath: codingPath
)
}
internal func singleValueContainer() throws -> SingleValueDecodingContainer {
self
}
}
extension DecodingError {
fileprivate static func keyedContainerTypeMismatch(
at codingPath: [CodingKey],
value: URLQueryValue?
) -> Self {
let debugDescription: String
switch value {
case .string:
debugDescription = "Expected to decode a dictionary but found string instead."
case .array:
debugDescription = "Expected to decode a dictionary but found array instead."
case .dictionary:
debugDescription = "Cannot get keyed decoding container."
case nil:
debugDescription = "Cannot get keyed decoding container -- found null value instead."
}
return .typeMismatch([String: Any].self, Context(codingPath: codingPath, debugDescription: debugDescription))
}
fileprivate static func unkeyedContainerTypeMismatch(
at codingPath: [CodingKey],
value: URLQueryValue?
) -> Self {
let debugDescription: String
switch value {
case .string:
debugDescription = "Expected to decode an array but found string instead."
case .dictionary:
debugDescription = "Expected to decode an array but found dictionary instead."
case .array:
debugDescription = "Cannot get unkeyed decoding container."
case nil:
debugDescription = "Cannot get unkeyed decoding container -- found null value instead."
}
let context = Context(
codingPath: codingPath,
debugDescription: debugDescription
)
return .typeMismatch([Any].self, context)
}
}