Search in sources :

Example 11 with EnumElement

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

the class ProtobufSchema method toDynamicEnum.

private static EnumDefinition toDynamicEnum(EnumElement enumElem) {
    Map<String, OptionElement> enumOptions = mergeOptions(enumElem.getOptions());
    Boolean allowAlias = findOption(ALLOW_ALIAS, enumOptions).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
    Boolean isDeprecated = findOption(DEPRECATED, enumOptions).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
    EnumDefinition.Builder enumer = EnumDefinition.newBuilder(enumElem.getName(), allowAlias, isDeprecated);
    for (ReservedElement reserved : enumElem.getReserveds()) {
        for (Object elem : reserved.getValues()) {
            if (elem instanceof String) {
                enumer.addReservedName((String) elem);
            } else if (elem instanceof Integer) {
                int tag = (Integer) elem;
                enumer.addReservedRange(tag, tag + 1);
            } else if (elem instanceof IntRange) {
                IntRange range = (IntRange) elem;
                enumer.addReservedRange(range.getStart(), range.getEndInclusive() + 1);
            } else {
                throw new IllegalStateException("Unsupported reserved type: " + elem.getClass().getName());
            }
        }
    }
    for (EnumConstantElement constant : enumElem.getConstants()) {
        Map<String, OptionElement> constantOptions = mergeOptions(constant.getOptions());
        Boolean isConstDeprecated = findOption(DEPRECATED, constantOptions).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
        Optional<OptionElement> meta = findOption(CONFLUENT_ENUM_VALUE_META, constantOptions);
        String doc = findDoc(meta);
        Map<String, String> params = findParams(meta);
        enumer.addValue(constant.getName(), constant.getTag(), doc, params, isConstDeprecated);
    }
    Optional<OptionElement> meta = findOption(CONFLUENT_ENUM_META, enumOptions);
    String doc = findDoc(meta);
    Map<String, String> params = findParams(meta);
    enumer.setMeta(doc, params);
    return enumer.build();
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) QuaternionProto(com.google.type.QuaternionProto) CType(com.google.protobuf.DescriptorProtos.FieldOptions.CType) SchemaDiff(io.confluent.kafka.schemaregistry.protobuf.diff.SchemaDiff) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) LoggerFactory(org.slf4j.LoggerFactory) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) ProtoParser(com.squareup.wire.schema.internal.parser.ProtoParser) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) EmptyProto(com.google.protobuf.EmptyProto) Map(java.util.Map) PhoneNumberProto(com.google.type.PhoneNumberProto) DynamicSchema(io.confluent.kafka.schemaregistry.protobuf.dynamic.DynamicSchema) ExprProto(com.google.type.ExprProto) SchemaReference(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) ServiceDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.ServiceDefinition) IntRange(kotlin.ranges.IntRange) JSType(com.google.protobuf.DescriptorProtos.FieldOptions.JSType) 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) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) StructProto(com.google.protobuf.StructProto) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) DurationProto(com.google.protobuf.DurationProto) Difference(io.confluent.kafka.schemaregistry.protobuf.diff.Difference) FieldMaskProto(com.google.protobuf.FieldMaskProto) Base64(java.util.Base64) 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) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) SourceContextProto(com.google.protobuf.SourceContextProto) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) ColorProto(com.google.type.ColorProto) TypeProto(com.google.protobuf.TypeProto) LatLngProto(com.google.type.LatLngProto) TimeOfDayProto(com.google.type.TimeOfDayProto) OptimizeMode(com.google.protobuf.DescriptorProtos.FileOptions.OptimizeMode) Descriptor(com.google.protobuf.Descriptors.Descriptor) DynamicMessage(com.google.protobuf.DynamicMessage) Descriptors(com.google.protobuf.Descriptors) DateTimeProto(com.google.type.DateTimeProto) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) HashMap(java.util.HashMap) ReservedRange(com.google.protobuf.DescriptorProtos.DescriptorProto.ReservedRange) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MetaProto(io.confluent.protobuf.MetaProto) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) IdempotencyLevel(com.google.protobuf.DescriptorProtos.MethodOptions.IdempotencyLevel) MessageDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.MessageDefinition) AnyProto(com.google.protobuf.AnyProto) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) WrappersProto(com.google.protobuf.WrappersProto) Logger(org.slf4j.Logger) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) DateProto(com.google.type.DateProto) ApiProto(com.google.protobuf.ApiProto) EnumReservedRange(com.google.protobuf.DescriptorProtos.EnumDescriptorProto.EnumReservedRange) DayOfWeekProto(com.google.type.DayOfWeekProto) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) EnumDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition) DecimalProto(io.confluent.protobuf.type.DecimalProto) TimestampProto(com.google.protobuf.TimestampProto) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Kind(com.squareup.wire.schema.internal.parser.OptionElement.Kind) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) Meta(io.confluent.protobuf.MetaProto.Meta) IntRange(kotlin.ranges.IntRange) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) EnumDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement)

Example 12 with EnumElement

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

the class ProtobufSchemaUtils method toString.

private static String toString(Context ctx, ProtoFileElement protoFile, boolean normalize) {
    StringBuilder sb = new StringBuilder();
    if (protoFile.getSyntax() != null) {
        sb.append("syntax = \"");
        sb.append(protoFile.getSyntax());
        sb.append("\";\n");
    }
    if (protoFile.getPackageName() != null) {
        sb.append("package ");
        sb.append(protoFile.getPackageName());
        sb.append(";\n");
    }
    if (!protoFile.getImports().isEmpty() || !protoFile.getPublicImports().isEmpty()) {
        sb.append('\n');
        List<String> imports = protoFile.getImports();
        if (normalize) {
            imports = imports.stream().sorted().distinct().collect(Collectors.toList());
        }
        for (String file : imports) {
            sb.append("import \"");
            sb.append(file);
            sb.append("\";\n");
        }
        List<String> publicImports = protoFile.getPublicImports();
        if (normalize) {
            publicImports = publicImports.stream().sorted().distinct().collect(Collectors.toList());
        }
        for (String file : publicImports) {
            sb.append("import public \"");
            sb.append(file);
            sb.append("\";\n");
        }
    }
    if (!protoFile.getOptions().isEmpty()) {
        sb.append('\n');
        List<OptionElement> options = protoFile.getOptions();
        if (normalize) {
            options = new ArrayList<>(options);
            options.sort(Comparator.comparing(OptionElement::getName));
        }
        for (OptionElement option : options) {
            sb.append(toOptionString(option, normalize));
        }
    }
    if (!protoFile.getTypes().isEmpty()) {
        sb.append('\n');
        // the non-normalized schema to serialize message indexes
        for (TypeElement typeElement : protoFile.getTypes()) {
            if (typeElement instanceof MessageElement) {
                if (normalize) {
                    TypeElementInfo typeInfo = ctx.getType(typeElement.getName(), true);
                    if (typeInfo != null && typeInfo.isMap()) {
                        // don't emit synthetic map message
                        continue;
                    }
                }
                try (Context.NamedScope nameScope = ctx.enterName(typeElement.getName())) {
                    sb.append(toString(ctx, (MessageElement) typeElement, normalize));
                }
            }
        }
        for (TypeElement typeElement : protoFile.getTypes()) {
            if (typeElement instanceof EnumElement) {
                try (Context.NamedScope nameScope = ctx.enterName(typeElement.getName())) {
                    sb.append(toString(ctx, (EnumElement) typeElement, normalize));
                }
            }
        }
    }
    if (!protoFile.getExtendDeclarations().isEmpty()) {
        sb.append('\n');
        for (ExtendElement extendDeclaration : protoFile.getExtendDeclarations()) {
            sb.append(extendDeclaration.toSchema());
        }
    }
    if (!protoFile.getServices().isEmpty()) {
        sb.append('\n');
        // we don't sort message/enum elements
        for (ServiceElement service : protoFile.getServices()) {
            sb.append(toString(ctx, service, normalize));
        }
    }
    return sb.toString();
}
Also used : Context(io.confluent.kafka.schemaregistry.protobuf.diff.Context) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) ExtendElement(com.squareup.wire.schema.internal.parser.ExtendElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) TypeElementInfo(io.confluent.kafka.schemaregistry.protobuf.diff.Context.TypeElementInfo)

Example 13 with EnumElement

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

the class SchemaDiff method compareMessageElements.

private static void compareMessageElements(List<TypeElement> types, Map<String, MessageElement> messages, Map<String, Integer> messageIndexes, Map<String, EnumElement> enums) {
    int index = 0;
    for (TypeElement typeElement : types) {
        if (typeElement instanceof MessageElement) {
            MessageElement messageElement = (MessageElement) typeElement;
            messages.put(messageElement.getName(), messageElement);
            messageIndexes.put(messageElement.getName(), index++);
        } else if (typeElement instanceof EnumElement) {
            EnumElement enumElement = (EnumElement) typeElement;
            enums.put(enumElement.getName(), enumElement);
        }
    }
}
Also used : TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement)

Example 14 with EnumElement

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

the class SchemaDiff method compareTypeElements.

public static void compareTypeElements(final Context ctx, final List<TypeElement> original, final List<TypeElement> update) {
    Map<String, MessageElement> originalMessages = new HashMap<>();
    Map<String, MessageElement> updateMessages = new HashMap<>();
    Map<String, Integer> originalMessageIndexes = new HashMap<>();
    Map<String, Integer> updateMessageIndexes = new HashMap<>();
    Map<String, EnumElement> originalEnums = new HashMap<>();
    Map<String, EnumElement> updateEnums = new HashMap<>();
    compareMessageElements(original, originalMessages, originalMessageIndexes, originalEnums);
    compareMessageElements(update, updateMessages, updateMessageIndexes, updateEnums);
    Set<String> allMessageNames = new HashSet<>(originalMessages.keySet());
    allMessageNames.addAll(updateMessages.keySet());
    Set<String> allEnumNames = new HashSet<>(originalEnums.keySet());
    allEnumNames.addAll(updateEnums.keySet());
    for (String name : allMessageNames) {
        try (Context.NamedScope nameScope = ctx.enterName(name)) {
            MessageElement originalMessage = originalMessages.get(name);
            MessageElement updateMessage = updateMessages.get(name);
            if (updateMessage == null) {
                TypeElementInfo originalType = ctx.getType(name, true);
                if (originalType != null && !originalType.isMap()) {
                    ctx.addDifference(MESSAGE_REMOVED);
                }
            } else if (originalMessage == null) {
                TypeElementInfo updateType = ctx.getType(name, false);
                if (updateType != null && !updateType.isMap()) {
                    ctx.addDifference(MESSAGE_ADDED);
                }
            } else {
                MessageSchemaDiff.compare(ctx, originalMessage, updateMessage);
                Integer originalMessageIndex = originalMessageIndexes.get(name);
                Integer updateMessageIndex = updateMessageIndexes.get(name);
                if (originalMessageIndex == null || !originalMessageIndex.equals(updateMessageIndex)) {
                    // Moving or reordering a message is compatible since serialized message indexes
                    // are w.r.t. the schema of the corresponding ID
                    ctx.addDifference(MESSAGE_MOVED);
                }
            }
        }
    }
    for (String name : allEnumNames) {
        try (Context.NamedScope nameScope = ctx.enterName(name)) {
            EnumElement originalEnum = originalEnums.get(name);
            EnumElement updateEnum = updateEnums.get(name);
            if (updateEnum == null) {
                ctx.addDifference(ENUM_REMOVED);
            } else if (originalEnum == null) {
                ctx.addDifference(ENUM_ADDED);
            } else {
                EnumSchemaDiff.compare(ctx, originalEnum, updateEnum);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TypeElementInfo(io.confluent.kafka.schemaregistry.protobuf.diff.Context.TypeElementInfo) HashSet(java.util.HashSet)

Example 15 with EnumElement

use of com.squareup.wire.schema.internal.parser.EnumElement 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)

Aggregations

EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)26 MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)23 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)22 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)18 ServiceElement (com.squareup.wire.schema.internal.parser.ServiceElement)17 HashSet (java.util.HashSet)16 ImmutableList (com.google.common.collect.ImmutableList)14 EnumValueDescriptorProto (com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto)14 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)14 EnumConstantElement (com.squareup.wire.schema.internal.parser.EnumConstantElement)13 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)13 OneOfElement (com.squareup.wire.schema.internal.parser.OneOfElement)13 ReservedElement (com.squareup.wire.schema.internal.parser.ReservedElement)13 Syntax (com.squareup.wire.Syntax)12 RpcElement (com.squareup.wire.schema.internal.parser.RpcElement)12 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)11 EnumDescriptorProto (com.google.protobuf.DescriptorProtos.EnumDescriptorProto)11 FieldDescriptorProto (com.google.protobuf.DescriptorProtos.FieldDescriptorProto)11 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)11 MethodDescriptorProto (com.google.protobuf.DescriptorProtos.MethodDescriptorProto)11