Skip to content

Add support for non-primitive Booleans. #2237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public ObjectEncoderContext add(@NonNull FieldDescriptor field, @Nullable Object
return add(field, ((Number) obj).longValue());
}

if (obj instanceof Boolean) {
return add(field, (boolean) obj);
}

if (obj instanceof byte[]) {
byte[] bytes = (byte[]) obj;
if (bytes.length == 0) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@
@RunWith(JUnit4.class)
public class OtherTypesEncodingTests {
@Test
public void test() {
public void encode_withDefaults() {
assertThat(new OtherTypes().encode()).isEmpty();
}

@Test
public void test2() throws InvalidProtocolBufferException {
public void encode_withNonDefaultValues() throws InvalidProtocolBufferException {
byte[] result =
new OtherTypes("hello", "world".getBytes(Charset.forName("UTF-8")), true).encode();
new OtherTypes("hello", "world".getBytes(Charset.forName("UTF-8")), true, true).encode();
OtherTypesProto parsed = OtherTypesProto.parseFrom(result);
assertThat(parsed)
.isEqualTo(
OtherTypesProto.newBuilder()
.setStr("hello")
.setBts(ByteString.copyFrom("world", Charset.forName("UTF-8")))
.setBl(true)
.setWrappedBool(true)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void test2() throws InvalidProtocolBufferException {
.put("value", new Fixed(1, 2, 3, 4))
.build(),
ImmutableList.of(
new OtherTypes("hello", new byte[0], false),
new OtherTypes("", new byte[] {42}, true)))
new OtherTypes("hello", new byte[0], false, true),
new OtherTypes("", new byte[] {42}, true, false)))
.encode();

WithCollectionsProto parsed = WithCollectionsProto.parseFrom(result);
Expand All @@ -66,7 +66,8 @@ public void test2() throws InvalidProtocolBufferException {
.putMyMap(
"value",
FixedProto.newBuilder().setF32(1).setSf32(2).setF64(3).setSf64(4).build())
.addOtherTypes(OtherTypesProto.newBuilder().setStr("hello").build())
.addOtherTypes(
OtherTypesProto.newBuilder().setStr("hello").setWrappedBool(true).build())
.addOtherTypes(
OtherTypesProto.newBuilder()
.setBts(ByteString.copyFrom(new byte[] {42}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ public class OtherTypes {

private final String str;
private final byte[] bytes;
private final boolean bl;
private final boolean bool;
private final Boolean wrappedBool;

public OtherTypes(String str, byte[] bytes, boolean bl) {
public OtherTypes(String str, byte[] bytes, boolean bool, Boolean wrappedBool) {
this.str = str;
this.bytes = bytes;
this.bl = bl;
this.bool = bool;
this.wrappedBool = wrappedBool;
}

public OtherTypes() {
this("", new byte[0], false);
this("", new byte[0], false, null);
}

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

@Protobuf(tag = 3)
public boolean isBl() {
return bl;
public boolean isBool() {
return bool;
}

@Protobuf(tag = 4)
public Boolean getWrappedBool() {
return wrappedBool;
}

public byte[] encode() {
Expand Down
1 change: 1 addition & 0 deletions encoders/firebase-encoders-proto/src/test/proto/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ message OtherTypesProto {
string str = 1;
bytes bts = 2;
bool bl = 3;
bool wrappedBool = 4;
}

message WithCollectionsProto {
Expand Down