Search in sources :

Example 6 with EnumConstantElement

use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project schema-registry by confluentinc.

the class EnumSchemaDiff method compare.

static void compare(final Context ctx, final EnumElement original, final EnumElement update) {
    Map<Integer, EnumConstantElement> originalByTag = new HashMap<>();
    for (EnumConstantElement enumer : original.getConstants()) {
        originalByTag.put(enumer.getTag(), enumer);
    }
    Map<Integer, EnumConstantElement> updateByTag = new HashMap<>();
    for (EnumConstantElement enumer : update.getConstants()) {
        updateByTag.put(enumer.getTag(), enumer);
    }
    Set<Integer> allTags = new HashSet<>(originalByTag.keySet());
    allTags.addAll(updateByTag.keySet());
    for (Integer tag : allTags) {
        try (Context.PathScope pathScope = ctx.enterPath(tag.toString())) {
            EnumConstantElement originalEnumConstant = originalByTag.get(tag);
            EnumConstantElement updateEnumConstant = updateByTag.get(tag);
            if (updateEnumConstant == null) {
                ctx.addDifference(ENUM_CONST_REMOVED);
            } else if (originalEnumConstant == null) {
                ctx.addDifference(ENUM_CONST_ADDED);
            } else if (!originalEnumConstant.getName().equals(updateEnumConstant.getName())) {
                ctx.addDifference(ENUM_CONST_CHANGED);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) HashSet(java.util.HashSet)

Example 7 with EnumConstantElement

use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project apicurio-registry by Apicurio.

the class FileDescriptorUtils method toDynamicEnum.

private static EnumDefinition toDynamicEnum(EnumElement enumElem) {
    Boolean allowAlias = findOption("allow_alias", enumElem.getOptions()).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
    EnumDefinition.Builder enumer = EnumDefinition.newBuilder(enumElem.getName(), allowAlias);
    for (EnumConstantElement constant : enumElem.getConstants()) {
        enumer.addValue(constant.getName(), constant.getTag());
    }
    return enumer.build();
}
Also used : QuaternionProto(com.google.type.QuaternionProto) Decimals(additionalTypes.Decimals) com.google.protobuf(com.google.protobuf) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) MonthProto(com.google.type.MonthProto) Set(java.util.Set) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ProtoType(com.squareup.wire.schema.ProtoType) Collectors(java.util.stream.Collectors) EnumConstant(com.squareup.wire.schema.EnumConstant) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) List(java.util.List) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) PostalAddressProto(com.google.type.PostalAddressProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) ProtobufSchemaMetadata(metadata.ProtobufSchemaMetadata) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement)

Example 8 with EnumConstantElement

use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project apicurio-registry by Apicurio.

the class FileDescriptorUtils method toEnum.

private static EnumElement toEnum(EnumDescriptorProto ed) {
    String name = ed.getName();
    ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder();
    for (EnumValueDescriptorProto ev : ed.getValueList()) {
        ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
        constants.add(new EnumConstantElement(DEFAULT_LOCATION, ev.getName(), ev.getNumber(), "", options.build()));
    }
    ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
    if (ed.getOptions().hasAllowAlias()) {
        OptionElement option = new OptionElement(ALLOW_ALIAS_OPTION, booleanKind, ed.getOptions().getAllowAlias(), false);
        options.add(option);
    }
    return new EnumElement(DEFAULT_LOCATION, name, "", options.build(), constants.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement)

Example 9 with EnumConstantElement

use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project apicurio-registry by Apicurio.

the class ProtobufCompatibilityCheckerLibrary method checkNoChangingFieldIDs.

/**
 * Determine if any field ID number has been changed.
 *
 * @return differences list
 */
public List<ProtobufDifference> checkNoChangingFieldIDs() {
    List<ProtobufDifference> issues = new ArrayList<>();
    Map<String, Map<String, FieldElement>> before = fileBefore.getFieldMap();
    Map<String, Map<String, FieldElement>> after = fileAfter.getFieldMap();
    for (Map.Entry<String, Map<String, FieldElement>> entry : before.entrySet()) {
        Map<String, FieldElement> afterMap = after.get(entry.getKey());
        if (afterMap != null) {
            for (Map.Entry<String, FieldElement> beforeKV : entry.getValue().entrySet()) {
                FieldElement afterFE = afterMap.get(beforeKV.getKey());
                if (afterFE != null && beforeKV.getValue().getTag() != afterFE.getTag()) {
                    issues.add(ProtobufDifference.from(String.format("Conflict, field id changed, message %s , before: %s , after %s", entry.getKey(), beforeKV.getValue().getTag(), afterFE.getTag())));
                }
            }
        }
    }
    Map<String, Map<String, EnumConstantElement>> beforeEnum = fileBefore.getEnumFieldMap();
    Map<String, Map<String, EnumConstantElement>> afterEnum = fileAfter.getEnumFieldMap();
    for (Map.Entry<String, Map<String, EnumConstantElement>> entry : beforeEnum.entrySet()) {
        Map<String, EnumConstantElement> afterMap = afterEnum.get(entry.getKey());
        if (afterMap != null) {
            for (Map.Entry<String, EnumConstantElement> beforeKV : entry.getValue().entrySet()) {
                EnumConstantElement afterECE = afterMap.get(beforeKV.getKey());
                if (afterECE != null && beforeKV.getValue().getTag() != afterECE.getTag()) {
                    issues.add(ProtobufDifference.from(String.format("Conflict, field id changed, message %s , before: %s , after %s", entry.getKey(), beforeKV.getValue().getTag(), afterECE.getTag())));
                }
            }
        }
    }
    return issues;
}
Also used : ProtobufDifference(io.apicurio.registry.protobuf.ProtobufDifference) ArrayList(java.util.ArrayList) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with EnumConstantElement

use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project schema-registry by confluentinc.

the class ProtobufSchemaUtils method toString.

private static String toString(Context ctx, EnumElement type, boolean normalize) {
    StringBuilder sb = new StringBuilder();
    sb.append("enum ");
    sb.append(type.getName());
    sb.append(" {");
    if (!type.getReserveds().isEmpty()) {
        sb.append('\n');
        List<ReservedElement> reserveds = type.getReserveds();
        if (normalize) {
            reserveds = reserveds.stream().flatMap(r -> r.getValues().stream().map(o -> new ReservedElement(r.getLocation(), r.getDocumentation(), Collections.singletonList(o)))).collect(Collectors.toList());
            Comparator<Object> cmp = Comparator.comparing(r -> {
                Object o = ((ReservedElement) r).getValues().get(0);
                if (o instanceof IntRange) {
                    return ((IntRange) o).getStart();
                } else if (o instanceof Integer) {
                    return (Integer) o;
                } else {
                    return Integer.MAX_VALUE;
                }
            }).thenComparing(r -> ((ReservedElement) r).getValues().get(0).toString());
            reserveds.sort(cmp);
        }
        for (ReservedElement reserved : reserveds) {
            appendIndented(sb, toString(ctx, reserved, normalize));
        }
    }
    if (type.getReserveds().isEmpty() && (!type.getOptions().isEmpty() || !type.getConstants().isEmpty())) {
        sb.append('\n');
    }
    if (!type.getOptions().isEmpty()) {
        List<OptionElement> options = type.getOptions();
        if (normalize) {
            options = new ArrayList<>(options);
            options.sort(Comparator.comparing(OptionElement::getName));
        }
        for (OptionElement option : options) {
            appendIndented(sb, toOptionString(option, normalize));
        }
    }
    if (!type.getConstants().isEmpty()) {
        List<EnumConstantElement> constants = type.getConstants();
        if (normalize) {
            constants = new ArrayList<>(constants);
            constants.sort(Comparator.comparing(EnumConstantElement::getTag).thenComparing(EnumConstantElement::getName));
        }
        for (EnumConstantElement constant : constants) {
            appendIndented(sb, toString(ctx, constant, normalize));
        }
    }
    sb.append("}\n");
    return sb.toString();
}
Also used : Arrays(java.util.Arrays) DynamicMessage(com.google.protobuf.DynamicMessage) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) Label(com.squareup.wire.schema.Field.Label) ArrayList(java.util.ArrayList) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) Locale(java.util.Locale) Map(java.util.Map) ExtendElement(com.squareup.wire.schema.internal.parser.ExtendElement) JsonNode(com.fasterxml.jackson.databind.JsonNode) IntRange(kotlin.ranges.IntRange) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GroupElement(com.squareup.wire.schema.internal.parser.GroupElement) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) Ascii(com.google.common.base.Ascii) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) JacksonMapper(io.confluent.kafka.schemaregistry.utils.JacksonMapper) ProtoType(com.squareup.wire.schema.ProtoType) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) Context(io.confluent.kafka.schemaregistry.protobuf.diff.Context) List(java.util.List) JsonFormat(com.google.protobuf.util.JsonFormat) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) TypeElementInfo(io.confluent.kafka.schemaregistry.protobuf.diff.Context.TypeElementInfo) Message(com.google.protobuf.Message) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) Kind(com.squareup.wire.schema.internal.parser.OptionElement.Kind) Comparator(java.util.Comparator) Collections(java.util.Collections) MAX_TAG_VALUE(com.squareup.wire.schema.internal.UtilKt.MAX_TAG_VALUE) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) IntRange(kotlin.ranges.IntRange) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement)

Aggregations

EnumConstantElement (com.squareup.wire.schema.internal.parser.EnumConstantElement)10 EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)6 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)6 ImmutableList (com.google.common.collect.ImmutableList)5 EnumValueDescriptorProto (com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto)5 ReservedElement (com.squareup.wire.schema.internal.parser.ReservedElement)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 LOWER_UNDERSCORE (com.google.common.base.CaseFormat.LOWER_UNDERSCORE)2 UPPER_CAMEL (com.google.common.base.CaseFormat.UPPER_CAMEL)2 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)2 EnumDescriptorProto (com.google.protobuf.DescriptorProtos.EnumDescriptorProto)2 EnumReservedRange (com.google.protobuf.DescriptorProtos.EnumDescriptorProto.EnumReservedRange)2 FieldDescriptorProto (com.google.protobuf.DescriptorProtos.FieldDescriptorProto)2 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)2 MethodDescriptorProto (com.google.protobuf.DescriptorProtos.MethodDescriptorProto)2 OneofDescriptorProto (com.google.protobuf.DescriptorProtos.OneofDescriptorProto)2