Skip to content

Commit

Permalink
Merge pull request square#625 from square/jwilson.0805.fix_problems_w…
Browse files Browse the repository at this point in the history
…ith_abstract_adapters

Fix AbstractAdapters for field order and missing enums.
  • Loading branch information
JakeWharton authored Aug 5, 2016
2 parents 5c8ca90 + acedf2e commit 8bf2df8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,15 @@ private MethodSpec messageAdapterDecode(NameAllocator nameAllocator, MessageType
result.beginControlFlow("try");
result.addCode(decodeAndAssign(field, nameAllocator, useBuilder));
result.addCode(";\n");
result.nextControlFlow("catch ($T e)", EnumConstantNotFoundException.class);
result.addStatement("builder.addUnknownField(tag, $T.VARINT, (long) e.value)",
FieldEncoding.class);
result.endControlFlow(); // try/catch
if (useBuilder) {
result.nextControlFlow("catch ($T e)", EnumConstantNotFoundException.class);
result.addStatement("builder.addUnknownField(tag, $T.VARINT, (long) e.value)",
FieldEncoding.class);
result.endControlFlow(); // try/catch
} else {
result.nextControlFlow("catch ($T ignored)", EnumConstantNotFoundException.class);
result.endControlFlow(); // try/catch
}
result.addStatement("break");
result.endControlFlow(); // case
} else {
Expand Down Expand Up @@ -814,7 +819,7 @@ private MethodSpec messageAdapterDecode(NameAllocator nameAllocator, MessageType
} else {
result.addCode("return fromProto(");
boolean first = true;
for (Field field : fields) {
for (Field field : type.fieldsAndOneOfFields()) {
if (!first) result.addCode(", ");
result.addCode("$N", nameAllocator.get(field));
first = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,20 @@ public final class JavaGeneratorTest {
+ "import \"foo.proto\";\n"
+ "message ProtoMessage {\n"
+ " optional foo.proto.Foo field = 1;\n"
+ " map<string, foo.proto.Bar> bars = 2;\n"
+ " repeated int32 numbers = 3;\n"
+ " optional foo.proto.CoinFlip coin_flip = 4;\n"
+ " map<string, foo.proto.Bar> bars = 2;\n"
+ "}\n")
.add("foo.proto", ""
+ "package foo.proto;\n"
+ "option java_package = \"foo.java\";\n"
+ "message Foo {\n"
+ "}\n"
+ "message Bar {\n"
+ "}\n"
+ "enum CoinFlip {\n"
+ " HEADS = 1;\n"
+ " TAILS = 2;\n"
+ "}\n")
.add("android.wire", ""
+ "syntax = \"wire2\";\n"
Expand Down Expand Up @@ -111,6 +116,7 @@ public final class JavaGeneratorTest {
+ "import com.squareup.wire.ProtoWriter;\n"
+ "import com.squareup.wire.internal.Internal;\n"
+ "import foo.java.Bar;\n"
+ "import foo.java.CoinFlip;\n"
+ "import foo.java.Foo;\n"
+ "import java.io.IOException;\n"
+ "import java.lang.Integer;\n"
Expand All @@ -129,44 +135,56 @@ public final class JavaGeneratorTest {
+ "\n"
+ " public abstract Foo field(JavaMessage value);\n"
+ "\n"
+ " public abstract Map<String, Bar> bars(JavaMessage value);\n"
+ "\n"
+ " public abstract List<Integer> numbers(JavaMessage value);\n"
+ "\n"
+ " public abstract JavaMessage fromProto(Foo field, Map<String, Bar> bars, List<Integer> numbers);\n"
+ " public abstract CoinFlip coin_flip(JavaMessage value);\n"
+ "\n"
+ " public abstract Map<String, Bar> bars(JavaMessage value);\n"
+ "\n"
+ " public abstract JavaMessage fromProto(Foo field, List<Integer> numbers, CoinFlip coin_flip, Map<String, Bar> bars);\n"
+ "\n"
+ " @Override\n"
+ " public int encodedSize(JavaMessage value) {\n"
+ " return Foo.ADAPTER.encodedSizeWithTag(1, field(value))\n"
+ " + bars.encodedSizeWithTag(2, bars(value))\n"
+ " + ProtoAdapter.INT32.asRepeated().encodedSizeWithTag(3, numbers(value));\n"
+ " + ProtoAdapter.INT32.asRepeated().encodedSizeWithTag(3, numbers(value))\n"
+ " + CoinFlip.ADAPTER.encodedSizeWithTag(4, coin_flip(value))\n"
+ " + bars.encodedSizeWithTag(2, bars(value));\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public void encode(ProtoWriter writer, JavaMessage value) throws IOException {\n"
+ " Foo.ADAPTER.encodeWithTag(writer, 1, field(value));\n"
+ " bars.encodeWithTag(writer, 2, bars(value));\n"
+ " ProtoAdapter.INT32.asRepeated().encodeWithTag(writer, 3, numbers(value));\n"
+ " CoinFlip.ADAPTER.encodeWithTag(writer, 4, coin_flip(value));\n"
+ " bars.encodeWithTag(writer, 2, bars(value));\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public JavaMessage decode(ProtoReader reader) throws IOException {\n"
+ " Foo field = null;\n"
+ " Map<String, Bar> bars = Internal.newMutableMap();\n"
+ " List<Integer> numbers = Internal.newMutableList();\n"
+ " CoinFlip coin_flip = null;\n"
+ " long token = reader.beginMessage();\n"
+ " for (int tag; (tag = reader.nextTag()) != -1;) {\n"
+ " switch (tag) {\n"
+ " case 1: field = Foo.ADAPTER.decode(reader); break;\n"
+ " case 2: bars.putAll(bars.decode(reader)); break;\n"
+ " case 3: numbers.add(ProtoAdapter.INT32.decode(reader)); break;\n"
+ " case 4: {\n"
+ " try {\n"
+ " coin_flip = CoinFlip.ADAPTER.decode(reader);\n"
+ " } catch (ProtoAdapter.EnumConstantNotFoundException ignored) {\n"
+ " }\n"
+ " break;\n"
+ " }\n"
+ " default: {\n"
+ " reader.skip();\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " reader.endMessage(token);\n"
+ " return fromProto(field, bars, numbers);\n"
+ " return fromProto(field, numbers, coin_flip, bars);\n"
+ " }\n"
+ "\n"
+ " @Override\n"
Expand Down

0 comments on commit 8bf2df8

Please sign in to comment.