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);
}
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");
}
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);
}
}
}
Aggregations