this repo has no description
1//
2// EncodingsingleValueContainer+Keyed.swift
3// URLQueryItemCoder
4//
5// Created by Kyle Hughes on 1/15/23.
6//
7
8extension EncodingContainer {
9 internal final class Keyed {
10 internal private(set) var children: [String: EncodingContainer]
11 internal private(set) var codingPath: [any CodingKey]
12
13 private let configuration: EncodingStrategies
14
15 // MARK: Internal Initialization
16
17 internal init(codingPath: [any CodingKey], configuration: EncodingStrategies) {
18 self.codingPath = codingPath
19 self.configuration = configuration
20
21 children = [:]
22 }
23
24 // MARK: Internal Instance Interface
25
26 internal func wrapped<NestedKey>() -> Wrapper<NestedKey> {
27 Wrapper(self)
28 }
29
30 // MARK: Private Instance Interface
31
32 private func nextCodingPath(appending key: any CodingKey) -> [any CodingKey] {
33 var nextCodingPath = codingPath
34 nextCodingPath.append(key)
35
36 return nextCodingPath
37 }
38 }
39}
40
41// MARK: - KeyedEncodingsingleValueContainerProtocol Extension
42
43extension EncodingContainer.Keyed: KeyedEncodingContainerProtocol {
44 // MARK: Internal Instance Interface
45
46 internal func encode(_ value: Bool, forKey key: StringCodingKey) throws {
47 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
48 let nextCodingPath = nextCodingPath(appending: encodedKey)
49
50 let singleValueContainer = EncodingContainer.SingleValue(
51 codingPath: nextCodingPath,
52 configuration: configuration
53 )
54
55 try singleValueContainer.encode(value)
56
57 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
58 }
59
60 internal func encode(_ value: Double, forKey key: StringCodingKey) throws {
61 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
62 let nextCodingPath = nextCodingPath(appending: encodedKey)
63
64 let singleValueContainer = EncodingContainer.SingleValue(
65 codingPath: nextCodingPath,
66 configuration: configuration
67 )
68
69 try singleValueContainer.encode(value)
70
71 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
72 }
73
74 internal func encode(_ value: Float, forKey key: StringCodingKey) throws {
75 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
76 let nextCodingPath = nextCodingPath(appending: encodedKey)
77
78 let singleValueContainer = EncodingContainer.SingleValue(
79 codingPath: nextCodingPath,
80 configuration: configuration
81 )
82
83 try singleValueContainer.encode(value)
84
85 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
86 }
87
88 internal func encode(_ value: Int, forKey key: StringCodingKey) throws {
89 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
90 let nextCodingPath = nextCodingPath(appending: encodedKey)
91
92 let singleValueContainer = EncodingContainer.SingleValue(
93 codingPath: nextCodingPath,
94 configuration: configuration
95 )
96
97 try singleValueContainer.encode(value)
98
99 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
100 }
101
102 internal func encode(_ value: Int8, forKey key: StringCodingKey) throws {
103 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
104 let nextCodingPath = nextCodingPath(appending: encodedKey)
105
106 let singleValueContainer = EncodingContainer.SingleValue(
107 codingPath: nextCodingPath,
108 configuration: configuration
109 )
110
111 try singleValueContainer.encode(value)
112
113 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
114 }
115
116 internal func encode(_ value: Int16, forKey key: StringCodingKey) throws {
117 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
118 let nextCodingPath = nextCodingPath(appending: encodedKey)
119
120 let singleValueContainer = EncodingContainer.SingleValue(
121 codingPath: nextCodingPath,
122 configuration: configuration
123 )
124
125 try singleValueContainer.encode(value)
126
127 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
128 }
129
130 internal func encode(_ value: Int32, forKey key: StringCodingKey) throws {
131 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
132 let nextCodingPath = nextCodingPath(appending: encodedKey)
133
134 let singleValueContainer = EncodingContainer.SingleValue(
135 codingPath: nextCodingPath,
136 configuration: configuration
137 )
138
139 try singleValueContainer.encode(value)
140
141 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
142 }
143
144 internal func encode(_ value: Int64, forKey key: StringCodingKey) throws {
145 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
146 let nextCodingPath = nextCodingPath(appending: encodedKey)
147
148 let singleValueContainer = EncodingContainer.SingleValue(
149 codingPath: nextCodingPath,
150 configuration: configuration
151 )
152
153 try singleValueContainer.encode(value)
154
155 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
156 }
157
158 internal func encode(_ value: String, forKey key: StringCodingKey) throws {
159 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
160 let nextCodingPath = nextCodingPath(appending: encodedKey)
161
162 let singleValueContainer = EncodingContainer.SingleValue(
163 codingPath: nextCodingPath,
164 configuration: configuration
165 )
166
167 try singleValueContainer.encode(value)
168
169 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
170 }
171
172 internal func encode(_ value: UInt, forKey key: StringCodingKey) throws {
173 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
174 let nextCodingPath = nextCodingPath(appending: encodedKey)
175
176 let singleValueContainer = EncodingContainer.SingleValue(
177 codingPath: nextCodingPath,
178 configuration: configuration
179 )
180
181 try singleValueContainer.encode(value)
182
183 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
184 }
185
186 internal func encode(_ value: UInt8, forKey key: StringCodingKey) throws {
187 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
188 let nextCodingPath = nextCodingPath(appending: encodedKey)
189
190 let singleValueContainer = EncodingContainer.SingleValue(
191 codingPath: nextCodingPath,
192 configuration: configuration
193 )
194
195 try singleValueContainer.encode(value)
196
197 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
198 }
199
200 internal func encode(_ value: UInt16, forKey key: StringCodingKey) throws {
201 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
202 let nextCodingPath = nextCodingPath(appending: encodedKey)
203
204 let singleValueContainer = EncodingContainer.SingleValue(
205 codingPath: nextCodingPath,
206 configuration: configuration
207 )
208
209 try singleValueContainer.encode(value)
210
211 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
212 }
213
214 internal func encode(_ value: UInt32, forKey key: StringCodingKey) throws {
215 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
216 let nextCodingPath = nextCodingPath(appending: encodedKey)
217
218 let singleValueContainer = EncodingContainer.SingleValue(
219 codingPath: nextCodingPath,
220 configuration: configuration
221 )
222
223 try singleValueContainer.encode(value)
224
225 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
226 }
227
228 internal func encode(_ value: UInt64, forKey key: StringCodingKey) throws {
229 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
230 let nextCodingPath = nextCodingPath(appending: encodedKey)
231
232 let singleValueContainer = EncodingContainer.SingleValue(
233 codingPath: nextCodingPath,
234 configuration: configuration
235 )
236
237 try singleValueContainer.encode(value)
238
239 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
240 }
241
242 internal func encode<Value>(_ value: Value, forKey key: StringCodingKey) throws where Value: Encodable {
243 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
244 let nextCodingPath = nextCodingPath(appending: encodedKey)
245
246 children[encodedKey.stringValue] = try .encodeWithSpecialTreatment(
247 value,
248 at: nextCodingPath,
249 using: configuration
250 )
251 }
252
253 internal func encodeNil(forKey key: StringCodingKey) throws {
254 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
255 let nextCodingPath = nextCodingPath(appending: encodedKey)
256
257 let singleValueContainer = EncodingContainer.SingleValue(
258 codingPath: nextCodingPath,
259 configuration: configuration
260 )
261
262 try singleValueContainer.encodeNil()
263
264 children[encodedKey.stringValue] = .singleValue(singleValueContainer)
265 }
266
267 internal func nestedContainer<NestedKey>(
268 keyedBy keyType: NestedKey.Type,
269 forKey key: StringCodingKey
270 ) -> KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey {
271 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
272 let nextCodingPath = nextCodingPath(appending: encodedKey)
273
274 let keyedContainer = EncodingContainer.Keyed(codingPath: nextCodingPath, configuration: configuration)
275
276 children[encodedKey.stringValue] = .keyed(keyedContainer)
277
278 return KeyedEncodingContainer(keyedContainer.wrapped())
279 }
280
281 internal func nestedUnkeyedContainer(forKey key: StringCodingKey) -> UnkeyedEncodingContainer {
282 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
283 let nextCodingPath = nextCodingPath(appending: encodedKey)
284
285 let unkeyedContainer = EncodingContainer.Unkeyed(codingPath: nextCodingPath, configuration: configuration)
286
287 children[encodedKey.stringValue] = .unkeyed(unkeyedContainer)
288
289 return unkeyedContainer
290 }
291
292 internal func superEncoder() -> Encoder {
293 let key = StringCodingKey(stringValue: "super")
294
295 return superEncoder(forKey: key)
296 }
297
298 internal func superEncoder(forKey key: StringCodingKey) -> Encoder {
299 let encodedKey = configuration.keyStrategy.encode(key, at: codingPath)
300 let nextCodingPath = nextCodingPath(appending: encodedKey)
301
302 let lowLevelEncoder = LowLevelEncoder(codingPath: nextCodingPath, configuration: configuration)
303
304 children[encodedKey.stringValue] = .lowLevelEncoder(lowLevelEncoder)
305
306 return lowLevelEncoder
307 }
308}