Skip to content

Commit 95e608e

Browse files
committed
Fix encoding top-level TLVCodable values
1 parent 548e984 commit 95e608e

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

Sources/Decoder.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,27 @@ public struct TLVDecoder {
3838

3939
log?("Will decode \(String(reflecting: T.self))")
4040

41-
let items = try decode(data)
42-
43-
let options = Decoder.Options(
44-
numericFormatting: numericFormatting,
45-
uuidFormatting: uuidFormatting,
46-
dateFormatting: dateFormatting
47-
)
48-
49-
let decoder = Decoder(referencing: .items(items),
50-
userInfo: userInfo,
51-
log: log,
52-
options: options)
53-
54-
// decode from container
55-
return try T.init(from: decoder)
41+
if let tlvCodable = type as? TLVDecodable.Type {
42+
guard let value = tlvCodable.init(tlvData: data) else {
43+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Invalid data for \(String(reflecting: type))"))
44+
}
45+
return value as! T
46+
} else {
47+
let items = try decode(data)
48+
let options = Decoder.Options(
49+
numericFormatting: numericFormatting,
50+
uuidFormatting: uuidFormatting,
51+
dateFormatting: dateFormatting
52+
)
53+
let decoder = Decoder(
54+
referencing: .items(items),
55+
userInfo: userInfo,
56+
log: log,
57+
options: options
58+
)
59+
// decode from container
60+
return try T.init(from: decoder)
61+
}
5662
}
5763

5864
public func decode(_ data: Data) throws -> [TLVItem] {
@@ -311,7 +317,7 @@ internal extension TLVDecoder.Decoder {
311317
return try unboxUUID(item.value) as! T
312318
} else if type == Date.self {
313319
return try unboxDate(item.value) as! T
314-
} else if let tlvCodable = type as? TLVCodable.Type {
320+
} else if let tlvCodable = type as? TLVDecodable.Type {
315321
guard let value = tlvCodable.init(tlvData: item.value) else {
316322
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Invalid data for \(String(reflecting: type))"))
317323
}

Sources/Encoder.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,23 @@ public struct TLVEncoder {
4141

4242
log?("Will encode \(String(reflecting: T.self))")
4343

44-
let options = Encoder.Options(
45-
outputFormatting: outputFormatting,
46-
numericFormatting: numericFormatting,
47-
uuidFormatting: uuidFormatting,
48-
dateFormatting: dateFormatting
49-
)
50-
51-
let encoder = Encoder(userInfo: userInfo, log: log, options: options)
52-
try value.encode(to: encoder)
53-
assert(encoder.stack.containers.count == 1)
54-
55-
guard case let .items(container) = encoder.stack.root else {
56-
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) is not encoded as items."))
44+
if let encodable = value as? TLVEncodable {
45+
return encodable.tlvData
46+
} else {
47+
let options = Encoder.Options(
48+
outputFormatting: outputFormatting,
49+
numericFormatting: numericFormatting,
50+
uuidFormatting: uuidFormatting,
51+
dateFormatting: dateFormatting
52+
)
53+
let encoder = Encoder(userInfo: userInfo, log: log, options: options)
54+
try value.encode(to: encoder)
55+
assert(encoder.stack.containers.count == 1)
56+
guard case let .items(container) = encoder.stack.root else {
57+
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) is not encoded as items."))
58+
}
59+
return container.data
5760
}
58-
59-
return container.data
6061
}
6162

6263
public func encode(_ items: [TLVItem]) -> Data {

Tests/TLVCodingTests/TLVCodingTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ final class TLVCodingTests: XCTestCase {
7474
uint64: 30_000),
7575
Data([0, 1, 1, 1, 4, 246, 255, 255, 255, 2, 4, 10, 0, 0, 0, 3, 4, 146, 203, 143, 63, 4, 8, 114, 138, 142, 228, 242, 255, 37, 64, 5, 1, 127, 6, 2, 56, 255, 7, 4, 48, 248, 255, 255, 8, 8, 224, 177, 255, 255, 255, 255, 255, 255, 9, 1, 255, 10, 2, 44, 1, 11, 4, 184, 11, 0, 0, 12, 8, 48, 117, 0, 0, 0, 0, 0, 0]))
7676

77+
compare(
78+
Version(major: 1, minor: 2, patch: 3),
79+
Data([0x01, 0x02, 0x03])
80+
)
81+
7782
compare(
7883
CustomEncodable(
7984
data: nil,
@@ -371,9 +376,7 @@ final class TLVCodingTests: XCTestCase {
371376
)
372377

373378
XCTAssertEqual(try encoder.encode(value), try encoder.encode(valueUnordered))
374-
375379
encoder.outputFormatting.sortedKeys = false
376-
377380
XCTAssertNotEqual(try encoder.encode(value), try encoder.encode(valueUnordered))
378381
}
379382
}

0 commit comments

Comments
 (0)