Search in sources :

Example 1 with ProtoType

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"));
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) RepoBuilder(com.squareup.wire.schema.RepoBuilder) Test(org.junit.Test)

Example 2 with ProtoType

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);
}
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 3 with ProtoType

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());
    }
}
Also used : Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) EnclosingType(com.squareup.wire.schema.EnclosingType) EnumType(com.squareup.wire.schema.EnumType) ClassName(com.squareup.javapoet.ClassName)

Example 4 with ProtoType

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);
    }
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) SchemaException(com.squareup.wire.schema.SchemaException) ProfileFileElement(com.squareup.wire.java.internal.ProfileFileElement) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) ArrayList(java.util.ArrayList) TypeConfigElement(com.squareup.wire.java.internal.TypeConfigElement)

Example 5 with ProtoType

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();
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) TypeName(com.squareup.javapoet.TypeName) Rpc(com.squareup.wire.schema.Rpc) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ProtoType (com.squareup.wire.schema.ProtoType)7 ClassName (com.squareup.javapoet.ClassName)3 TypeName (com.squareup.javapoet.TypeName)2 Type (com.squareup.wire.schema.Type)2 LinkedHashMap (java.util.LinkedHashMap)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 ProfileFileElement (com.squareup.wire.java.internal.ProfileFileElement)1 TypeConfigElement (com.squareup.wire.java.internal.TypeConfigElement)1 EnclosingType (com.squareup.wire.schema.EnclosingType)1 EnumType (com.squareup.wire.schema.EnumType)1 MessageType (com.squareup.wire.schema.MessageType)1 ProtoFile (com.squareup.wire.schema.ProtoFile)1 RepoBuilder (com.squareup.wire.schema.RepoBuilder)1 Rpc (com.squareup.wire.schema.Rpc)1 Schema (com.squareup.wire.schema.Schema)1 SchemaException (com.squareup.wire.schema.SchemaException)1