Search in sources :

Example 1 with ProfileFileElement

use of com.squareup.wire.java.internal.ProfileFileElement in project wire by square.

the class ProfileLoader method load.

public Profile load() throws IOException {
    Set<Location> protoLocations = new LinkedHashSet<>();
    for (ProtoFile protoFile : schema.protoFiles()) {
        protoLocations.add(protoFile.location());
    }
    Multimap<Path, String> pathsToAttempt = pathsToAttempt(protoLocations);
    ImmutableList<ProfileFileElement> profileFiles = loadProfileFiles(pathsToAttempt);
    Profile profile = new Profile(profileFiles);
    validate(schema, profileFiles);
    return profile;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Path(java.nio.file.Path) ProfileFileElement(com.squareup.wire.java.internal.ProfileFileElement) ProtoFile(com.squareup.wire.schema.ProtoFile) Location(com.squareup.wire.schema.Location)

Example 2 with ProfileFileElement

use of com.squareup.wire.java.internal.ProfileFileElement 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 3 with ProfileFileElement

use of com.squareup.wire.java.internal.ProfileFileElement in project wire by square.

the class ProfileLoader method loadProfileFiles.

private ImmutableList<ProfileFileElement> loadProfileFiles(Multimap<Path, String> pathsToAttempt) throws IOException {
    ImmutableList.Builder<ProfileFileElement> result = ImmutableList.builder();
    try (Closer closer = Closer.create()) {
        for (Map.Entry<Path, Collection<String>> entry : pathsToAttempt.asMap().entrySet()) {
            Path base = entry.getKey();
            if (Files.isRegularFile(base)) {
                FileSystem sourceFs = FileSystems.newFileSystem(base, getClass().getClassLoader());
                closer.register(sourceFs);
                base = getOnlyElement(sourceFs.getRootDirectories());
            }
            for (String path : entry.getValue()) {
                ProfileFileElement element = loadProfileFile(base, path);
                if (element != null)
                    result.add(element);
            }
        }
    }
    return result.build();
}
Also used : Closer(com.google.common.io.Closer) Path(java.nio.file.Path) ProfileFileElement(com.squareup.wire.java.internal.ProfileFileElement) ImmutableList(com.google.common.collect.ImmutableList) FileSystem(java.nio.file.FileSystem) Collection(java.util.Collection) Map(java.util.Map)

Aggregations

ProfileFileElement (com.squareup.wire.java.internal.ProfileFileElement)3 Path (java.nio.file.Path)2 ImmutableList (com.google.common.collect.ImmutableList)1 Closer (com.google.common.io.Closer)1 TypeConfigElement (com.squareup.wire.java.internal.TypeConfigElement)1 Location (com.squareup.wire.schema.Location)1 ProtoFile (com.squareup.wire.schema.ProtoFile)1 ProtoType (com.squareup.wire.schema.ProtoType)1 SchemaException (com.squareup.wire.schema.SchemaException)1 Type (com.squareup.wire.schema.Type)1 FileSystem (java.nio.file.FileSystem)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1