Search in sources :

Example 1 with DescriptorBuilder

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

the class DescriptorSetUtils method buildDescriptorSet.

/**
 * Convenience method to build a DescriptorSet for a single proto file.
 */
public static DescriptorSet buildDescriptorSet(final String path, final String content) throws Exception {
    try (final DescriptorBuilder descriptorBuilder = DESCRIPTOR_BUILDER_FACTORY.newDescriptorBuilder()) {
        descriptorBuilder.setProtoFile(Paths.get(path), content);
        final DescriptorBuilder.Result result = descriptorBuilder.buildDescriptor(Stream.of(Paths.get(path)));
        if (result.compilationError() != null) {
            throw new RuntimeException("Protobuf compilation failed: " + result.compilationError());
        }
        checkNotNull(result.fileDescriptorSet());
        return DescriptorSet.create(result.fileDescriptorSet(), __ -> true);
    }
}
Also used : ProtocDescriptorBuilder(com.spotify.protoman.descriptor.ProtocDescriptorBuilder) DescriptorBuilder(com.spotify.protoman.descriptor.DescriptorBuilder)

Example 2 with DescriptorBuilder

use of com.spotify.protoman.descriptor.DescriptorBuilder 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)

Example 3 with DescriptorBuilder

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

the class DescriptorSetUtils method buildDescriptorSet.

public static DescriptorSet buildDescriptorSet(final Path root, final Predicate<Path> filter) throws IOException, URISyntaxException, DescriptorBuilderException {
    final ClassLoader classLoader = DescriptorSetUtils.class.getClassLoader();
    try (final DescriptorBuilder descriptorBuilder = DESCRIPTOR_BUILDER_FACTORY.newDescriptorBuilder()) {
        final ImmutableList<Path> resourceFiles = listResourceFiles(root);
        for (final Path path : resourceFiles) {
            final String strPath = root.resolve(path).toString();
            try (final InputStream is = classLoader.getResourceAsStream(strPath)) {
                final String content = CharStreams.toString(new InputStreamReader(is));
                descriptorBuilder.setProtoFile(path, content);
            }
        }
        final DescriptorBuilder.Result result = descriptorBuilder.buildDescriptor(resourceFiles.stream().filter(filter));
        if (result.compilationError() != null) {
            throw new RuntimeException("Failed to build descriptor set: " + result.compilationError());
        }
        return DescriptorSet.create(result.fileDescriptorSet(), filter);
    }
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ProtocDescriptorBuilder(com.spotify.protoman.descriptor.ProtocDescriptorBuilder) DescriptorBuilder(com.spotify.protoman.descriptor.DescriptorBuilder)

Aggregations

DescriptorBuilder (com.spotify.protoman.descriptor.DescriptorBuilder)3 ProtocDescriptorBuilder (com.spotify.protoman.descriptor.ProtocDescriptorBuilder)2 Path (java.nio.file.Path)2 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 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 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayDeque (java.util.ArrayDeque)1