Search in sources :

Example 1 with EnumConstantElement

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

the class ProtobufFile method processEnumElement.

private void processEnumElement(String scope, EnumElement enumElement) {
    // TODO reservedEnumFields - wire doesn't preserve these
    // https://github.com/square/wire/issues/797 RFE: capture EnumElement reserved info
    // enumFieldMap, enumFieldsIDName, nonReservedEnumFields
    Map<String, EnumConstantElement> map = new HashMap<>();
    Map<Integer, String> idsToNames = new HashMap<>();
    Set<Object> fieldKeySet = new HashSet<>();
    for (EnumConstantElement enumConstantElement : enumElement.getConstants()) {
        map.put(enumConstantElement.getName(), enumConstantElement);
        idsToNames.put(enumConstantElement.getTag(), enumConstantElement.getName());
        fieldKeySet.add(enumConstantElement.getTag());
        fieldKeySet.add(enumConstantElement.getName());
    }
    if (!map.isEmpty()) {
        enumFieldMap.put(scope + enumElement.getName(), map);
    }
    if (!idsToNames.isEmpty()) {
        enumFieldsById.put(scope + enumElement.getName(), idsToNames);
    }
    if (!fieldKeySet.isEmpty()) {
        nonReservedEnumFields.put(scope + enumElement.getName(), fieldKeySet);
    }
}
Also used : HashMap(java.util.HashMap) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) HashSet(java.util.HashSet)

Example 2 with EnumConstantElement

use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project aws-glue-schema-registry by awslabs.

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 3 with EnumConstantElement

use of com.squareup.wire.schema.internal.parser.EnumConstantElement in project aws-glue-schema-registry by awslabs.

the class ProtobufFile method processEnumElement.

private void processEnumElement(String scope, EnumElement enumElement) {
    // TODO reservedEnumFields - wire doesn't preserve these
    // https://github.com/square/wire/issues/797 RFE: capture EnumElement reserved info
    // enumFieldMap, enumFieldsIDName, nonReservedEnumFields
    Map<String, EnumConstantElement> map = new HashMap<>();
    Map<Integer, String> idsToNames = new HashMap<>();
    Set<Object> fieldKeySet = new HashSet<>();
    for (EnumConstantElement enumConstantElement : enumElement.getConstants()) {
        map.put(enumConstantElement.getName(), enumConstantElement);
        idsToNames.put(enumConstantElement.getTag(), enumConstantElement.getName());
        fieldKeySet.add(enumConstantElement.getTag());
        fieldKeySet.add(enumConstantElement.getName());
    }
    if (!map.isEmpty()) {
        enumFieldMap.put(scope + enumElement.getName(), map);
    }
    if (!idsToNames.isEmpty()) {
        enumFieldsById.put(scope + enumElement.getName(), idsToNames);
    }
    if (!fieldKeySet.isEmpty()) {
        nonReservedEnumFields.put(scope + enumElement.getName(), fieldKeySet);
    }
}
Also used : HashMap(java.util.HashMap) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) HashSet(java.util.HashSet)

Example 4 with EnumConstantElement

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

the class ProtobufSchema method toEnum.

private static EnumElement toEnum(EnumDescriptorProto ed) {
    String name = ed.getName();
    log.trace("*** enum name: {}", name);
    ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder();
    for (EnumValueDescriptorProto ev : ed.getValueList()) {
        ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
        if (ev.getOptions().hasDeprecated()) {
            OptionElement option = new OptionElement(DEPRECATED, Kind.BOOLEAN, ev.getOptions().getDeprecated(), false);
            options.add(option);
        }
        if (ev.getOptions().hasExtension(MetaProto.enumValueMeta)) {
            Meta meta = ev.getOptions().getExtension(MetaProto.enumValueMeta);
            OptionElement option = toOption(CONFLUENT_ENUM_VALUE_META, meta);
            if (option != null) {
                options.add(option);
            }
        }
        constants.add(new EnumConstantElement(DEFAULT_LOCATION, ev.getName(), ev.getNumber(), "", options.build()));
    }
    ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
    for (EnumReservedRange range : ed.getReservedRangeList()) {
        ReservedElement reservedElem = toReserved(range);
        reserved.add(reservedElem);
    }
    for (String reservedName : ed.getReservedNameList()) {
        ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
        reserved.add(reservedElem);
    }
    ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
    if (ed.getOptions().hasAllowAlias()) {
        OptionElement option = new OptionElement(ALLOW_ALIAS, Kind.BOOLEAN, ed.getOptions().getAllowAlias(), false);
        options.add(option);
    }
    if (ed.getOptions().hasDeprecated()) {
        OptionElement option = new OptionElement(DEPRECATED, Kind.BOOLEAN, ed.getOptions().getDeprecated(), false);
        options.add(option);
    }
    if (ed.getOptions().hasExtension(MetaProto.enumMeta)) {
        Meta meta = ed.getOptions().getExtension(MetaProto.enumMeta);
        OptionElement option = toOption(CONFLUENT_ENUM_META, meta);
        if (option != null) {
            options.add(option);
        }
    }
    return new EnumElement(DEFAULT_LOCATION, name, "", options.build(), constants.build(), reserved.build());
}
Also used : Meta(io.confluent.protobuf.MetaProto.Meta) EnumReservedRange(com.google.protobuf.DescriptorProtos.EnumDescriptorProto.EnumReservedRange) ImmutableList(com.google.common.collect.ImmutableList) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement)

Example 5 with EnumConstantElement

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

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