Search in sources :

Example 1 with ExtensionsElement

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

the class FileDescriptorUtils method messageElementToDescriptorProto.

private static DescriptorProto messageElementToDescriptorProto(MessageType messageElem, Schema schema, ProtoFile element) {
    ProtobufMessage message = new ProtobufMessage();
    message.protoBuilder().setName(messageElem.getType().getSimpleName());
    Comparator<Location> locationComparator = Comparator.comparing(Location::getLine).thenComparing(Location::getColumn);
    Map<Location, DescriptorProto> allNestedTypes = new TreeMap<>(locationComparator);
    List<FieldDescriptorProto> allFields = new ArrayList<>();
    for (Type type : messageElem.getNestedTypes()) {
        if (type instanceof MessageType) {
            allNestedTypes.put(type.getLocation(), messageElementToDescriptorProto((MessageType) type, schema, element));
        } else if (type instanceof EnumType) {
            message.protoBuilder().addEnumType(enumElementToProto((EnumType) type));
        }
    }
    final Predicate<Field> isProto3Optional = field -> Field.Label.OPTIONAL.equals(field.getLabel()) && Syntax.PROTO_3.equals(element.getSyntax());
    final List<OneOf> oneOfs = messageElem.getOneOfs();
    final List<OneOf> proto3OptionalOneOfs = messageElem.getFieldsAndOneOfFields().stream().filter(isProto3Optional).map(FileDescriptorUtils::getProto3OptionalField).collect(Collectors.toList());
    // Proto3 Optionals are considered as "synthetic-oneofs" by Protobuf compiler.
    oneOfs.addAll(proto3OptionalOneOfs);
    final Function<String, Optional<OneOf>> findOneOfByFieldName = fieldName -> {
        for (OneOf oneOf : oneOfs) {
            if (oneOf.getFields().stream().map(Field::getName).anyMatch(f -> f.equals(fieldName))) {
                return Optional.of(oneOf);
            }
        }
        return Optional.empty();
    };
    // Add all the declared fields first skipping oneOfs.
    for (final Field field : messageElem.getDeclaredFields()) {
        final Optional<OneOf> optionalOneOf = findOneOfByFieldName.apply(field.getName());
        if (!optionalOneOf.isPresent()) {
            Field.Label fieldLabel = field.getLabel();
            // Fields are optional by default in Proto3.
            String label = fieldLabel != null ? fieldLabel.toString().toLowerCase() : OPTIONAL;
            String fieldType = determineFieldType(field.getType(), schema);
            ProtoType protoType = field.getType();
            String fieldTypeName = String.valueOf(protoType);
            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 = "message";
                String fieldMapEntryName = toMapEntry(field.getName());
                // Map entry field name is capitalized
                fieldMapEntryName = fieldMapEntryName.substring(0, 1).toUpperCase() + fieldMapEntryName.substring(1);
                // Map field type name is resolved with reference to the package
                fieldTypeName = String.format("%s.%s", messageElem.getType(), fieldMapEntryName);
                ProtobufMessage protobufMapMessage = new ProtobufMessage();
                DescriptorProto.Builder mapMessage = protobufMapMessage.protoBuilder().setName(fieldMapEntryName).mergeOptions(DescriptorProtos.MessageOptions.newBuilder().setMapEntry(true).build());
                protobufMapMessage.addField(OPTIONAL, determineFieldType(keyType, schema), String.valueOf(keyType), KEY_FIELD, 1, null, null, null, null, null, null, null, null);
                protobufMapMessage.addField(OPTIONAL, determineFieldType(valueType, schema), String.valueOf(valueType), VALUE_FIELD, 2, null, null, null, null, null, null, null, null);
                allNestedTypes.put(field.getLocation(), mapMessage.build());
            }
            String jsonName = getDefaultJsonName(field.getName()).equals(field.getDeclaredJsonName()) ? null : field.getDeclaredJsonName();
            Boolean isDeprecated = findOptionBoolean(DEPRECATED_OPTION, field.getOptions());
            Boolean isPacked = findOptionBoolean(PACKED_OPTION, field.getOptions());
            DescriptorProtos.FieldOptions.CType cType = findOption(CTYPE_OPTION, field.getOptions()).map(o -> DescriptorProtos.FieldOptions.CType.valueOf(o.getValue().toString())).orElse(null);
            DescriptorProtos.FieldOptions.JSType jsType = findOption(JSTYPE_OPTION, field.getOptions()).map(o -> DescriptorProtos.FieldOptions.JSType.valueOf(o.getValue().toString())).orElse(null);
            allFields.add(ProtobufMessage.buildFieldDescriptorProto(label, fieldType, fieldTypeName, field.getName(), field.getTag(), field.getDefault(), jsonName, isDeprecated, isPacked, cType, jsType, null, null));
        }
    }
    final Set<OneOf> addedOneOfs = new LinkedHashSet<>();
    // Add the oneOfs next including Proto3 Optionals.
    for (final OneOf oneOf : oneOfs) {
        if (addedOneOfs.contains(oneOf)) {
            continue;
        }
        Boolean isProto3OptionalField = null;
        if (proto3OptionalOneOfs.contains(oneOf)) {
            isProto3OptionalField = true;
        }
        OneofDescriptorProto.Builder oneofBuilder = OneofDescriptorProto.newBuilder().setName(oneOf.getName());
        message.protoBuilder().addOneofDecl(oneofBuilder);
        for (Field oneOfField : oneOf.getFields()) {
            String oneOfJsonName = getDefaultJsonName(oneOfField.getName()).equals(oneOfField.getDeclaredJsonName()) ? null : oneOfField.getDeclaredJsonName();
            Boolean oneOfIsDeprecated = findOptionBoolean(DEPRECATED_OPTION, oneOfField.getOptions());
            Boolean oneOfIsPacked = findOptionBoolean(PACKED_OPTION, oneOfField.getOptions());
            DescriptorProtos.FieldOptions.CType oneOfCType = findOption(CTYPE_OPTION, oneOfField.getOptions()).map(o -> DescriptorProtos.FieldOptions.CType.valueOf(o.getValue().toString())).orElse(null);
            DescriptorProtos.FieldOptions.JSType oneOfJsType = findOption(JSTYPE_OPTION, oneOfField.getOptions()).map(o -> DescriptorProtos.FieldOptions.JSType.valueOf(o.getValue().toString())).orElse(null);
            allFields.add(ProtobufMessage.buildFieldDescriptorProto(OPTIONAL, determineFieldType(oneOfField.getType(), schema), String.valueOf(oneOfField.getType()), oneOfField.getName(), oneOfField.getTag(), oneOfField.getDefault(), oneOfJsonName, oneOfIsDeprecated, oneOfIsPacked, oneOfCType, oneOfJsType, message.protoBuilder().getOneofDeclCount() - 1, isProto3OptionalField));
        }
        addedOneOfs.add(oneOf);
    }
    for (ReservedElement reserved : messageElem.toElement().getReserveds()) {
        for (Object elem : reserved.getValues()) {
            if (elem instanceof String) {
                message.protoBuilder().addReservedName((String) elem);
            } else if (elem instanceof Integer) {
                int tag = (Integer) elem;
                DescriptorProto.ReservedRange.Builder rangeBuilder = DescriptorProto.ReservedRange.newBuilder().setStart(tag).setEnd(tag + 1);
                message.protoBuilder().addReservedRange(rangeBuilder.build());
            } else if (elem instanceof IntRange) {
                IntRange range = (IntRange) elem;
                DescriptorProto.ReservedRange.Builder rangeBuilder = DescriptorProto.ReservedRange.newBuilder().setStart(range.getStart()).setEnd(range.getEndInclusive() + 1);
                message.protoBuilder().addReservedRange(rangeBuilder.build());
            } else {
                throw new IllegalStateException("Unsupported reserved type: " + elem.getClass().getName());
            }
        }
    }
    for (ExtensionsElement extensions : messageElem.toElement().getExtensions()) {
        for (Object elem : extensions.getValues()) {
            if (elem instanceof Integer) {
                int tag = (Integer) elem;
                DescriptorProto.ExtensionRange.Builder extensionBuilder = DescriptorProto.ExtensionRange.newBuilder().setStart(tag).setEnd(tag + 1);
                message.protoBuilder().addExtensionRange(extensionBuilder.build());
            } else if (elem instanceof IntRange) {
                IntRange range = (IntRange) elem;
                DescriptorProto.ExtensionRange.Builder extensionBuilder = DescriptorProto.ExtensionRange.newBuilder().setStart(range.getStart()).setEnd(range.getEndInclusive() + 1);
                message.protoBuilder().addExtensionRange(extensionBuilder.build());
            } else {
                throw new IllegalStateException("Unsupported extension type: " + elem.getClass().getName());
            }
        }
    }
    Boolean isMapEntry = findOptionBoolean(MAP_ENTRY_OPTION, messageElem.getOptions());
    if (isMapEntry != null) {
        DescriptorProtos.MessageOptions.Builder optionsBuilder = DescriptorProtos.MessageOptions.newBuilder().setMapEntry(isMapEntry);
        message.protoBuilder().mergeOptions(optionsBuilder.build());
    }
    Boolean noStandardDescriptorAccessor = findOptionBoolean(NO_STANDARD_DESCRIPTOR_OPTION, messageElem.getOptions());
    if (noStandardDescriptorAccessor != null) {
        DescriptorProtos.MessageOptions.Builder optionsBuilder = DescriptorProtos.MessageOptions.newBuilder().setNoStandardDescriptorAccessor(noStandardDescriptorAccessor);
        message.protoBuilder().mergeOptions(optionsBuilder.build());
    }
    message.protoBuilder().addAllNestedType(allNestedTypes.values());
    message.protoBuilder().addAllField(allFields);
    return message.build();
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) QuaternionProto(com.google.type.QuaternionProto) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) EmptyProto(com.google.protobuf.EmptyProto) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) 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) EnumConstant(com.squareup.wire.schema.EnumConstant) StructProto(com.google.protobuf.StructProto) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) DurationProto(com.google.protobuf.DurationProto) FieldMaskProto(com.google.protobuf.FieldMaskProto) 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) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) SourceContextProto(com.google.protobuf.SourceContextProto) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) TypeProto(com.google.protobuf.TypeProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) AnyProto(com.google.protobuf.AnyProto) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) WrappersProto(com.google.protobuf.WrappersProto) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) ApiProto(com.google.protobuf.ApiProto) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) TimestampProto(com.google.protobuf.TimestampProto) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ProtoType(com.squareup.wire.schema.ProtoType) Field(com.squareup.wire.schema.Field) EnumType(com.squareup.wire.schema.EnumType) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) 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) MessageType(com.squareup.wire.schema.MessageType) Optional(java.util.Optional) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) IntRange(kotlin.ranges.IntRange) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) TreeMap(java.util.TreeMap) OneOf(com.squareup.wire.schema.OneOf) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) EnumType(com.squareup.wire.schema.EnumType) Location(com.squareup.wire.schema.Location)

Example 2 with ExtensionsElement

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

the class FileDescriptorUtils method toMessage.

private static MessageElement toMessage(FileDescriptorProto file, DescriptorProto descriptor) {
    String name = descriptor.getName();
    ImmutableList.Builder<FieldElement> fields = ImmutableList.builder();
    ImmutableList.Builder<TypeElement> nested = ImmutableList.builder();
    ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
    ImmutableList.Builder<ExtensionsElement> extensions = 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());
    List<FieldElement> proto3OptionalFields = new ArrayList<>();
    for (FieldDescriptorProto fd : descriptor.getFieldList()) {
        if (fd.hasProto3Optional()) {
            proto3OptionalFields.add(toField(file, fd, false));
            continue;
        }
        if (fd.hasOneofIndex()) {
            FieldElement field = toField(file, fd, true);
            oneofs.get(fd.getOneofIndex()).getValue().add(field);
        } else {
            FieldElement field = toField(file, fd, false);
            fields.add(field);
        }
    }
    fields.addAll(proto3OptionalFields);
    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 (String reservedName : descriptor.getReservedNameList()) {
        ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
        reserved.add(reservedElem);
    }
    for (DescriptorProto.ReservedRange reservedRange : descriptor.getReservedRangeList()) {
        List<IntRange> values = new ArrayList<>();
        int start = reservedRange.getStart();
        int end = reservedRange.getEnd() - 1;
        values.add(new IntRange(start, end));
        ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", values);
        reserved.add(reservedElem);
    }
    for (DescriptorProto.ExtensionRange extensionRange : descriptor.getExtensionRangeList()) {
        List<IntRange> values = new ArrayList<>();
        int start = extensionRange.getStart();
        int end = extensionRange.getEnd() - 1;
        values.add(new IntRange(start, end));
        ExtensionsElement extensionsElement = new ExtensionsElement(DEFAULT_LOCATION, "", values);
        extensions.add(extensionsElement);
    }
    ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
    if (descriptor.getOptions().hasMapEntry()) {
        OptionElement option = new OptionElement(MAP_ENTRY_OPTION, booleanKind, descriptor.getOptions().getMapEntry(), false);
        options.add(option);
    }
    if (descriptor.getOptions().hasNoStandardDescriptorAccessor()) {
        OptionElement option = new OptionElement(NO_STANDARD_DESCRIPTOR_OPTION, booleanKind, descriptor.getOptions().getNoStandardDescriptorAccessor(), false);
        options.add(option);
    }
    return new MessageElement(DEFAULT_LOCATION, name, "", nested.build(), options.build(), reserved.build(), fields.build(), oneofs.stream().filter(e -> e.getValue().build().size() != 0).map(e -> toOneof(e.getKey(), e.getValue())).collect(Collectors.toList()), extensions.build(), Collections.emptyList());
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) QuaternionProto(com.google.type.QuaternionProto) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) EmptyProto(com.google.protobuf.EmptyProto) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) 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) EnumConstant(com.squareup.wire.schema.EnumConstant) StructProto(com.google.protobuf.StructProto) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) DurationProto(com.google.protobuf.DurationProto) FieldMaskProto(com.google.protobuf.FieldMaskProto) 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) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) SourceContextProto(com.google.protobuf.SourceContextProto) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) TypeProto(com.google.protobuf.TypeProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) AnyProto(com.google.protobuf.AnyProto) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) WrappersProto(com.google.protobuf.WrappersProto) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) ApiProto(com.google.protobuf.ApiProto) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) TimestampProto(com.google.protobuf.TimestampProto) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) ImmutableList(com.google.common.collect.ImmutableList) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) 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) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) IntRange(kotlin.ranges.IntRange) 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 3 with ExtensionsElement

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

the class FileDescriptorUtils method messageElementToDescriptorProto.

private static DescriptorProto messageElementToDescriptorProto(MessageType messageElem, Schema schema, ProtoFile element) {
    ProtobufMessage message = new ProtobufMessage();
    message.protoBuilder().setName(messageElem.getType().getSimpleName());
    Comparator<Location> locationComparator = Comparator.comparing(Location::getLine).thenComparing(Location::getColumn);
    Map<Location, DescriptorProto> allNestedTypes = new TreeMap<>(locationComparator);
    List<FieldDescriptorProto> allFields = new ArrayList<>();
    for (Type type : messageElem.getNestedTypes()) {
        if (type instanceof MessageType) {
            allNestedTypes.put(type.getLocation(), messageElementToDescriptorProto((MessageType) type, schema, element));
        } else if (type instanceof EnumType) {
            message.protoBuilder().addEnumType(enumElementToProto((EnumType) type));
        }
    }
    final Predicate<Field> isProto3Optional = field -> Field.Label.OPTIONAL.equals(field.getLabel()) && Syntax.PROTO_3.equals(element.getSyntax());
    final List<OneOf> oneOfs = messageElem.getOneOfs();
    final List<OneOf> proto3OptionalOneOfs = messageElem.getFieldsAndOneOfFields().stream().filter(isProto3Optional).map(FileDescriptorUtils::getProto3OptionalField).collect(Collectors.toList());
    // Proto3 Optionals are considered as "synthetic-oneofs" by Protobuf compiler.
    oneOfs.addAll(proto3OptionalOneOfs);
    final Function<String, Optional<OneOf>> findOneOfByFieldName = fieldName -> {
        for (OneOf oneOf : oneOfs) {
            if (oneOf.getFields().stream().map(Field::getName).anyMatch(f -> f.equals(fieldName))) {
                return Optional.of(oneOf);
            }
        }
        return Optional.empty();
    };
    // Add all the declared fields first skipping oneOfs.
    for (final Field field : messageElem.getDeclaredFields()) {
        final Optional<OneOf> optionalOneOf = findOneOfByFieldName.apply(field.getName());
        if (!optionalOneOf.isPresent()) {
            Field.Label fieldLabel = field.getLabel();
            // Fields are optional by default in Proto3.
            String label = fieldLabel != null ? fieldLabel.toString().toLowerCase() : OPTIONAL;
            String fieldType = determineFieldType(field.getType(), schema);
            ProtoType protoType = field.getType();
            String fieldTypeName = String.valueOf(protoType);
            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 = "message";
                String fieldMapEntryName = toMapEntry(field.getName());
                // Map entry field name is capitalized
                fieldMapEntryName = fieldMapEntryName.substring(0, 1).toUpperCase() + fieldMapEntryName.substring(1);
                // Map field type name is resolved with reference to the package
                fieldTypeName = String.format("%s.%s", messageElem.getType(), fieldMapEntryName);
                ProtobufMessage protobufMapMessage = new ProtobufMessage();
                DescriptorProto.Builder mapMessage = protobufMapMessage.protoBuilder().setName(fieldMapEntryName).mergeOptions(DescriptorProtos.MessageOptions.newBuilder().setMapEntry(true).build());
                protobufMapMessage.addField(OPTIONAL, determineFieldType(keyType, schema), String.valueOf(keyType), KEY_FIELD, 1, null, null, null, null, null, null, null, null, null, null);
                protobufMapMessage.addField(OPTIONAL, determineFieldType(valueType, schema), String.valueOf(valueType), VALUE_FIELD, 2, null, null, null, null, null, null, null, null, null, null);
                allNestedTypes.put(field.getLocation(), mapMessage.build());
            }
            String jsonName = getDefaultJsonName(field.getName()).equals(field.getDeclaredJsonName()) ? null : field.getDeclaredJsonName();
            Boolean isDeprecated = findOptionBoolean(DEPRECATED_OPTION, field.getOptions());
            Boolean isPacked = findOptionBoolean(PACKED_OPTION, field.getOptions());
            DescriptorProtos.FieldOptions.CType cType = findOption(CTYPE_OPTION, field.getOptions()).map(o -> DescriptorProtos.FieldOptions.CType.valueOf(o.getValue().toString())).orElse(null);
            DescriptorProtos.FieldOptions.JSType jsType = findOption(JSTYPE_OPTION, field.getOptions()).map(o -> DescriptorProtos.FieldOptions.JSType.valueOf(o.getValue().toString())).orElse(null);
            String metadataKey = findOptionString(ProtobufSchemaMetadata.metadataKey.getDescriptor().getFullName(), field.getOptions());
            String metadataValue = findOptionString(ProtobufSchemaMetadata.metadataValue.getDescriptor().getFullName(), field.getOptions());
            allFields.add(ProtobufMessage.buildFieldDescriptorProto(label, fieldType, fieldTypeName, field.getName(), field.getTag(), field.getDefault(), jsonName, isDeprecated, isPacked, cType, jsType, metadataKey, metadataValue, null, null));
        }
    }
    final Set<OneOf> addedOneOfs = new LinkedHashSet<>();
    // Add the oneOfs next including Proto3 Optionals.
    for (final OneOf oneOf : oneOfs) {
        if (addedOneOfs.contains(oneOf)) {
            continue;
        }
        Boolean isProto3OptionalField = null;
        if (proto3OptionalOneOfs.contains(oneOf)) {
            isProto3OptionalField = true;
        }
        OneofDescriptorProto.Builder oneofBuilder = OneofDescriptorProto.newBuilder().setName(oneOf.getName());
        message.protoBuilder().addOneofDecl(oneofBuilder);
        for (Field oneOfField : oneOf.getFields()) {
            String oneOfJsonName = getDefaultJsonName(oneOfField.getName()).equals(oneOfField.getDeclaredJsonName()) ? null : oneOfField.getDeclaredJsonName();
            Boolean oneOfIsDeprecated = findOptionBoolean(DEPRECATED_OPTION, oneOfField.getOptions());
            Boolean oneOfIsPacked = findOptionBoolean(PACKED_OPTION, oneOfField.getOptions());
            DescriptorProtos.FieldOptions.CType oneOfCType = findOption(CTYPE_OPTION, oneOfField.getOptions()).map(o -> DescriptorProtos.FieldOptions.CType.valueOf(o.getValue().toString())).orElse(null);
            DescriptorProtos.FieldOptions.JSType oneOfJsType = findOption(JSTYPE_OPTION, oneOfField.getOptions()).map(o -> DescriptorProtos.FieldOptions.JSType.valueOf(o.getValue().toString())).orElse(null);
            String metadataKey = findOptionString(ProtobufSchemaMetadata.metadataKey.getDescriptor().getFullName(), oneOfField.getOptions());
            String metadataValue = findOptionString(ProtobufSchemaMetadata.metadataValue.getDescriptor().getFullName(), oneOfField.getOptions());
            allFields.add(ProtobufMessage.buildFieldDescriptorProto(OPTIONAL, determineFieldType(oneOfField.getType(), schema), String.valueOf(oneOfField.getType()), oneOfField.getName(), oneOfField.getTag(), oneOfField.getDefault(), oneOfJsonName, oneOfIsDeprecated, oneOfIsPacked, oneOfCType, oneOfJsType, metadataKey, metadataValue, message.protoBuilder().getOneofDeclCount() - 1, isProto3OptionalField));
        }
        addedOneOfs.add(oneOf);
    }
    for (ReservedElement reserved : messageElem.toElement().getReserveds()) {
        for (Object elem : reserved.getValues()) {
            if (elem instanceof String) {
                message.protoBuilder().addReservedName((String) elem);
            } else if (elem instanceof Integer) {
                int tag = (Integer) elem;
                DescriptorProto.ReservedRange.Builder rangeBuilder = DescriptorProto.ReservedRange.newBuilder().setStart(tag).setEnd(tag + 1);
                message.protoBuilder().addReservedRange(rangeBuilder.build());
            } else if (elem instanceof IntRange) {
                IntRange range = (IntRange) elem;
                DescriptorProto.ReservedRange.Builder rangeBuilder = DescriptorProto.ReservedRange.newBuilder().setStart(range.getStart()).setEnd(range.getEndInclusive() + 1);
                message.protoBuilder().addReservedRange(rangeBuilder.build());
            } else {
                throw new IllegalStateException("Unsupported reserved type: " + elem.getClass().getName());
            }
        }
    }
    for (ExtensionsElement extensions : messageElem.toElement().getExtensions()) {
        for (Object elem : extensions.getValues()) {
            if (elem instanceof Integer) {
                int tag = (Integer) elem;
                DescriptorProto.ExtensionRange.Builder extensionBuilder = DescriptorProto.ExtensionRange.newBuilder().setStart(tag).setEnd(tag + 1);
                message.protoBuilder().addExtensionRange(extensionBuilder.build());
            } else if (elem instanceof IntRange) {
                IntRange range = (IntRange) elem;
                DescriptorProto.ExtensionRange.Builder extensionBuilder = DescriptorProto.ExtensionRange.newBuilder().setStart(range.getStart()).setEnd(range.getEndInclusive() + 1);
                message.protoBuilder().addExtensionRange(extensionBuilder.build());
            } else {
                throw new IllegalStateException("Unsupported extension type: " + elem.getClass().getName());
            }
        }
    }
    Boolean isMapEntry = findOptionBoolean(MAP_ENTRY_OPTION, messageElem.getOptions());
    if (isMapEntry != null) {
        DescriptorProtos.MessageOptions.Builder optionsBuilder = DescriptorProtos.MessageOptions.newBuilder().setMapEntry(isMapEntry);
        message.protoBuilder().mergeOptions(optionsBuilder.build());
    }
    Boolean noStandardDescriptorAccessor = findOptionBoolean(NO_STANDARD_DESCRIPTOR_OPTION, messageElem.getOptions());
    if (noStandardDescriptorAccessor != null) {
        DescriptorProtos.MessageOptions.Builder optionsBuilder = DescriptorProtos.MessageOptions.newBuilder().setNoStandardDescriptorAccessor(noStandardDescriptorAccessor);
        message.protoBuilder().mergeOptions(optionsBuilder.build());
    }
    message.protoBuilder().addAllNestedType(allNestedTypes.values());
    message.protoBuilder().addAllField(allFields);
    return message.build();
}
Also used : QuaternionProto(com.google.type.QuaternionProto) Decimals(additionalTypes.Decimals) com.google.protobuf(com.google.protobuf) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) 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) EnumConstant(com.squareup.wire.schema.EnumConstant) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) 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) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) ProtobufSchemaMetadata(metadata.ProtobufSchemaMetadata) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ProtoType(com.squareup.wire.schema.ProtoType) Field(com.squareup.wire.schema.Field) EnumType(com.squareup.wire.schema.EnumType) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) 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) MessageType(com.squareup.wire.schema.MessageType) Optional(java.util.Optional) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) IntRange(kotlin.ranges.IntRange) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) TreeMap(java.util.TreeMap) OneOf(com.squareup.wire.schema.OneOf) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) EnumType(com.squareup.wire.schema.EnumType) Location(com.squareup.wire.schema.Location)

Example 4 with ExtensionsElement

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

the class FileDescriptorUtils method toMessage.

private static MessageElement toMessage(FileDescriptorProto file, DescriptorProto descriptor) {
    String name = descriptor.getName();
    ImmutableList.Builder<FieldElement> fields = ImmutableList.builder();
    ImmutableList.Builder<TypeElement> nested = ImmutableList.builder();
    ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
    ImmutableList.Builder<ExtensionsElement> extensions = 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());
    List<FieldElement> proto3OptionalFields = new ArrayList<>();
    for (FieldDescriptorProto fd : descriptor.getFieldList()) {
        if (fd.hasProto3Optional()) {
            proto3OptionalFields.add(toField(file, fd, false));
            continue;
        }
        if (fd.hasOneofIndex()) {
            FieldElement field = toField(file, fd, true);
            oneofs.get(fd.getOneofIndex()).getValue().add(field);
        } else {
            FieldElement field = toField(file, fd, false);
            fields.add(field);
        }
    }
    fields.addAll(proto3OptionalFields);
    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 (String reservedName : descriptor.getReservedNameList()) {
        ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
        reserved.add(reservedElem);
    }
    for (DescriptorProto.ReservedRange reservedRange : descriptor.getReservedRangeList()) {
        List<IntRange> values = new ArrayList<>();
        int start = reservedRange.getStart();
        int end = reservedRange.getEnd() - 1;
        values.add(new IntRange(start, end));
        ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", values);
        reserved.add(reservedElem);
    }
    for (DescriptorProto.ExtensionRange extensionRange : descriptor.getExtensionRangeList()) {
        List<IntRange> values = new ArrayList<>();
        int start = extensionRange.getStart();
        int end = extensionRange.getEnd() - 1;
        values.add(new IntRange(start, end));
        ExtensionsElement extensionsElement = new ExtensionsElement(DEFAULT_LOCATION, "", values);
        extensions.add(extensionsElement);
    }
    ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
    if (descriptor.getOptions().hasMapEntry()) {
        OptionElement option = new OptionElement(MAP_ENTRY_OPTION, booleanKind, descriptor.getOptions().getMapEntry(), false);
        options.add(option);
    }
    if (descriptor.getOptions().hasNoStandardDescriptorAccessor()) {
        OptionElement option = new OptionElement(NO_STANDARD_DESCRIPTOR_OPTION, booleanKind, descriptor.getOptions().getNoStandardDescriptorAccessor(), false);
        options.add(option);
    }
    return new MessageElement(DEFAULT_LOCATION, name, "", nested.build(), options.build(), reserved.build(), fields.build(), oneofs.stream().filter(e -> e.getValue().build().size() != 0).map(e -> toOneof(e.getKey(), e.getValue())).collect(Collectors.toList()), extensions.build(), Collections.emptyList());
}
Also used : QuaternionProto(com.google.type.QuaternionProto) Decimals(additionalTypes.Decimals) com.google.protobuf(com.google.protobuf) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) 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) EnumConstant(com.squareup.wire.schema.EnumConstant) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) 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) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) ProtobufSchemaMetadata(metadata.ProtobufSchemaMetadata) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) ImmutableList(com.google.common.collect.ImmutableList) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) 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) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) IntRange(kotlin.ranges.IntRange) 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 5 with ExtensionsElement

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

LOWER_UNDERSCORE (com.google.common.base.CaseFormat.LOWER_UNDERSCORE)4 UPPER_CAMEL (com.google.common.base.CaseFormat.UPPER_CAMEL)4 ImmutableList (com.google.common.collect.ImmutableList)4 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)4 EnumDescriptorProto (com.google.protobuf.DescriptorProtos.EnumDescriptorProto)4 EnumValueDescriptorProto (com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto)4 FieldDescriptorProto (com.google.protobuf.DescriptorProtos.FieldDescriptorProto)4 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)4 FileOptions (com.google.protobuf.DescriptorProtos.FileOptions)4 MethodDescriptorProto (com.google.protobuf.DescriptorProtos.MethodDescriptorProto)4 MethodOptions (com.google.protobuf.DescriptorProtos.MethodOptions)4 OneofDescriptorProto (com.google.protobuf.DescriptorProtos.OneofDescriptorProto)4 ServiceDescriptorProto (com.google.protobuf.DescriptorProtos.ServiceDescriptorProto)4 DescriptorValidationException (com.google.protobuf.Descriptors.DescriptorValidationException)4 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)4 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)4 CalendarPeriodProto (com.google.type.CalendarPeriodProto)4 ColorProto (com.google.type.ColorProto)4 ProtoType (com.squareup.wire.schema.ProtoType)3 EnumConstantElement (com.squareup.wire.schema.internal.parser.EnumConstantElement)3