Skip to content

Commit 7c811a2

Browse files
committed
Updated logging
1 parent 5758c7e commit 7c811a2

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

Sources/CodingKey.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,7 @@ public extension TLVCodingKey where Self: CaseIterable, Self: RawRepresentable,
5555

5656
internal extension TLVTypeCode {
5757

58-
enum CodingKeyError: Error {
59-
60-
case missingIntegerValue
61-
case invalidIntegerValue(Int)
62-
}
63-
64-
init<K: CodingKey> (codingKey: K) throws {
58+
init? <K: CodingKey> (codingKey: K) {
6559

6660
if let tlvCodingKey = codingKey as? TLVCodingKey {
6761

@@ -71,7 +65,7 @@ internal extension TLVTypeCode {
7165

7266
guard intValue <= Int(UInt8.max),
7367
intValue >= Int(UInt8.min)
74-
else { throw CodingKeyError.invalidIntegerValue(intValue) }
68+
else { return nil }
7569

7670
self.init(rawValue: UInt8(intValue))
7771

@@ -82,7 +76,7 @@ internal extension TLVTypeCode {
8276

8377
} else {
8478

85-
throw CodingKeyError.missingIntegerValue
79+
return nil
8680
}
8781
}
8882
}
@@ -94,3 +88,17 @@ internal extension Sequence where Element == CodingKey {
9488
return reduce("", { $0 + "\($0.isEmpty ? "" : ".")" + $1.stringValue })
9589
}
9690
}
91+
92+
internal extension CodingKey {
93+
94+
static var sanitizedName: String {
95+
96+
let rawName = String(reflecting: self)
97+
var elements = rawName.split(separator: ".")
98+
guard elements.count > 2
99+
else { return rawName }
100+
elements.removeFirst()
101+
elements.removeAll { $0.contains("(unknown context") }
102+
return elements.reduce("", { $0 + ($0.isEmpty ? "" : ".") + $1 })
103+
}
104+
}

Sources/Decoder.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ internal extension TLVDecoder {
122122

123123
func container <Key: CodingKey> (keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
124124

125-
log?("Requested container keyed by \(String(reflecting: type)) for path \"\(codingPath.path)\"")
125+
log?("Requested container keyed by \(type.sanitizedName) for path \"\(codingPath.path)\"")
126126

127127
let container = self.stack.top
128128

@@ -530,8 +530,7 @@ internal struct TLVSingleValueDecodingContainer: SingleValueDecodingContainer {
530530

531531
func decode(_ type: Int.Type) throws -> Int {
532532

533-
let value = try self.decoder.unbox(container.value, as: Int32.self)
534-
533+
let value = try self.decoder.unboxNumeric(container.value, as: Int32.self)
535534
return Int(value)
536535
}
537536

@@ -542,23 +541,22 @@ internal struct TLVSingleValueDecodingContainer: SingleValueDecodingContainer {
542541

543542
func decode(_ type: Int16.Type) throws -> Int16 {
544543

545-
return try self.decoder.unbox(container.value, as: type)
544+
return try self.decoder.unboxNumeric(container.value, as: type)
546545
}
547546

548547
func decode(_ type: Int32.Type) throws -> Int32 {
549548

550-
return try self.decoder.unbox(container.value, as: type)
549+
return try self.decoder.unboxNumeric(container.value, as: type)
551550
}
552551

553552
func decode(_ type: Int64.Type) throws -> Int64 {
554553

555-
return try self.decoder.unbox(container.value, as: type)
554+
return try self.decoder.unboxNumeric(container.value, as: type)
556555
}
557556

558557
func decode(_ type: UInt.Type) throws -> UInt {
559558

560-
let value = try self.decoder.unbox(container.value, as: UInt32.self)
561-
559+
let value = try self.decoder.unboxNumeric(container.value, as: UInt32.self)
562560
return UInt(value)
563561
}
564562

@@ -569,27 +567,29 @@ internal struct TLVSingleValueDecodingContainer: SingleValueDecodingContainer {
569567

570568
func decode(_ type: UInt16.Type) throws -> UInt16 {
571569

572-
return try self.decoder.unbox(container.value, as: type)
570+
return try self.decoder.unboxNumeric(container.value, as: type)
573571
}
574572

575573
func decode(_ type: UInt32.Type) throws -> UInt32 {
576574

577-
return try self.decoder.unbox(container.value, as: type)
575+
return try self.decoder.unboxNumeric(container.value, as: type)
578576
}
579577

580578
func decode(_ type: UInt64.Type) throws -> UInt64 {
581579

582-
return try self.decoder.unbox(container.value, as: type)
580+
return try self.decoder.unboxNumeric(container.value, as: type)
583581
}
584582

585583
func decode(_ type: Float.Type) throws -> Float {
586584

587-
fatalError()
585+
let value = try self.decoder.unboxNumeric(container.value, as: UInt32.self)
586+
return Float(bitPattern: value)
588587
}
589588

590589
func decode(_ type: Double.Type) throws -> Double {
591590

592-
fatalError()
591+
let value = try self.decoder.unboxNumeric(container.value, as: UInt64.self)
592+
return Double(bitPattern: value)
593593
}
594594

595595
func decode(_ type: String.Type) throws -> String {
@@ -636,7 +636,7 @@ internal struct TLVUnkeyedDecodingContainer: UnkeyedDecodingContainer {
636636
return _count
637637
}
638638

639-
var _count: Int {
639+
private var _count: Int {
640640
return container.count
641641
}
642642

Sources/Encoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal extension TLVEncoder {
9191

9292
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
9393

94-
log?("Requested container keyed by \(type) for path \"\(codingPath.path)\"")
94+
log?("Requested container keyed by \(type.sanitizedName) for path \"\(codingPath.path)\"")
9595

9696
let stackContainer = ItemsContainer()
9797
self.stack.push(.items(stackContainer))

0 commit comments

Comments
 (0)