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());
}
}
Aggregations