Skip to content

Commit e2535c9

Browse files
authored
Add support for non-primitive Booleans. (#2237)
1 parent f201907 commit e2535c9

File tree

6 files changed

+26
-35
lines changed

6 files changed

+26
-35
lines changed

encoders/firebase-encoders-proto/src/main/java/com/google/firebase/encoders/proto/ProtobufDataEncoderContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ public ObjectEncoderContext add(@NonNull FieldDescriptor field, @Nullable Object
141141
return add(field, ((Number) obj).longValue());
142142
}
143143

144+
if (obj instanceof Boolean) {
145+
return add(field, (boolean) obj);
146+
}
147+
144148
if (obj instanceof byte[]) {
145149
byte[] bytes = (byte[]) obj;
146150
if (bytes.length == 0) {

encoders/firebase-encoders-proto/src/test/java/com/google/firebase/encoders/proto/Encoder.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

encoders/firebase-encoders-proto/src/test/java/com/google/firebase/encoders/proto/OtherTypesEncodingTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,22 @@
2929
@RunWith(JUnit4.class)
3030
public class OtherTypesEncodingTests {
3131
@Test
32-
public void test() {
32+
public void encode_withDefaults() {
3333
assertThat(new OtherTypes().encode()).isEmpty();
3434
}
3535

3636
@Test
37-
public void test2() throws InvalidProtocolBufferException {
37+
public void encode_withNonDefaultValues() throws InvalidProtocolBufferException {
3838
byte[] result =
39-
new OtherTypes("hello", "world".getBytes(Charset.forName("UTF-8")), true).encode();
39+
new OtherTypes("hello", "world".getBytes(Charset.forName("UTF-8")), true, true).encode();
4040
OtherTypesProto parsed = OtherTypesProto.parseFrom(result);
4141
assertThat(parsed)
4242
.isEqualTo(
4343
OtherTypesProto.newBuilder()
4444
.setStr("hello")
4545
.setBts(ByteString.copyFrom("world", Charset.forName("UTF-8")))
4646
.setBl(true)
47+
.setWrappedBool(true)
4748
.build());
4849
}
4950
}

encoders/firebase-encoders-proto/src/test/java/com/google/firebase/encoders/proto/WithCollectionsTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public void test2() throws InvalidProtocolBufferException {
5252
.put("value", new Fixed(1, 2, 3, 4))
5353
.build(),
5454
ImmutableList.of(
55-
new OtherTypes("hello", new byte[0], false),
56-
new OtherTypes("", new byte[] {42}, true)))
55+
new OtherTypes("hello", new byte[0], false, true),
56+
new OtherTypes("", new byte[] {42}, true, false)))
5757
.encode();
5858

5959
WithCollectionsProto parsed = WithCollectionsProto.parseFrom(result);
@@ -66,7 +66,8 @@ public void test2() throws InvalidProtocolBufferException {
6666
.putMyMap(
6767
"value",
6868
FixedProto.newBuilder().setF32(1).setSf32(2).setF64(3).setSf64(4).build())
69-
.addOtherTypes(OtherTypesProto.newBuilder().setStr("hello").build())
69+
.addOtherTypes(
70+
OtherTypesProto.newBuilder().setStr("hello").setWrappedBool(true).build())
7071
.addOtherTypes(
7172
OtherTypesProto.newBuilder()
7273
.setBts(ByteString.copyFrom(new byte[] {42}))

encoders/firebase-encoders-proto/src/test/java/com/google/firebase/encoders/proto/pojos/OtherTypes.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ public class OtherTypes {
2525

2626
private final String str;
2727
private final byte[] bytes;
28-
private final boolean bl;
28+
private final boolean bool;
29+
private final Boolean wrappedBool;
2930

30-
public OtherTypes(String str, byte[] bytes, boolean bl) {
31+
public OtherTypes(String str, byte[] bytes, boolean bool, Boolean wrappedBool) {
3132
this.str = str;
3233
this.bytes = bytes;
33-
this.bl = bl;
34+
this.bool = bool;
35+
this.wrappedBool = wrappedBool;
3436
}
3537

3638
public OtherTypes() {
37-
this("", new byte[0], false);
39+
this("", new byte[0], false, null);
3840
}
3941

4042
@Protobuf(tag = 1)
@@ -48,8 +50,13 @@ public byte[] getBytes() {
4850
}
4951

5052
@Protobuf(tag = 3)
51-
public boolean isBl() {
52-
return bl;
53+
public boolean isBool() {
54+
return bool;
55+
}
56+
57+
@Protobuf(tag = 4)
58+
public Boolean getWrappedBool() {
59+
return wrappedBool;
5360
}
5461

5562
public byte[] encode() {

encoders/firebase-encoders-proto/src/test/proto/test.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ message OtherTypesProto {
4343
string str = 1;
4444
bytes bts = 2;
4545
bool bl = 3;
46+
bool wrappedBool = 4;
4647
}
4748

4849
message WithCollectionsProto {

0 commit comments

Comments
 (0)