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