use of com.squareup.wire.schema.ProtoType 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"));
}
use of com.squareup.wire.schema.ProtoType 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.ProtoType in project wire by square.
the class JavaGenerator method putAll.
private static void putAll(Map<ProtoType, ClassName> wireToJava, String javaPackage, ClassName enclosingClassName, List<Type> types) {
for (Type type : types) {
ClassName className = enclosingClassName != null ? enclosingClassName.nestedClass(type.type().simpleName()) : ClassName.get(javaPackage, type.type().simpleName());
wireToJava.put(type.type(), className);
putAll(wireToJava, javaPackage, className, type.nestedTypes());
}
}
use of com.squareup.wire.schema.ProtoType in project wire by square.
the class ProfileLoader method validate.
/** Confirms that {@code protoFiles} link correctly against {@code schema}. */
void validate(Schema schema, ImmutableList<ProfileFileElement> profileFiles) {
List<String> errors = new ArrayList<>();
for (ProfileFileElement profileFile : profileFiles) {
for (TypeConfigElement typeConfig : profileFile.typeConfigs()) {
ProtoType type = importedType(ProtoType.get(typeConfig.type()));
if (type == null)
continue;
Type resolvedType = schema.getType(type);
if (resolvedType == null) {
errors.add(String.format("unable to resolve %s (%s)", type, typeConfig.location()));
continue;
}
String requiredImport = resolvedType.location().path();
if (!profileFile.imports().contains(requiredImport)) {
errors.add(String.format("%s needs to import %s (%s)", typeConfig.location().path(), requiredImport, typeConfig.location()));
}
}
}
if (!errors.isEmpty()) {
throw new SchemaException(errors);
}
}
use of com.squareup.wire.schema.ProtoType in project wire by square.
the class ServiceGenerator method api.
public TypeSpec api(Service service) {
ClassName apiName = (ClassName) javaGenerator.typeName(service.type());
TypeSpec.Builder typeBuilder = TypeSpec.interfaceBuilder(apiName.simpleName());
typeBuilder.addModifiers(PUBLIC);
if (!service.documentation().isEmpty()) {
typeBuilder.addJavadoc("$L\n", service.documentation());
}
for (Rpc rpc : service.rpcs()) {
ProtoType requestType = rpc.requestType();
TypeName requestJavaType = javaGenerator.typeName(requestType);
ProtoType responseType = rpc.responseType();
TypeName responseJavaType = javaGenerator.typeName(responseType);
MethodSpec.Builder rpcBuilder = MethodSpec.methodBuilder(rpc.name());
rpcBuilder.addModifiers(PUBLIC, ABSTRACT);
rpcBuilder.returns(responseJavaType);
rpcBuilder.addParameter(requestJavaType, "request");
if (!rpc.documentation().isEmpty()) {
rpcBuilder.addJavadoc("$L\n", rpc.documentation());
}
typeBuilder.addMethod(rpcBuilder.build());
}
return typeBuilder.build();
}
Aggregations