Skip to content

Commit 43acb72

Browse files
committed
Merge branch 'master' of github.com:stickytools/sticky-encoding into cocoapods
* 'master' of github.com:stickytools/sticky-encoding: Added the ability to set a userInfo dictionary. (#5) # Resolved Conflicts: # Sources/StickyEncoding/BinaryDecoder.swift
2 parents 9309105 + 8f8cac9 commit 43acb72

File tree

4 files changed

+441
-53
lines changed

4 files changed

+441
-53
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
All significant changes to this project will be documented in this file.
3+
4+
## [1.0.0-beta.2] (Upcoming release)
5+
6+
#### Added
7+
- Added the ability to set a userInfo dictionary on the `BinaryEncoder` and `BinaryDecoder`.

Sources/StickyEncoding/BinaryDecoder.swift

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import Foundation
2525
open class BinaryDecoder {
2626

2727
/// Initializes `self`.
28-
public init() {}
28+
public init(userInfo: [CodingUserInfoKey : Any] = [:]) {
29+
self.userInfo = userInfo
30+
}
2931

3032
///
3133
/// Decodes a top-level value of the given type `T` from the Binary representation `data`.
@@ -40,8 +42,10 @@ open class BinaryDecoder {
4042
/// - throws: An error if any value throws an error during decoding.
4143
///
4244
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))
4446
}
47+
48+
public var userInfo: [CodingUserInfoKey : Any]
4549
}
4650

4751
// MARK: -
@@ -61,10 +65,10 @@ private class _BinaryDecoder : Decoder {
6165
/// - codingPath: The path of coding keys taken to get to this point in decoding.
6266
/// - rootStorage: The rootStorage container used for decoding values.
6367
///
64-
init(codingPath: [CodingKey], rootStorage: StorageContainer) {
68+
init(codingPath: [CodingKey], rootStorage: StorageContainer, userInfo: [CodingUserInfoKey : Any]) {
6569
self.rootStorage = rootStorage
6670
self.codingPath = codingPath
67-
self.userInfo = [:]
71+
self.userInfo = userInfo
6872
}
6973

7074
// MARK: - `Decoder` conformance.
@@ -79,7 +83,7 @@ private class _BinaryDecoder : Decoder {
7983
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
8084
let storage = try storageContainer(KeyedStorageContainer.self, errorType: KeyedDecodingContainer<Key>.self)
8185

82-
return KeyedDecodingContainer(_BinaryKeyedDecodingContainer<Key>(codingPath: self.codingPath, rootStorage: storage))
86+
return KeyedDecodingContainer(_BinaryKeyedDecodingContainer<Key>(referencing: self, codingPath: self.codingPath, rootStorage: storage))
8387
}
8488

8589
///
@@ -88,14 +92,14 @@ private class _BinaryDecoder : Decoder {
8892
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
8993
let storage = try storageContainer(UnkeyedStorageContainer.self, errorType: UnkeyedDecodingContainer.self)
9094

91-
return _BinaryUnkeyedDecodingContainer(codingPath: codingPath, rootStorage: storage)
95+
return _BinaryUnkeyedDecodingContainer(referencing: self, codingPath: codingPath, rootStorage: storage)
9296
}
9397

9498
///
9599
/// - throws: `DecodingError.typeMismatch` if the encountered stored value is not a single value container.
96100
///
97101
func singleValueContainer() throws -> SingleValueDecodingContainer {
98-
return _BinarySingleValueDecodingContainer(codingPath: codingPath, storage: self.rootStorage)
102+
return _BinarySingleValueDecodingContainer(referencing: self, codingPath: codingPath, storage: self.rootStorage)
99103
}
100104

101105
// MARK: - Private methods and storage.
@@ -142,9 +146,10 @@ extension _BinaryDecoder {
142146
/// - codingPath: The path of coding keys taken to get to this point in decoding.
143147
/// - rootStorage: The `KeyedStorageContainer` used for decoding values.
144148
///
145-
init(codingPath: [CodingKey], rootStorage: KeyedStorageContainer) {
149+
init(referencing decoder: _BinaryDecoder, codingPath: [CodingKey], rootStorage: KeyedStorageContainer) {
150+
self.decoder = decoder
146151
self.rootStorage = rootStorage
147-
self.codingPath = codingPath
152+
self.codingPath = codingPath
148153
}
149154

150155
// MARK: - `KeyedDecodingContainerProtocol` conformance.
@@ -221,7 +226,7 @@ extension _BinaryDecoder {
221226
func decode<T: Decodable >(_ type: T.Type, forKey key: K) throws -> T {
222227
let storage = try storageContainer(StorageContainer.self, forKey: key, errorType: type)
223228

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))
225230
}
226231

227232
///
@@ -230,7 +235,7 @@ extension _BinaryDecoder {
230235
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
231236
let storage = try storageContainer(KeyedStorageContainer.self, forKey: key, errorType: KeyedDecodingContainer<NestedKey>.self)
232237

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))
234239
}
235240

236241
///
@@ -239,7 +244,7 @@ extension _BinaryDecoder {
239244
func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer {
240245
let storage = try storageContainer(UnkeyedStorageContainer.self, forKey: key, errorType: UnkeyedDecodingContainer.self)
241246

242-
return _BinaryUnkeyedDecodingContainer(codingPath: self.codingPath + key, rootStorage: storage)
247+
return _BinaryUnkeyedDecodingContainer(referencing: self.decoder, codingPath: self.codingPath + key, rootStorage: storage)
243248
}
244249

245250
///
@@ -249,7 +254,7 @@ extension _BinaryDecoder {
249254
func superDecoder(forKey key: K) throws -> Decoder {
250255
let storage = try storageContainer(StorageContainer.self, forKey: key, errorType: Decoder.self)
251256

252-
return _BinaryDecoder(codingPath: self.codingPath + key, rootStorage: storage)
257+
return _BinaryDecoder(codingPath: self.codingPath + key, rootStorage: storage, userInfo: self.decoder.userInfo)
253258
}
254259

255260
///
@@ -259,7 +264,7 @@ extension _BinaryDecoder {
259264
func superDecoder() throws -> Decoder {
260265
let storage = try storageContainer(StorageContainer.self, forKey: BinaryCodingKey("super"), errorType: Decoder.self)
261266

262-
return _BinaryDecoder(codingPath: self.codingPath, rootStorage: storage)
267+
return _BinaryDecoder(codingPath: self.codingPath, rootStorage: storage, userInfo: self.decoder.userInfo)
263268
}
264269

265270
// MARK: - Private methods and storage
@@ -303,6 +308,12 @@ extension _BinaryDecoder {
303308
return typedStorage
304309
}
305310

311+
/// The decoder this container was created from.
312+
///
313+
private let decoder: _BinaryDecoder
314+
315+
/// The root storage for this container.
316+
///
306317
private var rootStorage: KeyedStorageContainer
307318
}
308319

@@ -321,9 +332,10 @@ extension _BinaryDecoder {
321332
/// - codingPath: The path of coding keys taken to get to this point in decoding.
322333
/// - rootStorage: The `UnkeyedStorageContainer` used for decoding values.
323334
///
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
327339
self.currentIndex = 0
328340
}
329341

@@ -395,7 +407,7 @@ extension _BinaryDecoder {
395407
mutating func decode<T: Decodable>(_ type: T.Type) throws -> T {
396408
let storage = try self.nextStorageContainer(as: StorageContainer.self, errorType: type)
397409

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))
399411
}
400412

401413
///
@@ -404,7 +416,7 @@ extension _BinaryDecoder {
404416
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
405417
let storage = try self.nextStorageContainer(as: KeyedStorageContainer.self, errorType: KeyedDecodingContainer<NestedKey>.self)
406418

407-
return KeyedDecodingContainer<NestedKey>(_BinaryKeyedDecodingContainer(codingPath: self.codingPath, rootStorage: storage))
419+
return KeyedDecodingContainer<NestedKey>(_BinaryKeyedDecodingContainer(referencing: self.decoder, codingPath: self.codingPath, rootStorage: storage))
408420
}
409421

410422
///
@@ -413,7 +425,7 @@ extension _BinaryDecoder {
413425
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
414426
let storage = try self.nextStorageContainer(as: UnkeyedStorageContainer.self, errorType: UnkeyedDecodingContainer.self)
415427

416-
return _BinaryUnkeyedDecodingContainer(codingPath: self.codingPath, rootStorage: storage)
428+
return _BinaryUnkeyedDecodingContainer(referencing: self.decoder, codingPath: self.codingPath, rootStorage: storage)
417429
}
418430

419431
///
@@ -431,7 +443,7 @@ extension _BinaryDecoder {
431443

432444
defer { currentIndex += 1 }
433445

434-
return _BinaryDecoder(codingPath: self.codingPath, rootStorage: rootStorage[currentIndex])
446+
return _BinaryDecoder(codingPath: self.codingPath, rootStorage: rootStorage[currentIndex], userInfo: self.decoder.userInfo)
435447
}
436448

437449
// MARK: - Private methods and storage.
@@ -475,6 +487,12 @@ extension _BinaryDecoder {
475487
return storage
476488
}
477489

490+
/// The decoder this container was created from.
491+
///
492+
private let decoder: _BinaryDecoder
493+
494+
/// The root storage for this container.
495+
///
478496
private var rootStorage: UnkeyedStorageContainer
479497
}
480498

@@ -493,8 +511,9 @@ extension _BinaryDecoder {
493511
/// - codingPath: The path of coding keys taken to get to this point in decoding.
494512
/// - rootStorage: The `StorageContainer` containing the value to decode.
495513
///
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
498517
self.rootStorage = storage
499518
}
500519

@@ -555,7 +574,7 @@ extension _BinaryDecoder {
555574
/// Instead we create a new _BinaryDecoder and pass the storage reference up to that decoder
556575
/// which will then decode the value.
557576
///
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))
559578
}
560579

561580
// MARK: - Private methods and storage
@@ -582,6 +601,12 @@ extension _BinaryDecoder {
582601
}
583602
}
584603

604+
/// The decoder this container was created from.
605+
///
606+
private let decoder: _BinaryDecoder
607+
608+
/// The root storage for this container.
609+
///
585610
private var rootStorage: StorageContainer
586611
}
587612
}

0 commit comments

Comments
 (0)