@@ -276,102 +276,87 @@ private class _URLEncodedFormDecoder: Decoder {
276
276
self . currentIndex = 0
277
277
}
278
278
279
+ mutating func decodeNextNode< T: Decodable > ( as: T . Type = T . self) throws -> T {
280
+ guard !self . isAtEnd else {
281
+ throw DecodingError . valueNotFound (
282
+ T . self,
283
+ . init( codingPath: self . codingPath, debugDescription: " Unkeyed container index out of range " )
284
+ )
285
+ }
286
+ let node = self . container. values [ self . currentIndex]
287
+ self . currentIndex += 1
288
+ return try self . decoder. unbox ( node, as: T . self)
289
+ }
290
+
279
291
mutating func decodeNil( ) throws -> Bool {
280
292
let node = self . container. values [ self . currentIndex]
281
293
return try self . decoder. unboxNil ( node)
282
294
}
283
295
284
296
mutating func decode( _: Bool . Type ) throws -> Bool {
285
- let node = self . container. values [ self . currentIndex]
286
- self . currentIndex += 1
287
- return try self . decoder. unbox ( node, as: Bool . self)
297
+ try self . decodeNextNode ( )
288
298
}
289
299
290
300
mutating func decode( _: String . Type ) throws -> String {
291
- let node = self . container. values [ self . currentIndex]
292
- self . currentIndex += 1
293
- return try self . decoder. unbox ( node, as: String . self)
301
+ try self . decodeNextNode ( )
294
302
}
295
303
296
304
mutating func decode( _: Double . Type ) throws -> Double {
297
- let node = self . container. values [ self . currentIndex]
298
- self . currentIndex += 1
299
- return try self . decoder. unbox ( node, as: Double . self)
305
+ try self . decodeNextNode ( )
300
306
}
301
307
302
308
mutating func decode( _: Float . Type ) throws -> Float {
303
- let node = self . container. values [ self . currentIndex]
304
- self . currentIndex += 1
305
- return try self . decoder. unbox ( node, as: Float . self)
309
+ try self . decodeNextNode ( )
306
310
}
307
311
308
312
mutating func decode( _: Int . Type ) throws -> Int {
309
- let node = self . container. values [ self . currentIndex]
310
- self . currentIndex += 1
311
- return try self . decoder. unbox ( node, as: Int . self)
313
+ try self . decodeNextNode ( )
312
314
}
313
315
314
316
mutating func decode( _: Int8 . Type ) throws -> Int8 {
315
- let node = self . container. values [ self . currentIndex]
316
- self . currentIndex += 1
317
- return try self . decoder. unbox ( node, as: Int8 . self)
317
+ try self . decodeNextNode ( )
318
318
}
319
319
320
320
mutating func decode( _: Int16 . Type ) throws -> Int16 {
321
- let node = self . container. values [ self . currentIndex]
322
- self . currentIndex += 1
323
- return try self . decoder. unbox ( node, as: Int16 . self)
321
+ try self . decodeNextNode ( )
324
322
}
325
323
326
324
mutating func decode( _: Int32 . Type ) throws -> Int32 {
327
- let node = self . container. values [ self . currentIndex]
328
- self . currentIndex += 1
329
- return try self . decoder. unbox ( node, as: Int32 . self)
325
+ try self . decodeNextNode ( )
330
326
}
331
327
332
328
mutating func decode( _: Int64 . Type ) throws -> Int64 {
333
- let node = self . container. values [ self . currentIndex]
334
- self . currentIndex += 1
335
- return try self . decoder. unbox ( node, as: Int64 . self)
329
+ try self . decodeNextNode ( )
336
330
}
337
331
338
332
mutating func decode( _: UInt . Type ) throws -> UInt {
339
- let node = self . container. values [ self . currentIndex]
340
- self . currentIndex += 1
341
- return try self . decoder. unbox ( node, as: UInt . self)
333
+ try self . decodeNextNode ( )
342
334
}
343
335
344
336
mutating func decode( _: UInt8 . Type ) throws -> UInt8 {
345
- let node = self . container. values [ self . currentIndex]
346
- self . currentIndex += 1
347
- return try self . decoder. unbox ( node, as: UInt8 . self)
337
+ try self . decodeNextNode ( )
348
338
}
349
339
350
340
mutating func decode( _: UInt16 . Type ) throws -> UInt16 {
351
- let node = self . container. values [ self . currentIndex]
352
- self . currentIndex += 1
353
- return try self . decoder. unbox ( node, as: UInt16 . self)
341
+ try self . decodeNextNode ( )
354
342
}
355
343
356
344
mutating func decode( _: UInt32 . Type ) throws -> UInt32 {
357
- let node = self . container. values [ self . currentIndex]
358
- self . currentIndex += 1
359
- return try self . decoder. unbox ( node, as: UInt32 . self)
345
+ try self . decodeNextNode ( )
360
346
}
361
347
362
348
mutating func decode( _: UInt64 . Type ) throws -> UInt64 {
363
- let node = self . container. values [ self . currentIndex]
364
- self . currentIndex += 1
365
- return try self . decoder. unbox ( node, as: UInt64 . self)
349
+ try self . decodeNextNode ( )
366
350
}
367
351
368
352
mutating func decode< T> ( _: T . Type ) throws -> T where T: Decodable {
369
- let node = self . container. values [ self . currentIndex]
370
- self . currentIndex += 1
371
- return try self . decoder. unbox ( node, as: T . self)
353
+ try self . decodeNextNode ( )
372
354
}
373
355
374
356
mutating func nestedContainer< NestedKey> ( keyedBy type: NestedKey . Type ) throws -> KeyedDecodingContainer < NestedKey > where NestedKey: CodingKey {
357
+ guard !self . isAtEnd else {
358
+ throw DecodingError . dataCorrupted ( . init( codingPath: self . codingPath, debugDescription: " Unkeyed container index out of range " ) )
359
+ }
375
360
let node = container. values [ self . currentIndex]
376
361
self . currentIndex += 1
377
362
guard case . map( let map) = node else {
@@ -382,10 +367,13 @@ private class _URLEncodedFormDecoder: Decoder {
382
367
}
383
368
384
369
mutating func nestedUnkeyedContainer( ) throws -> UnkeyedDecodingContainer {
370
+ guard !self . isAtEnd else {
371
+ throw DecodingError . dataCorrupted ( . init( codingPath: self . codingPath, debugDescription: " Unkeyed container index out of range " ) )
372
+ }
385
373
let node = self . container. values [ self . currentIndex]
386
374
self . currentIndex += 1
387
375
guard case . array( let array) = node else {
388
- throw DecodingError . dataCorrupted ( . init( codingPath: self . codingPath, debugDescription: " Expected a dictionary " ) )
376
+ throw DecodingError . dataCorrupted ( . init( codingPath: self . codingPath, debugDescription: " Expected an array " ) )
389
377
}
390
378
return UKDC ( container: array, decoder: self . decoder)
391
379
}
0 commit comments