use of com.squareup.wire.schema.RepoBuilder in project wire by square.
the class JavaGeneratorTest method generateAbstractAdapter.
@Test
public void generateAbstractAdapter() throws Exception {
RepoBuilder repoBuilder = new RepoBuilder().add("message.proto", "" + "package original.proto;\n" + "option java_package = \"original.java\";\n" + "import \"foo.proto\";\n" + "message ProtoMessage {\n" + " optional foo.proto.Foo field = 1;\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" + "import \"message.proto\";\n" + "package original.proto;\n" + "type original.proto.ProtoMessage {\n" + " target target.java.JavaMessage using target.java.JavaMessage#ADAPTER;\n" + "}\n");
assertThat(repoBuilder.generateCode("original.proto.ProtoMessage", "android")).isEqualTo("" + "package original.java;\n" + "\n" + "import com.squareup.wire.FieldEncoding;\n" + "import com.squareup.wire.ProtoAdapter;\n" + "import com.squareup.wire.ProtoReader;\n" + "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" + "import java.lang.Override;\n" + "import java.lang.String;\n" + "import java.util.List;\n" + "import java.util.Map;\n" + "import target.java.JavaMessage;\n" + "\n" + "public abstract class AbstractProtoMessageAdapter extends ProtoAdapter<JavaMessage> {\n" + " private final ProtoAdapter<Map<String, Bar>> bars = ProtoAdapter.newMapAdapter(ProtoAdapter.STRING, Bar.ADAPTER);\n" + "\n" + " public AbstractProtoMessageAdapter() {\n" + " super(FieldEncoding.LENGTH_DELIMITED, JavaMessage.class);\n" + " }\n" + "\n" + " public abstract Foo field(JavaMessage value);\n" + "\n" + " public abstract List<Integer> numbers(JavaMessage value);\n" + "\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" + " + 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" + " 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, numbers, coin_flip, bars);\n" + " }\n" + "\n" + " @Override\n" + " public JavaMessage redact(JavaMessage value) {\n" + " return value;\n" + " }\n" + "}\n");
}
use of com.squareup.wire.schema.RepoBuilder in project wire by square.
the class ProfileLoaderTest method missingImport.
@Test
public void missingImport() throws Exception {
RepoBuilder repoBuilder = new RepoBuilder().add("a/b/message.proto", "" + "package a.b;\n" + "message Message {\n" + "}\n").add("a/b/android.wire", "" + "syntax = \"wire2\";\n" + "type a.b.Message {\n" + " target java.lang.Object using com.example.Message#OBJECT_ADAPTER;\n" + "}\n");
try {
repoBuilder.profile("android");
fail();
} catch (SchemaException expected) {
assertThat(expected).hasMessage("a/b/android.wire needs to import a/b/message.proto (/source/a/b/android.wire at 2:1)");
}
}
use of com.squareup.wire.schema.RepoBuilder in project wire by square.
the class ProfileLoaderTest method unknownType.
@Test
public void unknownType() throws Exception {
RepoBuilder repoBuilder = new RepoBuilder().add("a/b/message.proto", "" + "package a.b;\n" + "message Message {\n" + "}\n").add("a/b/android.wire", "" + "syntax = \"wire2\";\n" + "type a.b.Message2 {\n" + " target java.lang.Object using com.example.Message#OBJECT_ADAPTER;\n" + "}\n");
try {
repoBuilder.profile("android");
fail();
} catch (SchemaException expected) {
assertThat(expected).hasMessage("unable to resolve a.b.Message2 (/source/a/b/android.wire at 2:1)");
}
}
Aggregations