Skip to content
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 @@ -27,7 +27,7 @@ public LightProtoBooleanField(ProtoFieldDescriptor field, int index) {
public void getter(PrintWriter w) {
w.format(" /** Returns the value of the {@code %s} field. */\n", field.getName());
w.format(" public %s %s() {\n", field.getJavaType(), Util.camelCase("is", ccName));
if (!field.hasImplicitPresence() && !field.isDefaultValueSet()) {
if (field.isRequired()) {
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" }\n");
Expand All @@ -40,7 +40,7 @@ public void getter(PrintWriter w) {
public void clear(PrintWriter w) {
if (field.isDefaultValueSet()) {
w.format("%s = %s;\n", ccName, field.getDefaultValueAsString());
} else if (field.hasImplicitPresence()) {
} else {
w.format("%s = false;\n", ccName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ public void setter(PrintWriter w, String enclosingType) {
public void getter(PrintWriter w) {
w.format("/** Returns the size in bytes of the {@code %s} field. */\n", field.getName());
w.format("public int %s() {\n", Util.camelCase("get", ccName, "size"));
if (field.hasImplicitPresence()) {
w.format(" if (_%sLen < 0) { return 0; }\n", ccName);
} else {
if (field.isRequired()) {
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" }\n");
} else {
w.format(" if (_%sLen < 0) { return 0; }\n", ccName);
}
w.format(" return _%sLen;\n", ccName);
w.format("}\n");

w.format("/** Returns the {@code %s} field as a byte array. */\n", field.getName());
w.format("public byte[] %s() {\n", Util.camelCase("get", ccName));
if (field.hasImplicitPresence()) {
if (!field.isRequired()) {
w.format(" if (_%sLen < 0) { return new byte[0]; }\n", ccName);
}
w.format(" io.netty.buffer.ByteBuf _b = %s();\n", Util.camelCase("get", ccName, "slice"));
Expand All @@ -88,12 +88,12 @@ public void getter(PrintWriter w) {

w.format("/** Returns the {@code %s} field as a ByteBuf slice. */\n", field.getName());
w.format("public io.netty.buffer.ByteBuf %s() {\n", Util.camelCase("get", ccName, "slice"));
if (field.hasImplicitPresence()) {
w.format(" if (_%sLen < 0) { return io.netty.buffer.Unpooled.EMPTY_BUFFER; }\n", ccName);
} else {
if (field.isRequired()) {
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" }\n");
} else {
w.format(" if (_%sLen < 0) { return io.netty.buffer.Unpooled.EMPTY_BUFFER; }\n", ccName);
}
w.format(" if (%s == null) {\n", ccName);
w.format(" return _parsedBuffer.slice(_%sIdx, _%sLen);\n", ccName, ccName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ public LightProtoEnumField(ProtoFieldDescriptor field, int index) {

@Override
public void declaration(PrintWriter w) {
if (field.hasImplicitPresence()) {
w.format("private %s %s = %s.valueOf(0);\n", field.getJavaType(), ccName, field.getJavaType());
} else {
if (field.isDefaultValueSet()) {
super.declaration(w);
} else {
w.format("private %s %s = %s.valueOf(0);\n", field.getJavaType(), ccName, field.getJavaType());
}
}

@Override
public void getter(PrintWriter w) {
w.format(" /** Returns the value of the {@code %s} field. */\n", field.getName());
w.format(" public %s %s() {\n", field.getJavaType(), Util.camelCase("get", field.getName()));
if (!field.hasImplicitPresence() && !field.isDefaultValueSet()) {
if (field.isRequired()) {
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" }\n");
Expand All @@ -47,10 +47,10 @@ public void getter(PrintWriter w) {

@Override
public void clear(PrintWriter w) {
if (field.hasImplicitPresence()) {
w.format("%s = %s.valueOf(0);\n", ccName, field.getJavaType());
if (field.isDefaultValueSet()) {
w.format("%s = %s;\n", ccName, field.getDefaultValueAsString());
} else {
super.clear(w);
w.format("%s = %s.valueOf(0);\n", ccName, field.getJavaType());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ public void copy(PrintWriter w) {
public void getter(PrintWriter w) {
w.format("/** Returns the value of the {@code %s} field. */\n", field.getName());
w.format("public %s %s() {\n", field.getJavaType(), Util.camelCase("get", field.getName()));
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" }\n");
if (field.isRequired()) {
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" }\n");
} else {
w.format(" if (%s == null) {\n", ccName);
w.format(" %s = new %s();\n", ccName, field.getJavaType());
w.format(" }\n");
}
w.format(" return %s;\n", ccName);
w.format("}\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void tags(PrintWriter w) {
public void getter(PrintWriter w) {
w.format(" /** Returns the value of the {@code %s} field. */\n", field.getName());
w.format(" public %s %s() {\n", field.getJavaType(), Util.camelCase("get", field.getName()));
if (!field.hasImplicitPresence() && !field.isDefaultValueSet()) {
if (field.isRequired()) {
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" }\n");
Expand Down Expand Up @@ -218,7 +218,7 @@ public void copy(PrintWriter w) {
public void clear(PrintWriter w) {
if (field.isDefaultValueSet()) {
w.format("%s = %s;\n", ccName, field.getDefaultValueAsString());
} else if (field.hasImplicitPresence()) {
} else {
w.format("%s = 0;\n", ccName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ public void copy(PrintWriter w) {
public void getter(PrintWriter w) {
w.format("/** Returns the value of the {@code %s} field. */\n", field.getName());
w.format("public %s %s() {\n", field.getJavaType(), Util.camelCase("get", field.getName()));
if (field.hasImplicitPresence()) {
if (field.isRequired()) {
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" }\n");
} else if (field.hasImplicitPresence()) {
w.format(" if (_%sBufferLen < 0) {\n", ccName);
w.format(" return \"\";\n");
w.format(" }\n");
} else if (!field.isDefaultValueSet()) {
w.format(" if (!%s()) {\n", Util.camelCase("has", ccName));
w.format(" throw new IllegalStateException(\"Field '%s' is not set\");\n", field.getName());
w.format(" return \"\";\n");
w.format(" }\n");
}
w.format(" if (%s == null) {\n", camelCase(field.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ public void testBytesBuf() throws Exception {
assertArrayEquals(b1, b2);
}

@Test
public void testAccessUnsetOptionalBytes() {
B lpb = new B();
assertFalse(lpb.hasPayload());

// Accessing unset optional bytes should return empty defaults, not throw
assertArrayEquals(new byte[0], lpb.getPayload());
assertEquals(0, lpb.getPayloadSize());
assertEquals(0, lpb.getPayloadSlice().readableBytes());
}

@Test
public void testClearResetsOptionalBytesToDefault() {
B lpb = new B();
lpb.setPayload(new byte[]{1, 2, 3});

assertTrue(lpb.hasPayload());
assertEquals(3, lpb.getPayloadSize());

lpb.clear();

assertFalse(lpb.hasPayload());
assertArrayEquals(new byte[0], lpb.getPayload());
assertEquals(0, lpb.getPayloadSize());
assertEquals(0, lpb.getPayloadSlice().readableBytes());
assertEquals(0, lpb.getSerializedSize());
}

@Test
public void testRepeatedBytes() throws Exception {
B lpb = new B();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,72 @@ public void testClearNestedMessage() throws Exception {
assertFalse(m.getX().hasB());
}

@Test
public void testAccessUnsetOptionalMessageReturnsDefault() {
M m = new M();
assertFalse(m.hasX());

// Accessing an unset optional message field should return an empty instance, not throw
X x = m.getX();
assertNotNull(x);

// The sub-message fields should also return defaults
assertFalse(x.hasA());
assertFalse(x.hasB());
assertEquals("", x.getA());
assertEquals("", x.getB());

// has() on the parent should still be false
assertFalse(m.hasX());

// Serialized size should be 0 (no fields set)
assertEquals(0, m.getSerializedSize());
}

@Test
public void testAccessUnsetNestedOptionalMessage() {
M m = new M();

// Add an item (with required k,v) but don't set the optional xx sub-message
m.addItem().setK("k1").setV("v1");

M.KV kv = m.getItemAt(0);
assertFalse(kv.hasXx());

// Accessing the unset optional nested message should return an empty instance
M.KV.XX xx = kv.getXx();
assertNotNull(xx);
assertEquals(0, xx.getN());
}

@Test
public void testClearResetsSubMessageFields() throws Exception {
M m = new M();
m.setX().setA("hello").setB("world");
m.addItem().setK("k1").setV("v1").setXx().setN(42);

assertTrue(m.hasX());
assertEquals("hello", m.getX().getA());
assertEquals(1, m.getItemsCount());

m.clear();

// After clear, optional message field should not be set
assertFalse(m.hasX());

// Accessing after clear should return an empty default instance
X x = m.getX();
assertNotNull(x);
assertEquals("", x.getA());
assertEquals("", x.getB());

// Repeated fields should be empty
assertEquals(0, m.getItemsCount());

// Serialized size should be 0
assertEquals(0, m.getSerializedSize());
}

@Test
public void testByteArrays() throws Exception {
M lp1 = new M();
Expand Down
Loading
Loading