Search in sources :

Example 1 with DynamicSchema

use of io.confluent.kafka.schemaregistry.protobuf.dynamic.DynamicSchema 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)

Example 2 with DynamicSchema

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

the class ProtobufData method fieldDefinitionFromConnectSchema.

private FieldDefinition fieldDefinitionFromConnectSchema(FromConnectContext ctx, DynamicSchema.Builder schema, MessageDefinition.Builder message, Schema fieldSchema, String name, int tag) {
    String label = null;
    if (fieldSchema.type() == Schema.Type.ARRAY) {
        label = "repeated";
        fieldSchema = fieldSchema.valueSchema();
    } else if (fieldSchema.type() == Schema.Type.MAP) {
        label = "repeated";
    } else if (useOptionalForNullables && fieldSchema.isOptional()) {
        label = "optional";
    }
    Map<String, String> params = new HashMap<>();
    String type = dataTypeFromConnectSchema(ctx, fieldSchema, name, params);
    Object defaultVal = null;
    if (fieldSchema.type() == Schema.Type.STRUCT) {
        String fieldSchemaName = fieldSchema.name();
        if (isUnionSchema(fieldSchema)) {
            String unionName = generalizedSumTypeSupport ? fieldSchema.parameters().get(GENERALIZED_TYPE_UNION) : getUnqualifiedName(ctx, fieldSchemaName.substring(PROTOBUF_TYPE_UNION_PREFIX.length()));
            oneofDefinitionFromConnectSchema(ctx, schema, message, fieldSchema, unionName);
            return null;
        } else {
            if (!ctx.contains(fieldSchemaName)) {
                ctx.add(fieldSchemaName);
                message.addMessageDefinition(messageDefinitionFromConnectSchema(ctx, schema, type, fieldSchema));
            }
        }
    } else if (fieldSchema.type() == Schema.Type.MAP) {
        message.addMessageDefinition(mapDefinitionFromConnectSchema(ctx, schema, type, fieldSchema));
    } else if (fieldSchema.parameters() != null && (fieldSchema.parameters().containsKey(GENERALIZED_TYPE_ENUM) || fieldSchema.parameters().containsKey(PROTOBUF_TYPE_ENUM))) {
        String enumName = getUnqualifiedName(ctx, fieldSchema.name());
        if (!message.containsEnum(enumName)) {
            message.addEnumDefinition(enumDefinitionFromConnectSchema(ctx, schema, fieldSchema));
        }
    } else {
        DynamicSchema dynamicSchema = typeToDynamicSchema(type);
        if (dynamicSchema != null) {
            schema.addSchema(dynamicSchema);
            schema.addDependency(dynamicSchema.getFileDescriptorProto().getName());
        } else {
            defaultVal = fieldSchema.defaultValue();
        }
    }
    return new FieldDefinition(label, type, name, tag, defaultVal != null ? defaultVal.toString() : null, null, params);
}
Also used : BoundedConcurrentHashMap(io.confluent.kafka.schemaregistry.utils.BoundedConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteString(com.google.protobuf.ByteString) DynamicSchema(io.confluent.kafka.schemaregistry.protobuf.dynamic.DynamicSchema)

Aggregations

DynamicSchema (io.confluent.kafka.schemaregistry.protobuf.dynamic.DynamicSchema)2 ByteString (com.google.protobuf.ByteString)1 Descriptors (com.google.protobuf.Descriptors)1 Syntax (com.squareup.wire.Syntax)1 EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)1 MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)1 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)1 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)1 ServiceElement (com.squareup.wire.schema.internal.parser.ServiceElement)1 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)1 EnumDefinition (io.confluent.kafka.schemaregistry.protobuf.dynamic.EnumDefinition)1 MessageDefinition (io.confluent.kafka.schemaregistry.protobuf.dynamic.MessageDefinition)1 ServiceDefinition (io.confluent.kafka.schemaregistry.protobuf.dynamic.ServiceDefinition)1 BoundedConcurrentHashMap (io.confluent.kafka.schemaregistry.utils.BoundedConcurrentHashMap)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1