@@ -25,7 +25,9 @@ import Foundation
25
25
open class BinaryDecoder {
26
26
27
27
/// Initializes `self`.
28
- public init ( ) { }
28
+ public init ( userInfo: [ CodingUserInfoKey : Any ] = [ : ] ) {
29
+ self . userInfo = userInfo
30
+ }
29
31
30
32
///
31
33
/// Decodes a top-level value of the given type `T` from the Binary representation `data`.
@@ -40,8 +42,10 @@ open class BinaryDecoder {
40
42
/// - throws: An error if any value throws an error during decoding.
41
43
///
42
44
open func decode< T : Decodable > ( _ type: T . Type , from data: EncodedData ) throws -> T {
43
- return try T . init ( from: _BinaryDecoder ( codingPath: [ ] , rootStorage: data. storage) )
45
+ return try T . init ( from: _BinaryDecoder ( codingPath: [ ] , rootStorage: data. storage, userInfo : self . userInfo ) )
44
46
}
47
+
48
+ public var userInfo : [ CodingUserInfoKey : Any ]
45
49
}
46
50
47
51
// MARK: -
@@ -61,10 +65,10 @@ private class _BinaryDecoder : Decoder {
61
65
/// - codingPath: The path of coding keys taken to get to this point in decoding.
62
66
/// - rootStorage: The rootStorage container used for decoding values.
63
67
///
64
- init ( codingPath: [ CodingKey ] , rootStorage: StorageContainer ) {
68
+ init ( codingPath: [ CodingKey ] , rootStorage: StorageContainer , userInfo : [ CodingUserInfoKey : Any ] ) {
65
69
self . rootStorage = rootStorage
66
70
self . codingPath = codingPath
67
- self . userInfo = [ : ]
71
+ self . userInfo = userInfo
68
72
}
69
73
70
74
// MARK: - `Decoder` conformance.
@@ -79,7 +83,7 @@ private class _BinaryDecoder : Decoder {
79
83
func container< Key> ( keyedBy type: Key . Type ) throws -> KeyedDecodingContainer < Key > where Key : CodingKey {
80
84
let storage = try storageContainer ( KeyedStorageContainer . self, errorType: KeyedDecodingContainer< Key> . self )
81
85
82
- return KeyedDecodingContainer ( _BinaryKeyedDecodingContainer < Key > ( codingPath: self . codingPath, rootStorage: storage) )
86
+ return KeyedDecodingContainer ( _BinaryKeyedDecodingContainer < Key > ( referencing : self , codingPath: self . codingPath, rootStorage: storage) )
83
87
}
84
88
85
89
///
@@ -88,14 +92,14 @@ private class _BinaryDecoder : Decoder {
88
92
func unkeyedContainer( ) throws -> UnkeyedDecodingContainer {
89
93
let storage = try storageContainer ( UnkeyedStorageContainer . self, errorType: UnkeyedDecodingContainer . self)
90
94
91
- return _BinaryUnkeyedDecodingContainer ( codingPath: codingPath, rootStorage: storage)
95
+ return _BinaryUnkeyedDecodingContainer ( referencing : self , codingPath: codingPath, rootStorage: storage)
92
96
}
93
97
94
98
///
95
99
/// - throws: `DecodingError.typeMismatch` if the encountered stored value is not a single value container.
96
100
///
97
101
func singleValueContainer( ) throws -> SingleValueDecodingContainer {
98
- return _BinarySingleValueDecodingContainer ( codingPath: codingPath, storage: self . rootStorage)
102
+ return _BinarySingleValueDecodingContainer ( referencing : self , codingPath: codingPath, storage: self . rootStorage)
99
103
}
100
104
101
105
// MARK: - Private methods and storage.
@@ -142,9 +146,10 @@ extension _BinaryDecoder {
142
146
/// - codingPath: The path of coding keys taken to get to this point in decoding.
143
147
/// - rootStorage: The `KeyedStorageContainer` used for decoding values.
144
148
///
145
- init ( codingPath: [ CodingKey ] , rootStorage: KeyedStorageContainer ) {
149
+ init ( referencing decoder: _BinaryDecoder , codingPath: [ CodingKey ] , rootStorage: KeyedStorageContainer ) {
150
+ self . decoder = decoder
146
151
self . rootStorage = rootStorage
147
- self . codingPath = codingPath
152
+ self . codingPath = codingPath
148
153
}
149
154
150
155
// MARK: - `KeyedDecodingContainerProtocol` conformance.
@@ -221,7 +226,7 @@ extension _BinaryDecoder {
221
226
func decode< T: Decodable > ( _ type: T . Type , forKey key: K ) throws -> T {
222
227
let storage = try storageContainer ( StorageContainer . self, forKey: key, errorType: type)
223
228
224
- return try T . init ( from: _BinaryDecoder ( codingPath: self . codingPath + key, rootStorage: storage) )
229
+ return try T . init ( from: _BinaryDecoder ( codingPath: self . codingPath + key, rootStorage: storage, userInfo : self . decoder . userInfo ) )
225
230
}
226
231
227
232
///
@@ -230,7 +235,7 @@ extension _BinaryDecoder {
230
235
func nestedContainer< NestedKey> ( keyedBy type: NestedKey . Type , forKey key: K ) throws -> KeyedDecodingContainer < NestedKey > where NestedKey : CodingKey {
231
236
let storage = try storageContainer ( KeyedStorageContainer . self, forKey: key, errorType: KeyedDecodingContainer< NestedKey> . self )
232
237
233
- return KeyedDecodingContainer ( _BinaryKeyedDecodingContainer < NestedKey > ( codingPath: self . codingPath + key, rootStorage: storage) )
238
+ return KeyedDecodingContainer ( _BinaryKeyedDecodingContainer < NestedKey > ( referencing : self . decoder , codingPath: self . codingPath + key, rootStorage: storage) )
234
239
}
235
240
236
241
///
@@ -239,7 +244,7 @@ extension _BinaryDecoder {
239
244
func nestedUnkeyedContainer( forKey key: K ) throws -> UnkeyedDecodingContainer {
240
245
let storage = try storageContainer ( UnkeyedStorageContainer . self, forKey: key, errorType: UnkeyedDecodingContainer . self)
241
246
242
- return _BinaryUnkeyedDecodingContainer ( codingPath: self . codingPath + key, rootStorage: storage)
247
+ return _BinaryUnkeyedDecodingContainer ( referencing : self . decoder , codingPath: self . codingPath + key, rootStorage: storage)
243
248
}
244
249
245
250
///
@@ -249,7 +254,7 @@ extension _BinaryDecoder {
249
254
func superDecoder( forKey key: K ) throws -> Decoder {
250
255
let storage = try storageContainer ( StorageContainer . self, forKey: key, errorType: Decoder . self)
251
256
252
- return _BinaryDecoder ( codingPath: self . codingPath + key, rootStorage: storage)
257
+ return _BinaryDecoder ( codingPath: self . codingPath + key, rootStorage: storage, userInfo : self . decoder . userInfo )
253
258
}
254
259
255
260
///
@@ -259,7 +264,7 @@ extension _BinaryDecoder {
259
264
func superDecoder( ) throws -> Decoder {
260
265
let storage = try storageContainer ( StorageContainer . self, forKey: BinaryCodingKey ( " super " ) , errorType: Decoder . self)
261
266
262
- return _BinaryDecoder ( codingPath: self . codingPath, rootStorage: storage)
267
+ return _BinaryDecoder ( codingPath: self . codingPath, rootStorage: storage, userInfo : self . decoder . userInfo )
263
268
}
264
269
265
270
// MARK: - Private methods and storage
@@ -303,6 +308,12 @@ extension _BinaryDecoder {
303
308
return typedStorage
304
309
}
305
310
311
+ /// The decoder this container was created from.
312
+ ///
313
+ private let decoder : _BinaryDecoder
314
+
315
+ /// The root storage for this container.
316
+ ///
306
317
private var rootStorage : KeyedStorageContainer
307
318
}
308
319
@@ -321,9 +332,10 @@ extension _BinaryDecoder {
321
332
/// - codingPath: The path of coding keys taken to get to this point in decoding.
322
333
/// - rootStorage: The `UnkeyedStorageContainer` used for decoding values.
323
334
///
324
- init ( codingPath: [ CodingKey ] , rootStorage: UnkeyedStorageContainer ) {
325
- self . rootStorage = rootStorage
326
- self . codingPath = codingPath
335
+ init ( referencing decoder: _BinaryDecoder , codingPath: [ CodingKey ] , rootStorage: UnkeyedStorageContainer ) {
336
+ self . decoder = decoder
337
+ self . rootStorage = rootStorage
338
+ self . codingPath = codingPath
327
339
self . currentIndex = 0
328
340
}
329
341
@@ -395,7 +407,7 @@ extension _BinaryDecoder {
395
407
mutating func decode< T: Decodable > ( _ type: T . Type ) throws -> T {
396
408
let storage = try self . nextStorageContainer ( as: StorageContainer . self, errorType: type)
397
409
398
- return try T . init ( from: _BinaryDecoder ( codingPath: self . codingPath, rootStorage: storage) )
410
+ return try T . init ( from: _BinaryDecoder ( codingPath: self . codingPath, rootStorage: storage, userInfo : self . decoder . userInfo ) )
399
411
}
400
412
401
413
///
@@ -404,7 +416,7 @@ extension _BinaryDecoder {
404
416
mutating func nestedContainer< NestedKey> ( keyedBy type: NestedKey . Type ) throws -> KeyedDecodingContainer < NestedKey > where NestedKey : CodingKey {
405
417
let storage = try self . nextStorageContainer ( as: KeyedStorageContainer . self, errorType: KeyedDecodingContainer< NestedKey> . self )
406
418
407
- return KeyedDecodingContainer < NestedKey > ( _BinaryKeyedDecodingContainer ( codingPath: self . codingPath, rootStorage: storage) )
419
+ return KeyedDecodingContainer < NestedKey > ( _BinaryKeyedDecodingContainer ( referencing : self . decoder , codingPath: self . codingPath, rootStorage: storage) )
408
420
}
409
421
410
422
///
@@ -413,7 +425,7 @@ extension _BinaryDecoder {
413
425
mutating func nestedUnkeyedContainer( ) throws -> UnkeyedDecodingContainer {
414
426
let storage = try self . nextStorageContainer ( as: UnkeyedStorageContainer . self, errorType: UnkeyedDecodingContainer . self)
415
427
416
- return _BinaryUnkeyedDecodingContainer ( codingPath: self . codingPath, rootStorage: storage)
428
+ return _BinaryUnkeyedDecodingContainer ( referencing : self . decoder , codingPath: self . codingPath, rootStorage: storage)
417
429
}
418
430
419
431
///
@@ -431,7 +443,7 @@ extension _BinaryDecoder {
431
443
432
444
defer { currentIndex += 1 }
433
445
434
- return _BinaryDecoder ( codingPath: self . codingPath, rootStorage: rootStorage [ currentIndex] )
446
+ return _BinaryDecoder ( codingPath: self . codingPath, rootStorage: rootStorage [ currentIndex] , userInfo : self . decoder . userInfo )
435
447
}
436
448
437
449
// MARK: - Private methods and storage.
@@ -475,6 +487,12 @@ extension _BinaryDecoder {
475
487
return storage
476
488
}
477
489
490
+ /// The decoder this container was created from.
491
+ ///
492
+ private let decoder : _BinaryDecoder
493
+
494
+ /// The root storage for this container.
495
+ ///
478
496
private var rootStorage : UnkeyedStorageContainer
479
497
}
480
498
@@ -493,8 +511,9 @@ extension _BinaryDecoder {
493
511
/// - codingPath: The path of coding keys taken to get to this point in decoding.
494
512
/// - rootStorage: The `StorageContainer` containing the value to decode.
495
513
///
496
- init ( codingPath: [ CodingKey ] , storage: StorageContainer ) {
497
- self . codingPath = codingPath
514
+ init ( referencing decoder: _BinaryDecoder , codingPath: [ CodingKey ] , storage: StorageContainer ) {
515
+ self . decoder = decoder
516
+ self . codingPath = codingPath
498
517
self . rootStorage = storage
499
518
}
500
519
@@ -555,7 +574,7 @@ extension _BinaryDecoder {
555
574
/// Instead we create a new _BinaryDecoder and pass the storage reference up to that decoder
556
575
/// which will then decode the value.
557
576
///
558
- return try T . init ( from: _BinaryDecoder ( codingPath: self . codingPath, rootStorage: rootStorage) )
577
+ return try T . init ( from: _BinaryDecoder ( codingPath: self . codingPath, rootStorage: rootStorage, userInfo : self . decoder . userInfo ) )
559
578
}
560
579
561
580
// MARK: - Private methods and storage
@@ -582,6 +601,12 @@ extension _BinaryDecoder {
582
601
}
583
602
}
584
603
604
+ /// The decoder this container was created from.
605
+ ///
606
+ private let decoder : _BinaryDecoder
607
+
608
+ /// The root storage for this container.
609
+ ///
585
610
private var rootStorage : StorageContainer
586
611
}
587
612
}
0 commit comments