Search in sources :

Example 1 with SchemaException

use of com.squareup.wire.schema.SchemaException 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 2 with SchemaException

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

the class ProfileLoaderTest method missingImport.

@Test
public void missingImport() throws Exception {
    RepoBuilder repoBuilder = new RepoBuilder().add("a/b/message.proto", "" + "package a.b;\n" + "message Message {\n" + "}\n").add("a/b/android.wire", "" + "syntax = \"wire2\";\n" + "type a.b.Message {\n" + "  target java.lang.Object using com.example.Message#OBJECT_ADAPTER;\n" + "}\n");
    try {
        repoBuilder.profile("android");
        fail();
    } catch (SchemaException expected) {
        assertThat(expected).hasMessage("a/b/android.wire needs to import a/b/message.proto (/source/a/b/android.wire at 2:1)");
    }
}
Also used : SchemaException(com.squareup.wire.schema.SchemaException) RepoBuilder(com.squareup.wire.schema.RepoBuilder) Test(org.junit.Test)

Example 3 with SchemaException

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

the class ProfileLoaderTest method unknownType.

@Test
public void unknownType() throws Exception {
    RepoBuilder repoBuilder = new RepoBuilder().add("a/b/message.proto", "" + "package a.b;\n" + "message Message {\n" + "}\n").add("a/b/android.wire", "" + "syntax = \"wire2\";\n" + "type a.b.Message2 {\n" + "  target java.lang.Object using com.example.Message#OBJECT_ADAPTER;\n" + "}\n");
    try {
        repoBuilder.profile("android");
        fail();
    } catch (SchemaException expected) {
        assertThat(expected).hasMessage("unable to resolve a.b.Message2 (/source/a/b/android.wire at 2:1)");
    }
}
Also used : SchemaException(com.squareup.wire.schema.SchemaException) RepoBuilder(com.squareup.wire.schema.RepoBuilder) Test(org.junit.Test)

Aggregations

SchemaException (com.squareup.wire.schema.SchemaException)3 RepoBuilder (com.squareup.wire.schema.RepoBuilder)2 Test (org.junit.Test)2 ProfileFileElement (com.squareup.wire.java.internal.ProfileFileElement)1 TypeConfigElement (com.squareup.wire.java.internal.TypeConfigElement)1 ProtoType (com.squareup.wire.schema.ProtoType)1 Type (com.squareup.wire.schema.Type)1 ArrayList (java.util.ArrayList)1