Search in sources :

Example 1 with CType

use of com.google.protobuf.DescriptorProtos.FieldOptions.CType in project schema-registry by confluentinc.

the class ProtobufSchema method toDynamicMessage.

private static MessageDefinition toDynamicMessage(Syntax syntax, MessageElement messageElem) {
    log.trace("*** message: {}", messageElem.getName());
    MessageDefinition.Builder message = MessageDefinition.newBuilder(messageElem.getName());
    for (TypeElement type : messageElem.getNestedTypes()) {
        if (type instanceof MessageElement) {
            message.addMessageDefinition(toDynamicMessage(syntax, (MessageElement) type));
        } else if (type instanceof EnumElement) {
            message.addEnumDefinition(toDynamicEnum((EnumElement) type));
        }
    }
    Set<String> added = new HashSet<>();
    for (OneOfElement oneof : messageElem.getOneOfs()) {
        MessageDefinition.OneofBuilder oneofBuilder = message.addOneof(oneof.getName());
        for (FieldElement field : oneof.getFields()) {
            String defaultVal = field.getDefaultValue();
            String jsonName = field.getJsonName();
            Map<String, OptionElement> options = mergeOptions(field.getOptions());
            CType ctype = findOption(CTYPE, options).map(o -> CType.valueOf(o.getValue().toString())).orElse(null);
            JSType jstype = findOption(JSTYPE, options).map(o -> JSType.valueOf(o.getValue().toString())).orElse(null);
            Boolean isDeprecated = findOption(DEPRECATED, options).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
            Optional<OptionElement> meta = findOption(CONFLUENT_FIELD_META, options);
            String doc = findDoc(meta);
            Map<String, String> params = findParams(meta);
            oneofBuilder.addField(false, field.getType(), field.getName(), field.getTag(), defaultVal, jsonName, doc, params, ctype, jstype, isDeprecated);
            added.add(field.getName());
        }
    }
    // Process fields after messages so that any newly created map entry messages are at the end
    for (FieldElement field : messageElem.getFields()) {
        if (added.contains(field.getName())) {
            continue;
        }
        Field.Label fieldLabel = field.getLabel();
        String label = fieldLabel != null ? fieldLabel.toString().toLowerCase() : null;
        boolean isProto3Optional = "optional".equals(label) && syntax == Syntax.PROTO_3;
        String fieldType = field.getType();
        String defaultVal = field.getDefaultValue();
        String jsonName = field.getJsonName();
        Map<String, OptionElement> options = mergeOptions(field.getOptions());
        CType ctype = findOption(CTYPE, options).map(o -> CType.valueOf(o.getValue().toString())).orElse(null);
        Boolean isPacked = findOption(PACKED, options).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
        JSType jstype = findOption(JSTYPE, options).map(o -> JSType.valueOf(o.getValue().toString())).orElse(null);
        Boolean isDeprecated = findOption(DEPRECATED, options).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
        Optional<OptionElement> meta = findOption(CONFLUENT_FIELD_META, options);
        String doc = findDoc(meta);
        Map<String, String> params = findParams(meta);
        ProtoType protoType = ProtoType.get(fieldType);
        ProtoType keyType = protoType.getKeyType();
        ProtoType valueType = protoType.getValueType();
        // Map fields are only permitted in messages
        if (protoType.isMap() && keyType != null && valueType != null) {
            label = "repeated";
            fieldType = toMapEntry(field.getName());
            MessageDefinition.Builder mapMessage = MessageDefinition.newBuilder(fieldType);
            mapMessage.setMapEntry(true);
            mapMessage.addField(null, keyType.toString(), KEY_FIELD, 1, null, null, null);
            mapMessage.addField(null, valueType.toString(), VALUE_FIELD, 2, null, null, null);
            message.addMessageDefinition(mapMessage.build());
        }
        if (isProto3Optional) {
            // Add synthetic oneof after real oneofs
            MessageDefinition.OneofBuilder oneofBuilder = message.addOneof("_" + field.getName());
            oneofBuilder.addField(true, fieldType, field.getName(), field.getTag(), defaultVal, jsonName, doc, params, ctype, jstype, isDeprecated);
        } else {
            message.addField(label, fieldType, field.getName(), field.getTag(), defaultVal, jsonName, doc, params, ctype, isPacked, jstype, isDeprecated);
        }
    }
    for (ReservedElement reserved : messageElem.getReserveds()) {
        for (Object elem : reserved.getValues()) {
            if (elem instanceof String) {
                message.addReservedName((String) elem);
            } else if (elem instanceof Integer) {
                int tag = (Integer) elem;
                message.addReservedRange(tag, tag + 1);
            } else if (elem instanceof IntRange) {
                IntRange range = (IntRange) elem;
                message.addReservedRange(range.getStart(), range.getEndInclusive() + 1);
            } else {
                throw new IllegalStateException("Unsupported reserved type: " + elem.getClass().getName());
            }
        }
    }
    Map<String, OptionElement> options = mergeOptions(messageElem.getOptions());
    Boolean noStandardDescriptorAccessor = findOption(NO_STANDARD_DESCRIPTOR_ACCESSOR, options).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
    if (noStandardDescriptorAccessor != null) {
        message.setNoStandardDescriptorAccessor(noStandardDescriptorAccessor);
    }
    Boolean isDeprecated = findOption(DEPRECATED, options).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
    if (isDeprecated != null) {
        message.setDeprecated(isDeprecated);
    }
    Boolean isMapEntry = findOption(MAP_ENTRY, options).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
    if (isMapEntry != null) {
        message.setMapEntry(isMapEntry);
    }
    Optional<OptionElement> meta = findOption(CONFLUENT_MESSAGE_META, options);
    String doc = findDoc(meta);
    Map<String, String> params = findParams(meta);
    message.setMeta(doc, params);
    return message.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) CType(com.google.protobuf.DescriptorProtos.FieldOptions.CType) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ProtoType(com.squareup.wire.schema.ProtoType) MessageDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.MessageDefinition) Field(com.squareup.wire.schema.Field) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) HashSet(java.util.HashSet) JSType(com.google.protobuf.DescriptorProtos.FieldOptions.JSType) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) IntRange(kotlin.ranges.IntRange) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement)

Aggregations

LOWER_UNDERSCORE (com.google.common.base.CaseFormat.LOWER_UNDERSCORE)1 UPPER_CAMEL (com.google.common.base.CaseFormat.UPPER_CAMEL)1 ImmutableList (com.google.common.collect.ImmutableList)1 AnyProto (com.google.protobuf.AnyProto)1 ApiProto (com.google.protobuf.ApiProto)1 DescriptorProtos (com.google.protobuf.DescriptorProtos)1 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)1 ReservedRange (com.google.protobuf.DescriptorProtos.DescriptorProto.ReservedRange)1 EnumDescriptorProto (com.google.protobuf.DescriptorProtos.EnumDescriptorProto)1 EnumReservedRange (com.google.protobuf.DescriptorProtos.EnumDescriptorProto.EnumReservedRange)1 EnumValueDescriptorProto (com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto)1 FieldDescriptorProto (com.google.protobuf.DescriptorProtos.FieldDescriptorProto)1 CType (com.google.protobuf.DescriptorProtos.FieldOptions.CType)1 JSType (com.google.protobuf.DescriptorProtos.FieldOptions.JSType)1 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)1 OptimizeMode (com.google.protobuf.DescriptorProtos.FileOptions.OptimizeMode)1 MethodDescriptorProto (com.google.protobuf.DescriptorProtos.MethodDescriptorProto)1 IdempotencyLevel (com.google.protobuf.DescriptorProtos.MethodOptions.IdempotencyLevel)1 OneofDescriptorProto (com.google.protobuf.DescriptorProtos.OneofDescriptorProto)1 ServiceDescriptorProto (com.google.protobuf.DescriptorProtos.ServiceDescriptorProto)1