Search in sources :

Example 1 with Service

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

the class JavaGenerator method get.

public static JavaGenerator get(Schema schema) {
    Map<ProtoType, ClassName> nameToJavaName = new LinkedHashMap<>();
    nameToJavaName.putAll(BUILT_IN_TYPES_MAP);
    for (ProtoFile protoFile : schema.protoFiles()) {
        String javaPackage = javaPackage(protoFile);
        putAll(nameToJavaName, javaPackage, null, protoFile.types());
        for (Service service : protoFile.services()) {
            ClassName className = ClassName.get(javaPackage, service.type().simpleName());
            nameToJavaName.put(service.type(), className);
        }
    }
    return new JavaGenerator(schema, nameToJavaName, new Profile(), false, false);
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) ClassName(com.squareup.javapoet.ClassName) ProtoFile(com.squareup.wire.schema.ProtoFile) Service(com.squareup.wire.schema.Service) ByteString(okio.ByteString) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Service

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

the class ServiceGeneratorTest method service.

@Test
public void service() throws IOException {
    Schema schema = schema(ImmutableMap.of("sample.proto", "" + "syntax = \"proto2\";\n" + "package squareup.wire.sample;\n" + "\n" + "message SampleMessage {\n" + "  repeated string array = 1;\n" + "}\n" + "\n" + "message SampleRequest {\n" + "  optional string name = 1;\n" + "  optional SampleMessage sample_message = 2;\n" + "}\n" + "\n" + "message SampleResponse {\n" + "  optional int32 age = 1;\n" + "}\n" + "\n" + "// This is it. A really fantastic service interface.\n" + "service SampleApi {\n" + "  // Call this RPC. You'll be glad you did!\n" + "  rpc FirstRpc (SampleRequest) returns (SampleResponse);\n" + "  rpc OtherOne (SampleRequest) returns (SampleResponse);\n" + "}\n"));
    Service service = schema.getService("squareup.wire.sample.SampleApi");
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    ServiceGenerator generator = new ServiceGenerator(javaGenerator);
    TypeSpec typeSpec = generator.api(service);
    assertThat(toString(typeSpec)).isEqualTo("" + "package squareup.wire.sample;\n" + "\n" + "/**\n" + " * This is it. A really fantastic service interface.\n" + " */\n" + "public interface SampleApi {\n" + "  /**\n" + "   * Call this RPC. You'll be glad you did!\n" + "   */\n" + "  SampleResponse FirstRpc(SampleRequest request);\n" + "\n" + "  SampleResponse OtherOne(SampleRequest request);\n" + "}\n");
}
Also used : Schema(com.squareup.wire.schema.Schema) Service(com.squareup.wire.schema.Service) JavaGenerator(com.squareup.wire.java.JavaGenerator) TypeSpec(com.squareup.javapoet.TypeSpec) Test(org.junit.Test)

Example 3 with Service

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

the class CodegenSample method execute.

public void execute() throws IOException {
    Schema schema = loadSchema();
    if (!identifierSet.isEmpty()) {
        schema = retainRoots(schema);
    }
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    ServiceGenerator serviceGenerator = new ServiceGenerator(javaGenerator);
    for (ProtoFile protoFile : schema.protoFiles()) {
        for (Type type : protoFile.types()) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            TypeSpec typeSpec = javaGenerator.generateType(type);
            ClassName javaTypeName = (ClassName) javaGenerator.typeName(type.type());
            writeJavaFile(javaTypeName, typeSpec, type.location(), stopwatch);
        }
        for (Service service : protoFile.services()) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            ClassName javaTypeName = (ClassName) javaGenerator.typeName(service.type());
            TypeSpec typeSpec = serviceGenerator.api(service);
            writeJavaFile(javaTypeName, typeSpec, service.location(), stopwatch);
        }
    }
}
Also used : Type(com.squareup.wire.schema.Type) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Stopwatch(com.google.common.base.Stopwatch) ClassName(com.squareup.javapoet.ClassName) Service(com.squareup.wire.schema.Service) JavaGenerator(com.squareup.wire.java.JavaGenerator) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

Service (com.squareup.wire.schema.Service)3 ClassName (com.squareup.javapoet.ClassName)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 JavaGenerator (com.squareup.wire.java.JavaGenerator)2 ProtoFile (com.squareup.wire.schema.ProtoFile)2 Schema (com.squareup.wire.schema.Schema)2 Stopwatch (com.google.common.base.Stopwatch)1 ProtoType (com.squareup.wire.schema.ProtoType)1 Type (com.squareup.wire.schema.Type)1 LinkedHashMap (java.util.LinkedHashMap)1 ByteString (okio.ByteString)1 Test (org.junit.Test)1