Search in sources :

Example 21 with EnumElement

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

the class ProtobufSchema method toProtoFile.

private static ProtoFileElement toProtoFile(FileDescriptorProto file) {
    String packageName = file.getPackage();
    // Don't set empty package name
    if ("".equals(packageName)) {
        packageName = null;
    }
    Syntax syntax = null;
    switch(file.getSyntax()) {
        case PROTO2:
            syntax = Syntax.PROTO_2;
            break;
        case PROTO3:
            syntax = Syntax.PROTO_3;
            break;
        default:
            break;
    }
    ImmutableList.Builder<TypeElement> types = ImmutableList.builder();
    for (DescriptorProto md : file.getMessageTypeList()) {
        MessageElement message = toMessage(file, md);
        types.add(message);
    }
    for (EnumDescriptorProto ed : file.getEnumTypeList()) {
        EnumElement enumer = toEnum(ed);
        types.add(enumer);
    }
    ImmutableList.Builder<ServiceElement> services = ImmutableList.builder();
    for (ServiceDescriptorProto sd : file.getServiceList()) {
        ServiceElement service = toService(sd);
        services.add(service);
    }
    ImmutableList.Builder<String> imports = ImmutableList.builder();
    ImmutableList.Builder<String> publicImports = ImmutableList.builder();
    List<String> dependencyList = file.getDependencyList();
    Set<Integer> publicDependencyList = new HashSet<>(file.getPublicDependencyList());
    for (int i = 0; i < dependencyList.size(); i++) {
        String depName = dependencyList.get(i);
        if (publicDependencyList.contains(i)) {
            publicImports.add(depName);
        } else {
            imports.add(depName);
        }
    }
    ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
    if (file.getOptions().hasJavaPackage()) {
        options.add(new OptionElement(JAVA_PACKAGE, Kind.STRING, file.getOptions().getJavaPackage(), false));
    }
    if (file.getOptions().hasJavaOuterClassname()) {
        options.add(new OptionElement(JAVA_OUTER_CLASSNAME, Kind.STRING, file.getOptions().getJavaOuterClassname(), false));
    }
    if (file.getOptions().hasJavaMultipleFiles()) {
        options.add(new OptionElement(JAVA_MULTIPLE_FILES, Kind.BOOLEAN, file.getOptions().getJavaMultipleFiles(), false));
    }
    if (file.getOptions().hasJavaStringCheckUtf8()) {
        options.add(new OptionElement(JAVA_STRING_CHECK_UTF8, Kind.BOOLEAN, file.getOptions().getJavaStringCheckUtf8(), false));
    }
    if (file.getOptions().hasOptimizeFor()) {
        options.add(new OptionElement(OPTIMIZE_FOR, Kind.ENUM, file.getOptions().getOptimizeFor(), false));
    }
    if (file.getOptions().hasGoPackage()) {
        options.add(new OptionElement(GO_PACKAGE, Kind.STRING, file.getOptions().getGoPackage(), false));
    }
    if (file.getOptions().hasCcGenericServices()) {
        options.add(new OptionElement(CC_GENERIC_SERVICES, Kind.BOOLEAN, file.getOptions().getCcGenericServices(), false));
    }
    if (file.getOptions().hasJavaGenericServices()) {
        options.add(new OptionElement(JAVA_GENERIC_SERVICES, Kind.BOOLEAN, file.getOptions().getJavaGenericServices(), false));
    }
    if (file.getOptions().hasPyGenericServices()) {
        options.add(new OptionElement(PY_GENERIC_SERVICES, Kind.BOOLEAN, file.getOptions().getPyGenericServices(), false));
    }
    if (file.getOptions().hasPhpGenericServices()) {
        options.add(new OptionElement(PHP_GENERIC_SERVICES, Kind.BOOLEAN, file.getOptions().getPhpGenericServices(), false));
    }
    if (file.getOptions().hasDeprecated()) {
        options.add(new OptionElement(DEPRECATED, Kind.BOOLEAN, file.getOptions().getDeprecated(), false));
    }
    if (file.getOptions().hasCcEnableArenas()) {
        options.add(new OptionElement(CC_ENABLE_ARENAS, Kind.BOOLEAN, file.getOptions().getCcEnableArenas(), false));
    }
    if (file.getOptions().hasObjcClassPrefix()) {
        options.add(new OptionElement(OBJC_CLASS_PREFIX, Kind.STRING, file.getOptions().getObjcClassPrefix(), false));
    }
    if (file.getOptions().hasCsharpNamespace()) {
        options.add(new OptionElement(CSHARP_NAMESPACE, Kind.STRING, file.getOptions().getCsharpNamespace(), false));
    }
    if (file.getOptions().hasSwiftPrefix()) {
        options.add(new OptionElement(SWIFT_PREFIX, Kind.STRING, file.getOptions().getSwiftPrefix(), false));
    }
    if (file.getOptions().hasPhpClassPrefix()) {
        options.add(new OptionElement(PHP_CLASS_PREFIX, Kind.STRING, file.getOptions().getPhpClassPrefix(), false));
    }
    if (file.getOptions().hasPhpNamespace()) {
        options.add(new OptionElement(PHP_NAMESPACE, Kind.STRING, file.getOptions().getPhpNamespace(), false));
    }
    if (file.getOptions().hasPhpMetadataNamespace()) {
        options.add(new OptionElement(PHP_METADATA_NAMESPACE, Kind.STRING, file.getOptions().getPhpMetadataNamespace(), false));
    }
    if (file.getOptions().hasRubyPackage()) {
        options.add(new OptionElement(RUBY_PACKAGE, Kind.STRING, file.getOptions().getRubyPackage(), false));
    }
    if (file.getOptions().hasExtension(MetaProto.fileMeta)) {
        Meta meta = file.getOptions().getExtension(MetaProto.fileMeta);
        OptionElement option = toOption(CONFLUENT_FILE_META, meta);
        if (option != null) {
            options.add(option);
        }
    }
    // NOTE: skip extensions
    return new ProtoFileElement(DEFAULT_LOCATION, packageName, syntax, imports.build(), publicImports.build(), types.build(), services.build(), Collections.emptyList(), options.build());
}
Also used : EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) Meta(io.confluent.protobuf.MetaProto.Meta) ImmutableList(com.google.common.collect.ImmutableList) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Syntax(com.squareup.wire.Syntax) HashSet(java.util.HashSet)

Example 22 with EnumElement

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

Example 23 with EnumElement

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

the class ProtobufSchema method toMessage.

private static MessageElement toMessage(FileDescriptorProto file, DescriptorProto descriptor) {
    String name = descriptor.getName();
    log.trace("*** msg name: {}", name);
    ImmutableList.Builder<FieldElement> fields = ImmutableList.builder();
    ImmutableList.Builder<TypeElement> nested = ImmutableList.builder();
    ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
    LinkedHashMap<String, ImmutableList.Builder<FieldElement>> oneofsMap = new LinkedHashMap<>();
    for (OneofDescriptorProto od : descriptor.getOneofDeclList()) {
        oneofsMap.put(od.getName(), ImmutableList.builder());
    }
    List<Map.Entry<String, ImmutableList.Builder<FieldElement>>> oneofs = new ArrayList<>(oneofsMap.entrySet());
    for (FieldDescriptorProto fd : descriptor.getFieldList()) {
        if (fd.hasOneofIndex() && !fd.getProto3Optional()) {
            FieldElement field = toField(file, fd, true);
            oneofs.get(fd.getOneofIndex()).getValue().add(field);
        } else {
            FieldElement field = toField(file, fd, false);
            fields.add(field);
        }
    }
    for (DescriptorProto nestedDesc : descriptor.getNestedTypeList()) {
        MessageElement nestedMessage = toMessage(file, nestedDesc);
        nested.add(nestedMessage);
    }
    for (EnumDescriptorProto nestedDesc : descriptor.getEnumTypeList()) {
        EnumElement nestedEnum = toEnum(nestedDesc);
        nested.add(nestedEnum);
    }
    for (ReservedRange range : descriptor.getReservedRangeList()) {
        ReservedElement reservedElem = toReserved(range);
        reserved.add(reservedElem);
    }
    for (String reservedName : descriptor.getReservedNameList()) {
        ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
        reserved.add(reservedElem);
    }
    ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
    if (descriptor.getOptions().hasNoStandardDescriptorAccessor()) {
        OptionElement option = new OptionElement(NO_STANDARD_DESCRIPTOR_ACCESSOR, Kind.BOOLEAN, descriptor.getOptions().getNoStandardDescriptorAccessor(), false);
        options.add(option);
    }
    if (descriptor.getOptions().hasDeprecated()) {
        OptionElement option = new OptionElement(DEPRECATED, Kind.BOOLEAN, descriptor.getOptions().getDeprecated(), false);
        options.add(option);
    }
    if (descriptor.getOptions().hasMapEntry()) {
        OptionElement option = new OptionElement(MAP_ENTRY, Kind.BOOLEAN, descriptor.getOptions().getMapEntry(), false);
        options.add(option);
    }
    if (descriptor.getOptions().hasExtension(MetaProto.messageMeta)) {
        Meta meta = descriptor.getOptions().getExtension(MetaProto.messageMeta);
        OptionElement option = toOption(CONFLUENT_MESSAGE_META, meta);
        if (option != null) {
            options.add(option);
        }
    }
    // NOTE: skip extensions, groups
    return new MessageElement(DEFAULT_LOCATION, name, "", nested.build(), options.build(), reserved.build(), fields.build(), oneofs.stream().map(e -> toOneof(e.getKey(), e.getValue())).filter(e -> !e.getFields().isEmpty()).collect(Collectors.toList()), Collections.emptyList(), Collections.emptyList());
}
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) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) Meta(io.confluent.protobuf.MetaProto.Meta) ImmutableList(com.google.common.collect.ImmutableList) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) ReservedRange(com.google.protobuf.DescriptorProtos.DescriptorProto.ReservedRange) EnumReservedRange(com.google.protobuf.DescriptorProtos.EnumDescriptorProto.EnumReservedRange) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement)

Example 24 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, EnumElement type, boolean normalize) {
    StringBuilder sb = new StringBuilder();
    sb.append("enum ");
    sb.append(type.getName());
    sb.append(" {");
    if (!type.getReserveds().isEmpty()) {
        sb.append('\n');
        List<ReservedElement> reserveds = type.getReserveds();
        if (normalize) {
            reserveds = reserveds.stream().flatMap(r -> r.getValues().stream().map(o -> new ReservedElement(r.getLocation(), r.getDocumentation(), Collections.singletonList(o)))).collect(Collectors.toList());
            Comparator<Object> cmp = Comparator.comparing(r -> {
                Object o = ((ReservedElement) r).getValues().get(0);
                if (o instanceof IntRange) {
                    return ((IntRange) o).getStart();
                } else if (o instanceof Integer) {
                    return (Integer) o;
                } else {
                    return Integer.MAX_VALUE;
                }
            }).thenComparing(r -> ((ReservedElement) r).getValues().get(0).toString());
            reserveds.sort(cmp);
        }
        for (ReservedElement reserved : reserveds) {
            appendIndented(sb, toString(ctx, reserved, normalize));
        }
    }
    if (type.getReserveds().isEmpty() && (!type.getOptions().isEmpty() || !type.getConstants().isEmpty())) {
        sb.append('\n');
    }
    if (!type.getOptions().isEmpty()) {
        List<OptionElement> options = type.getOptions();
        if (normalize) {
            options = new ArrayList<>(options);
            options.sort(Comparator.comparing(OptionElement::getName));
        }
        for (OptionElement option : options) {
            appendIndented(sb, toOptionString(option, normalize));
        }
    }
    if (!type.getConstants().isEmpty()) {
        List<EnumConstantElement> constants = type.getConstants();
        if (normalize) {
            constants = new ArrayList<>(constants);
            constants.sort(Comparator.comparing(EnumConstantElement::getTag).thenComparing(EnumConstantElement::getName));
        }
        for (EnumConstantElement constant : constants) {
            appendIndented(sb, toString(ctx, constant, normalize));
        }
    }
    sb.append("}\n");
    return sb.toString();
}
Also used : Arrays(java.util.Arrays) DynamicMessage(com.google.protobuf.DynamicMessage) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) Label(com.squareup.wire.schema.Field.Label) ArrayList(java.util.ArrayList) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) Locale(java.util.Locale) Map(java.util.Map) ExtendElement(com.squareup.wire.schema.internal.parser.ExtendElement) JsonNode(com.fasterxml.jackson.databind.JsonNode) IntRange(kotlin.ranges.IntRange) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GroupElement(com.squareup.wire.schema.internal.parser.GroupElement) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) Ascii(com.google.common.base.Ascii) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) JacksonMapper(io.confluent.kafka.schemaregistry.utils.JacksonMapper) ProtoType(com.squareup.wire.schema.ProtoType) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) Context(io.confluent.kafka.schemaregistry.protobuf.diff.Context) List(java.util.List) JsonFormat(com.google.protobuf.util.JsonFormat) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) TypeElementInfo(io.confluent.kafka.schemaregistry.protobuf.diff.Context.TypeElementInfo) Message(com.google.protobuf.Message) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) Kind(com.squareup.wire.schema.internal.parser.OptionElement.Kind) Comparator(java.util.Comparator) Collections(java.util.Collections) MAX_TAG_VALUE(com.squareup.wire.schema.internal.UtilKt.MAX_TAG_VALUE) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) IntRange(kotlin.ranges.IntRange) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement)

Example 25 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, MessageElement type, boolean normalize) {
    StringBuilder sb = new StringBuilder();
    sb.append("message ");
    sb.append(type.getName());
    sb.append(" {");
    if (!type.getReserveds().isEmpty()) {
        sb.append('\n');
        List<ReservedElement> reserveds = type.getReserveds();
        if (normalize) {
            reserveds = reserveds.stream().flatMap(r -> r.getValues().stream().map(o -> new ReservedElement(r.getLocation(), r.getDocumentation(), Collections.singletonList(o)))).collect(Collectors.toList());
            Comparator<Object> cmp = Comparator.comparing(r -> {
                Object o = ((ReservedElement) r).getValues().get(0);
                if (o instanceof IntRange) {
                    return ((IntRange) o).getStart();
                } else if (o instanceof Integer) {
                    return (Integer) o;
                } else {
                    return Integer.MAX_VALUE;
                }
            }).thenComparing(r -> ((ReservedElement) r).getValues().get(0).toString());
            reserveds.sort(cmp);
        }
        for (ReservedElement reserved : reserveds) {
            appendIndented(sb, toString(ctx, reserved, normalize));
        }
    }
    if (!type.getOptions().isEmpty()) {
        sb.append('\n');
        List<OptionElement> options = type.getOptions();
        if (normalize) {
            options = new ArrayList<>(options);
            options.sort(Comparator.comparing(OptionElement::getName));
        }
        for (OptionElement option : options) {
            appendIndented(sb, toOptionString(option, normalize));
        }
    }
    if (!type.getFields().isEmpty()) {
        sb.append('\n');
        List<FieldElement> fields = type.getFields();
        if (normalize) {
            fields = new ArrayList<>(fields);
            fields.sort(Comparator.comparing(FieldElement::getTag));
        }
        for (FieldElement field : fields) {
            appendIndented(sb, toString(ctx, field, normalize));
        }
    }
    if (!type.getOneOfs().isEmpty()) {
        sb.append('\n');
        List<OneOfElement> oneOfs = type.getOneOfs();
        if (normalize) {
            oneOfs = oneOfs.stream().filter(o -> !o.getFields().isEmpty()).map(o -> {
                List<FieldElement> fields = new ArrayList<>(o.getFields());
                fields.sort(Comparator.comparing(FieldElement::getTag));
                return new OneOfElement(o.getName(), o.getDocumentation(), fields, o.getGroups(), o.getOptions());
            }).collect(Collectors.toList());
            oneOfs.sort(Comparator.comparing(o -> o.getFields().get(0).getTag()));
        }
        for (OneOfElement oneOf : oneOfs) {
            appendIndented(sb, toString(ctx, oneOf, normalize));
        }
    }
    if (!type.getGroups().isEmpty()) {
        sb.append('\n');
        for (GroupElement group : type.getGroups()) {
            appendIndented(sb, group.toSchema());
        }
    }
    if (!type.getExtensions().isEmpty()) {
        sb.append('\n');
        for (ExtensionsElement extension : type.getExtensions()) {
            appendIndented(sb, extension.toSchema());
        }
    }
    if (!type.getNestedTypes().isEmpty()) {
        sb.append('\n');
        // the non-normalized schema to serialize message indexes
        for (TypeElement typeElement : type.getNestedTypes()) {
            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())) {
                    appendIndented(sb, toString(ctx, (MessageElement) typeElement, normalize));
                }
            }
        }
        for (TypeElement typeElement : type.getNestedTypes()) {
            if (typeElement instanceof EnumElement) {
                try (Context.NamedScope nameScope = ctx.enterName(typeElement.getName())) {
                    appendIndented(sb, toString(ctx, (EnumElement) typeElement, normalize));
                }
            }
        }
    }
    sb.append("}\n");
    return sb.toString();
}
Also used : Arrays(java.util.Arrays) DynamicMessage(com.google.protobuf.DynamicMessage) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) Label(com.squareup.wire.schema.Field.Label) ArrayList(java.util.ArrayList) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) Locale(java.util.Locale) Map(java.util.Map) ExtendElement(com.squareup.wire.schema.internal.parser.ExtendElement) JsonNode(com.fasterxml.jackson.databind.JsonNode) IntRange(kotlin.ranges.IntRange) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GroupElement(com.squareup.wire.schema.internal.parser.GroupElement) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) Ascii(com.google.common.base.Ascii) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) JacksonMapper(io.confluent.kafka.schemaregistry.utils.JacksonMapper) ProtoType(com.squareup.wire.schema.ProtoType) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) Context(io.confluent.kafka.schemaregistry.protobuf.diff.Context) List(java.util.List) JsonFormat(com.google.protobuf.util.JsonFormat) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) TypeElementInfo(io.confluent.kafka.schemaregistry.protobuf.diff.Context.TypeElementInfo) Message(com.google.protobuf.Message) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) Kind(com.squareup.wire.schema.internal.parser.OptionElement.Kind) Comparator(java.util.Comparator) Collections(java.util.Collections) MAX_TAG_VALUE(com.squareup.wire.schema.internal.UtilKt.MAX_TAG_VALUE) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) Context(io.confluent.kafka.schemaregistry.protobuf.diff.Context) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) GroupElement(com.squareup.wire.schema.internal.parser.GroupElement) IntRange(kotlin.ranges.IntRange) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ArrayList(java.util.ArrayList) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) TypeElementInfo(io.confluent.kafka.schemaregistry.protobuf.diff.Context.TypeElementInfo) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement)

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