Search in sources :

Example 1 with RepoBuilder

use of com.squareup.wire.schema.RepoBuilder in project wire by square.

the class JavaGeneratorTest method map.

@Test
public void map() throws Exception {
    Schema schema = new RepoBuilder().add("message.proto", "" + "message Message {\n" + "  map<string, CdnResource> templates = 1;\n" + "  message CdnResource {\n" + "  }\n" + "}\n").schema();
    MessageType message = (MessageType) schema.getType("Message");
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    TypeSpec typeSpec = javaGenerator.generateType(message);
    assertThat(JavaFile.builder("", typeSpec).build().toString()).contains("" + "  @WireField(\n" + "      tag = 1,\n" + "      keyAdapter = \"com.squareup.wire.ProtoAdapter#STRING\",\n" + "      adapter = \"Message$CdnResource#ADAPTER\"\n" + "  )\n" + "  public final Map<String, CdnResource> templates;\n");
}
Also used : RepoBuilder(com.squareup.wire.schema.RepoBuilder) Schema(com.squareup.wire.schema.Schema) MessageType(com.squareup.wire.schema.MessageType) TypeSpec(com.squareup.javapoet.TypeSpec) Test(org.junit.Test)

Example 2 with RepoBuilder

use of com.squareup.wire.schema.RepoBuilder in project wire by square.

the class JavaGeneratorTest method generateAbstractAdapterWithRedactedField.

@Test
public void generateAbstractAdapterWithRedactedField() throws IOException {
    RepoBuilder repoBuilder = new RepoBuilder().add("message.proto", "" + "import \"option_redacted.proto\";\n" + "message ProtoMessage {\n" + "  optional string secret = 1 [(squareup.protos.redacted_option.redacted) = true];\n" + "}\n").add("option_redacted.proto").add("android.wire", "" + "syntax = \"wire2\";\n" + "import \"message.proto\";\n" + "type ProtoMessage {\n" + "  target JavaMessage using JavaMessage#ADAPTER;\n" + "}\n");
    assertThat(repoBuilder.generateCode("ProtoMessage", "android")).contains("" + "  @Override\n" + "  public JavaMessage redact(JavaMessage value) {\n" + "    return null;\n" + "  }\n");
}
Also used : RepoBuilder(com.squareup.wire.schema.RepoBuilder) Test(org.junit.Test)

Example 3 with RepoBuilder

use of com.squareup.wire.schema.RepoBuilder in project wire by square.

the class ProfileLoaderTest method test.

@Test
public void test() throws IOException {
    RepoBuilder repoBuilder = new RepoBuilder().add("a/b/message1.proto", "" + "package a.b;\n" + "message Message1 {\n" + "}\n").add("a/b/c/message2.proto", "" + "package a.b.c;\n" + "message Message2 {\n" + "}\n").add("android.wire", "" + "syntax = \"wire2\";\n" + "import \"a/b/message1.proto\";\n" + "type a.b.Message1 {\n" + "  target java.lang.Object using com.example.Message1#OBJECT_ADAPTER;\n" + "}\n").add("a/b/c/android.wire", "" + "syntax = \"wire2\";\n" + "import \"a/b/c/message2.proto\";\n" + "package a.b.c;\n" + "type a.b.c.Message2 {\n" + "  target java.lang.String using com.example.Message2#STRING_ADAPTER;\n" + "}\n");
    Profile profile = repoBuilder.profile("android");
    ProtoType message1 = ProtoType.get("a.b.Message1");
    assertThat(profile.getTarget(message1)).isEqualTo(ClassName.OBJECT);
    assertThat(profile.getAdapter(message1)).isEqualTo(new AdapterConstant("com.example.Message1#OBJECT_ADAPTER"));
    ProtoType message2 = ProtoType.get("a.b.c.Message2");
    assertThat(profile.getTarget(message2)).isEqualTo(ClassName.get(String.class));
    assertThat(profile.getAdapter(message2)).isEqualTo(new AdapterConstant("com.example.Message2#STRING_ADAPTER"));
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) RepoBuilder(com.squareup.wire.schema.RepoBuilder) Test(org.junit.Test)

Example 4 with RepoBuilder

use of com.squareup.wire.schema.RepoBuilder in project wire by square.

the class JavaGeneratorTest method generateTypeUsesNameAllocatorInMessageBuilderBuild.

@Test
public void generateTypeUsesNameAllocatorInMessageBuilderBuild() throws Exception {
    RepoBuilder repoBuilder = new RepoBuilder().add("message.proto", "" + "message Message {\n" + "  required float long = 1;\n" + "}\n");
    assertThat(repoBuilder.generateCode("Message")).contains("" + "    @Override\n" + "    public Message build() {\n" + "      if (long_ == null) {\n" + "        throw Internal.missingRequiredFields(long_, \"long\");\n" + "      }\n" + "      return new Message(long_, super.buildUnknownFields());\n" + "    }\n");
}
Also used : RepoBuilder(com.squareup.wire.schema.RepoBuilder) Test(org.junit.Test)

Example 5 with RepoBuilder

use of com.squareup.wire.schema.RepoBuilder in project wire by square.

the class JavaGeneratorTest method nestedAbstractAdapterIsStatic.

@Test
public void nestedAbstractAdapterIsStatic() throws IOException {
    RepoBuilder repoBuilder = new RepoBuilder().add("message.proto", "" + "message A {\n" + "  message B {\n" + "    optional string c = 1;\n" + "  }\n" + "}\n").add("android.wire", "" + "syntax = \"wire2\";\n" + "import \"message.proto\";\n" + "type A.B {\n" + "  target java.lang.String using AbAdapter#INSTANCE;\n" + "}\n");
    assertThat(repoBuilder.generateCode("A", "android")).contains("" + "  public abstract static class AbstractBAdapter extends ProtoAdapter<String> {\n");
}
Also used : RepoBuilder(com.squareup.wire.schema.RepoBuilder) Test(org.junit.Test)

Aggregations

RepoBuilder (com.squareup.wire.schema.RepoBuilder)8 Test (org.junit.Test)8 SchemaException (com.squareup.wire.schema.SchemaException)2 TypeSpec (com.squareup.javapoet.TypeSpec)1 MessageType (com.squareup.wire.schema.MessageType)1 ProtoType (com.squareup.wire.schema.ProtoType)1 Schema (com.squareup.wire.schema.Schema)1