Search in sources :

Example 1 with DescriptorBuilderException

use of com.spotify.protoman.descriptor.DescriptorBuilderException in project protoman by spotify.

the class SchemaRegistry method buildDescriptorSets.

private BuildDescriptorsResult buildDescriptorSets(final SchemaStorage.Transaction tx, final ImmutableList<SchemaFile> schemaFiles) throws DescriptorBuilderException {
    final long snapshotVersion = tx.getLatestSnapshotVersion();
    // Paths of all updated files
    final ImmutableSet<Path> updatedPaths = schemaFiles.stream().map(SchemaFile::path).collect(toImmutableSet());
    final ImmutableSet<Path> updatedAndDependencies = resolveDependencies(tx, snapshotVersion, updatedPaths);
    try (final DescriptorBuilder descriptorBuilder = descriptorBuilderFactory.newDescriptorBuilder()) {
        // Seed descriptor builder with all files from registry
        // Builder DescriptorSet for what is currently in the registry for the files being updated
        final ImmutableMap<Path, SchemaFile> currentSchemata = updatedAndDependencies.stream().map(path -> tx.schemaFile(snapshotVersion, path)).collect(toImmutableMap(SchemaFile::path, Function.identity()));
        for (SchemaFile file : currentSchemata.values()) {
            descriptorBuilder.setProtoFile(file.path(), file.content());
        }
        // NOTE(staffan): As it is right now, we need compile ALL descriptors to catch breaking
        // changes.
        // NOTE(fredrikd): Not compiling all any more, but keeping this message until
        // tests are added
        // 
        // Consider the case where the following files exists in the repository:
        // 
        // herp/derp.proto:
        // package herp;
        // message Derp {}
        // foo/bar.proto
        // package foo;
        // import "herp/derp.proto"
        // message Bar {
        // Derp derp = 1;
        // }
        // 
        // And a publish request that changes "herp/derp.proto" to:
        // package herp {}
        // message Herpaderp {} // Derp was removed/renamed!
        // 
        // 
        // That change will break "foo/bar.proto" -- BUT protoc will succeeded if we only run it
        // with foo/bar.proto as the input.
        // 
        // So, we either need to either:
        // - Run protoc will ALL files as input, or
        // - Run protoc will ALL files that DEPEND ON a file being changed as input, or
        // - Track dependencies are ensured that types in use are not removed
        final DescriptorBuilder.Result currentResult = descriptorBuilder.buildDescriptor(currentSchemata.keySet().stream());
        @Nullable final DescriptorSet currentDs = createFilteredDescriptorSet(currentResult.fileDescriptorSet(), updatedPaths);
        // Build DescriptorSet for the updated files
        for (SchemaFile schemaFile : schemaFiles) {
            descriptorBuilder.setProtoFile(schemaFile.path(), schemaFile.content());
        }
        final DescriptorBuilder.Result candidateResult = descriptorBuilder.buildDescriptor(Stream.concat(currentSchemata.keySet().stream(), schemaFiles.stream().map(SchemaFile::path)).collect(toImmutableSet()).stream());
        @Nullable final DescriptorSet candidateDs = createFilteredDescriptorSet(candidateResult.fileDescriptorSet(), updatedPaths);
        return BuildDescriptorsResult.create(currentDs, candidateDs, currentResult.compilationError(), candidateResult.compilationError());
    }
}
Also used : Path(java.nio.file.Path) DescriptorProtos(com.google.protobuf.DescriptorProtos) DescriptorBuilder(com.spotify.protoman.descriptor.DescriptorBuilder) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SchemaValidator(com.spotify.protoman.validation.SchemaValidator) Function(java.util.function.Function) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) ValidationViolation(com.spotify.protoman.validation.ValidationViolation) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) DescriptorBuilderException(com.spotify.protoman.descriptor.DescriptorBuilderException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) List(java.util.List) Stream(java.util.stream.Stream) SchemaStorage(com.spotify.protoman.registry.storage.SchemaStorage) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) DescriptorBuilder(com.spotify.protoman.descriptor.DescriptorBuilder) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Nullable(javax.annotation.Nullable)

Aggregations

AutoValue (com.google.auto.value.AutoValue)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Sets (com.google.common.collect.Sets)1 DescriptorProtos (com.google.protobuf.DescriptorProtos)1 DescriptorBuilder (com.spotify.protoman.descriptor.DescriptorBuilder)1 DescriptorBuilderException (com.spotify.protoman.descriptor.DescriptorBuilderException)1 DescriptorSet (com.spotify.protoman.descriptor.DescriptorSet)1 FileDescriptor (com.spotify.protoman.descriptor.FileDescriptor)1 SchemaStorage (com.spotify.protoman.registry.storage.SchemaStorage)1 SchemaValidator (com.spotify.protoman.validation.SchemaValidator)1 ValidationViolation (com.spotify.protoman.validation.ValidationViolation)1 Path (java.nio.file.Path)1 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1