Search in sources :

Example 1 with DescriptorSet

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

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

Example 3 with DescriptorSet

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

the class EnumDefaultValueTest method testAllowedName.

@Parameters(method = "allowedNames")
@Test
public void testAllowedName(final String name) throws Exception {
    final DescriptorSet candidate = DescriptorSetUtils.buildDescriptorSet("a.proto", String.format(TEMPLATE, name));
    final ImmutableList<ValidationViolation> violations = schemaValidator.validate(DescriptorSet.empty(), candidate);
    assertThat(violations, is(empty()));
}
Also used : ValidationViolation(com.spotify.protoman.validation.ValidationViolation) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 4 with DescriptorSet

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

the class EnumNamingRuleTest method testAllowedName_existing.

@Parameters(method = "allowedNames")
@Test
public void testAllowedName_existing(final String name) throws Exception {
    final DescriptorSet candidate = DescriptorSetUtils.buildDescriptorSet("a.proto", String.format(TEMPLATE, name));
    final ImmutableList<ValidationViolation> violations = schemaValidator.validate(candidate, candidate);
    assertThat(violations, is(empty()));
}
Also used : ValidationViolation(com.spotify.protoman.validation.ValidationViolation) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 5 with DescriptorSet

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

the class EnumNamingRuleTest method testDisallowedName_new.

@Parameters(method = "disallowedNames")
@Test
public void testDisallowedName_new(final String name) throws Exception {
    final DescriptorSet current = DescriptorSet.empty();
    final DescriptorSet candidate = DescriptorSetUtils.buildDescriptorSet("a.proto", String.format(TEMPLATE, name));
    final ImmutableList<ValidationViolation> violations = schemaValidator.validate(current, candidate);
    assertThat(violations, contains(validationViolation().description(equalTo("enum name should be UpperCamelCase")).type(equalTo(ViolationType.STYLE_GUIDE_VIOLATION)).current(nullValue(GenericDescriptor.class)).candidate(genericDescriptor().sourceCodeInfo(optionalWithValue(sourceCodeInfo().start(filePosition().line(2).column(1)))))));
}
Also used : ValidationViolation(com.spotify.protoman.validation.ValidationViolation) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

DescriptorSet (com.spotify.protoman.descriptor.DescriptorSet)79 ValidationViolation (com.spotify.protoman.validation.ValidationViolation)76 Test (org.junit.Test)74 Parameters (junitparams.Parameters)43 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)3 DescriptorProtos (com.google.protobuf.DescriptorProtos)3 FileDescriptor (com.spotify.protoman.descriptor.FileDescriptor)3 Objects (java.util.Objects)3 Function (java.util.function.Function)3 Nullable (javax.annotation.Nullable)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Preconditions (com.google.common.base.Preconditions)2 DescriptorBuilderException (com.spotify.protoman.descriptor.DescriptorBuilderException)2 SchemaStorage (com.spotify.protoman.registry.storage.SchemaStorage)2 Path (java.nio.file.Path)2 Predicate (java.util.function.Predicate)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 AutoValue (com.google.auto.value.AutoValue)1