Skip to content

Commit 0e93d22

Browse files
authored
Add bool support to the proto decoder. (#32)
1 parent 8108735 commit 0e93d22

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Tests/BinaryCodableTests/protobufs/Message.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct Message: ProtoDecodable {
2626
var sint64Value: Int64?
2727
var fixed32Value: UInt32?
2828
var fixed64Value: UInt64?
29+
var boolValue: Bool?
2930
var missingValue: Int32?
3031

3132
static func fieldDescriptor(for key: CodingKey) -> Field? {
@@ -43,6 +44,7 @@ struct Message: ProtoDecodable {
4344
case .sint64Value: return Field(number: 8, type: .sint64)
4445
case .fixed32Value: return Field(number: 9, type: .fixed32)
4546
case .fixed64Value: return Field(number: 10, type: .fixed64)
47+
case .boolValue: return Field(number: 13, type: .bool)
4648
case .missingValue: return Field(number: 20, type: .int32)
4749
}
4850
}

Tests/BinaryCodableTests/protobufs/ProtoDecoderSupport.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct Field {
2828
case sint64
2929
case fixed32
3030
case fixed64
31+
case bool
3132
}
3233
let type: FieldType
3334
}
@@ -103,7 +104,7 @@ private struct ProtoKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContain
103104
}
104105

105106
func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
106-
preconditionFailure("Unimplemented")
107+
return try decodeFixedWidthInteger(UInt8.self, forKey: key) != 0
107108
}
108109

109110
func decode(_ type: String.Type, forKey key: Key) throws -> String {
@@ -197,7 +198,8 @@ private struct ProtoKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContain
197198
case (.int32, .varint(let rawValue)),
198199
(.int64, .varint(let rawValue)),
199200
(.uint32, .varint(let rawValue)),
200-
(.uint64, .varint(let rawValue)): return T.init(clamping: rawValue)
201+
(.uint64, .varint(let rawValue)),
202+
(.bool, .varint(let rawValue)): return T.init(clamping: rawValue)
201203
case (.sint32, .varint(let rawValue)): return T.init(clamping: Int32(rawValue >> 1) ^ -Int32(rawValue & 1))
202204
case (.sint64, .varint(let rawValue)): return T.init(clamping: Int64(rawValue >> 1) ^ -Int64(rawValue & 1))
203205
case (.fixed32, .fixed32(let rawValue)): return T.init(clamping: rawValue)

Tests/BinaryCodableTests/protobufs/ProtobufTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ class ProtobufTests: XCTestCase {
586586
sint32 sint64_value = 8;
587587
fixed32 fixed32_value = 9;
588588
fixed64 fixed64_value = 10;
589+
bool bool_value = 13;
589590
int32 missing_value = 20;
590591
}
591592
""", message: "value", content: """
@@ -599,6 +600,7 @@ class ProtobufTests: XCTestCase {
599600
sint64_value: 268435456
600601
fixed32_value: \(UInt32.max)
601602
fixed64_value: \(UInt64.max)
603+
bool_value: true
602604
""")
603605
let decoder = ProtoDecoder()
604606

@@ -616,6 +618,10 @@ class ProtobufTests: XCTestCase {
616618
XCTAssertEqual(message.sint64Value, 268435456)
617619
XCTAssertEqual(message.fixed32Value, UInt32.max)
618620
XCTAssertEqual(message.fixed64Value, UInt64.max)
621+
XCTAssertNotNil(message.boolValue)
622+
if let boolValue = message.boolValue {
623+
XCTAssertTrue(boolValue)
624+
}
619625
XCTAssertNil(message.missingValue)
620626
} catch let error {
621627
XCTFail(String(describing: error))

0 commit comments

Comments
 (0)