Search in sources :

Example 1 with FileDescriptor

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

the class SemverSchemaVersioner method hasMinorVersionChange.

/**
 * File additions, removals and renames are considered minor version changes. As are any
 * formatting/comment changes to proto files. I.e. adding a type, adding a field, etc. are
 * considered minor version changes.
 */
private static boolean hasMinorVersionChange(final String protoPackage, final DescriptorSet current, final DescriptorSet candidate) {
    final Predicate<FileDescriptor> predicate = fileDescriptor -> Objects.equals(fileDescriptor.protoPackage(), protoPackage);
    final ImmutableMap<String, FileDescriptor> currentFileDescriptors = current.fileDescriptors().stream().filter(predicate).collect(toImmutableMap(FileDescriptor::fullName, Function.identity()));
    final ImmutableMap<String, FileDescriptor> candidateFileDescriptors = candidate.fileDescriptors().stream().filter(predicate).collect(toImmutableMap(FileDescriptor::fullName, Function.identity()));
    if (!Objects.equals(currentFileDescriptors.keySet(), candidateFileDescriptors.keySet())) {
        // .proto files were added or removed
        return true;
    }
    // descriptors. I.e. formatting changes are usually not considered minor version changes.
    return currentFileDescriptors.keySet().stream().anyMatch(key -> {
        // No files were added or removed, thus currentFd and candidateFd should always be
        // non-null
        final FileDescriptor currentFd = currentFileDescriptors.get(key);
        final FileDescriptor candidateFd = candidateFileDescriptors.get(key);
        assert currentFd != null && candidateFd != null;
        final DescriptorProtos.FileDescriptorProto currentFdProto = currentFd.toProto().toBuilder().clearSourceCodeInfo().build();
        final DescriptorProtos.FileDescriptorProto candidateFdProto = candidateFd.toProto().toBuilder().clearSourceCodeInfo().build();
        return !Objects.equals(currentFdProto, candidateFdProto);
    });
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor) Function(java.util.function.Function) Objects(java.util.Objects) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Matcher(java.util.regex.Matcher) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Pattern(java.util.regex.Pattern) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Nullable(javax.annotation.Nullable) DescriptorProtos(com.google.protobuf.DescriptorProtos) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor)

Example 2 with FileDescriptor

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

the class SemverSchemaVersioner method hasPatchVersionChange.

private static boolean hasPatchVersionChange(final String protoPackage, final DescriptorSet current, final DescriptorSet candidate) {
    final Predicate<FileDescriptor> predicate = fileDescriptor -> Objects.equals(fileDescriptor.protoPackage(), protoPackage);
    final ImmutableMap<String, FileDescriptor> currentFileDescriptors = current.fileDescriptors().stream().filter(predicate).collect(toImmutableMap(FileDescriptor::fullName, Function.identity()));
    final ImmutableMap<String, FileDescriptor> candidateFileDescriptors = candidate.fileDescriptors().stream().filter(predicate).collect(toImmutableMap(FileDescriptor::fullName, Function.identity()));
    return currentFileDescriptors.keySet().stream().anyMatch(key -> {
        final FileDescriptor currentFd = currentFileDescriptors.get(key);
        final FileDescriptor candidateFd = candidateFileDescriptors.get(key);
        assert currentFd != null && candidateFd != null;
        return !Objects.equals(currentFd.toProto(), candidateFd.toProto());
    });
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor) Function(java.util.function.Function) Objects(java.util.Objects) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Matcher(java.util.regex.Matcher) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Pattern(java.util.regex.Pattern) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Nullable(javax.annotation.Nullable) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Preconditions (com.google.common.base.Preconditions)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 DescriptorProtos (com.google.protobuf.DescriptorProtos)2 DescriptorSet (com.spotify.protoman.descriptor.DescriptorSet)2 FileDescriptor (com.spotify.protoman.descriptor.FileDescriptor)2 Objects (java.util.Objects)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Nullable (javax.annotation.Nullable)2