Search in sources :

Example 1 with EnumDefinition

use of io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition in project schema-registry by confluentinc.

the class ProtobufData method enumDefinitionFromConnectSchema.

private EnumDefinition enumDefinitionFromConnectSchema(FromConnectContext ctx, DynamicSchema.Builder schema, Schema enumElem) {
    String enumName = getUnqualifiedName(ctx, enumElem.name());
    EnumDefinition.Builder enumBuilder = EnumDefinition.newBuilder(enumName);
    String paramName = generalizedSumTypeSupport ? GENERALIZED_TYPE_ENUM : PROTOBUF_TYPE_ENUM;
    for (Map.Entry<String, String> entry : enumElem.parameters().entrySet()) {
        if (entry.getKey().startsWith(paramName + ".")) {
            String name = entry.getKey().substring(paramName.length() + 1);
            int tag = Integer.parseInt(entry.getValue());
            enumBuilder.addValue(name, tag);
        }
    }
    return enumBuilder.build();
}
Also used : ByteString(com.google.protobuf.ByteString) EnumDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition) BoundedConcurrentHashMap(io.confluent.kafka.schemaregistry.utils.BoundedConcurrentHashMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with EnumDefinition

use of io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition 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 3 with EnumDefinition

use of io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition in project schema-registry by confluentinc.

the class ProtobufSchema method toDynamicSchema.

private static DynamicSchema toDynamicSchema(String name, ProtoFileElement rootElem, Map<String, ProtoFileElement> dependencies) {
    if (log.isTraceEnabled()) {
        log.trace("*** toDynamicSchema: {}", ProtobufSchemaUtils.toString(rootElem));
    }
    DynamicSchema.Builder schema = DynamicSchema.newBuilder();
    try {
        Syntax syntax = rootElem.getSyntax();
        if (syntax != null) {
            schema.setSyntax(syntax.toString());
        }
        if (rootElem.getPackageName() != null) {
            schema.setPackage(rootElem.getPackageName());
        }
        for (TypeElement typeElem : rootElem.getTypes()) {
            if (typeElem instanceof MessageElement) {
                MessageDefinition message = toDynamicMessage(syntax, (MessageElement) typeElem);
                schema.addMessageDefinition(message);
            } else if (typeElem instanceof EnumElement) {
                EnumDefinition enumer = toDynamicEnum((EnumElement) typeElem);
                schema.addEnumDefinition(enumer);
            }
        }
        for (ServiceElement serviceElement : rootElem.getServices()) {
            ServiceDefinition service = toDynamicService(serviceElement);
            schema.addServiceDefinition(service);
        }
        for (String ref : rootElem.getImports()) {
            ProtoFileElement dep = dependencies.get(ref);
            if (dep != null) {
                schema.addDependency(ref);
                schema.addSchema(toDynamicSchema(ref, dep, dependencies));
            }
        }
        for (String ref : rootElem.getPublicImports()) {
            ProtoFileElement dep = dependencies.get(ref);
            if (dep != null) {
                schema.addPublicDependency(ref);
                schema.addSchema(toDynamicSchema(ref, dep, dependencies));
            }
        }
        Map<String, OptionElement> options = mergeOptions(rootElem.getOptions());
        OptionElement javaPackageName = options.get(JAVA_PACKAGE);
        if (javaPackageName != null) {
            schema.setJavaPackage(javaPackageName.getValue().toString());
        }
        OptionElement javaOuterClassname = options.get(JAVA_OUTER_CLASSNAME);
        if (javaOuterClassname != null) {
            schema.setJavaOuterClassname(javaOuterClassname.getValue().toString());
        }
        OptionElement javaMultipleFiles = options.get(JAVA_MULTIPLE_FILES);
        if (javaMultipleFiles != null) {
            schema.setJavaMultipleFiles(Boolean.parseBoolean(javaMultipleFiles.getValue().toString()));
        }
        OptionElement javaStringCheckUtf8 = options.get(JAVA_STRING_CHECK_UTF8);
        if (javaStringCheckUtf8 != null) {
            schema.setJavaStringCheckUtf8(Boolean.parseBoolean(javaStringCheckUtf8.getValue().toString()));
        }
        OptionElement optimizeFor = options.get(OPTIMIZE_FOR);
        if (optimizeFor != null) {
            schema.setOptimizeFor(OptimizeMode.valueOf(optimizeFor.getValue().toString()));
        }
        OptionElement goPackage = options.get(GO_PACKAGE);
        if (goPackage != null) {
            schema.setGoPackage(goPackage.getValue().toString());
        }
        OptionElement ccGenericServices = options.get(CC_GENERIC_SERVICES);
        if (ccGenericServices != null) {
            schema.setCcGenericServices(Boolean.parseBoolean(ccGenericServices.getValue().toString()));
        }
        OptionElement javaGenericServices = options.get(JAVA_GENERIC_SERVICES);
        if (javaGenericServices != null) {
            schema.setJavaGenericServices(Boolean.parseBoolean(javaGenericServices.getValue().toString()));
        }
        OptionElement pyGenericServices = options.get(PY_GENERIC_SERVICES);
        if (pyGenericServices != null) {
            schema.setPyGenericServices(Boolean.parseBoolean(pyGenericServices.getValue().toString()));
        }
        OptionElement phpGenericServices = options.get(PHP_GENERIC_SERVICES);
        if (phpGenericServices != null) {
            schema.setPhpGenericServices(Boolean.parseBoolean(phpGenericServices.getValue().toString()));
        }
        OptionElement isDeprecated = options.get(DEPRECATED);
        if (isDeprecated != null) {
            schema.setDeprecated(Boolean.parseBoolean(isDeprecated.getValue().toString()));
        }
        OptionElement ccEnableArenas = options.get(CC_ENABLE_ARENAS);
        if (ccEnableArenas != null) {
            schema.setCcEnableArenas(Boolean.parseBoolean(ccEnableArenas.getValue().toString()));
        }
        OptionElement objcClassPrefix = options.get(OBJC_CLASS_PREFIX);
        if (objcClassPrefix != null) {
            schema.setObjcClassPrefix(objcClassPrefix.getValue().toString());
        }
        OptionElement csharpNamespace = options.get(CSHARP_NAMESPACE);
        if (csharpNamespace != null) {
            schema.setCsharpNamespace(csharpNamespace.getValue().toString());
        }
        OptionElement swiftPrefix = options.get(SWIFT_PREFIX);
        if (swiftPrefix != null) {
            schema.setSwiftPrefix(swiftPrefix.getValue().toString());
        }
        OptionElement phpClassPrefix = options.get(PHP_CLASS_PREFIX);
        if (phpClassPrefix != null) {
            schema.setPhpClassPrefix(phpClassPrefix.getValue().toString());
        }
        OptionElement phpNamespace = options.get(PHP_NAMESPACE);
        if (phpNamespace != null) {
            schema.setPhpNamespace(phpNamespace.getValue().toString());
        }
        OptionElement phpMetadataNamespace = options.get(PHP_METADATA_NAMESPACE);
        if (phpMetadataNamespace != null) {
            schema.setPhpMetadataNamespace(phpMetadataNamespace.getValue().toString());
        }
        OptionElement rubyPackage = options.get(RUBY_PACKAGE);
        if (rubyPackage != null) {
            schema.setRubyPackage(rubyPackage.getValue().toString());
        }
        Optional<OptionElement> meta = findOption(CONFLUENT_FILE_META, options);
        String doc = findDoc(meta);
        Map<String, String> params = findParams(meta);
        schema.setMeta(doc, params);
        schema.setName(name);
        return schema.build();
    } catch (Descriptors.DescriptorValidationException e) {
        throw new IllegalStateException(e);
    }
}
Also used : TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) DynamicSchema(io.confluent.kafka.schemaregistry.protobuf.dynamic.DynamicSchema) EnumDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) MessageDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.MessageDefinition) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Descriptors(com.google.protobuf.Descriptors) Syntax(com.squareup.wire.Syntax) ServiceDefinition(io.confluent.kafka.schemaregistry.protobuf.dynamic.ServiceDefinition)

Aggregations

EnumDefinition (io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition)3 Descriptors (com.google.protobuf.Descriptors)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 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 ByteString (com.google.protobuf.ByteString)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