Search in sources :

Example 1 with GroupElement

use of com.squareup.wire.schema.internal.parser.GroupElement in project schema2proto by entur.

the class MessageType method fromElement.

static MessageType fromElement(String packageName, ProtoType protoType, MessageElement messageElement) {
    if (!messageElement.getGroups().isEmpty()) {
        GroupElement group = messageElement.getGroups().get(0);
        throw new IllegalStateException(group.getLocation() + ": 'group' is not supported");
    }
    List<Field> declaredFields = Field.fromElements(packageName, messageElement.getFields(), false);
    // Extension fields be populated during linking.
    List<Field> extensionFields = new ArrayList<>();
    ImmutableList<OneOf> oneOfs = OneOf.fromElements(packageName, messageElement.getOneOfs(), false);
    ImmutableList.Builder<Type> nestedTypes = ImmutableList.builder();
    for (TypeElement nestedType : messageElement.getNestedTypes()) {
        nestedTypes.add(Type.get(packageName, protoType.nestedType(nestedType.getName()), nestedType));
    }
    List<Extensions> extensionsList = Extensions.fromElements(messageElement.getExtensions());
    List<Reserved> reserveds = Reserved.fromElements(messageElement.getReserveds());
    Options options = new Options(Options.MESSAGE_OPTIONS, messageElement.getOptions());
    return new MessageType(protoType, messageElement.getLocation(), messageElement.getDocumentation(), messageElement.getName(), declaredFields, extensionFields, oneOfs, nestedTypes.build(), extensionsList, reserveds, options);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) GroupElement(com.squareup.wire.schema.internal.parser.GroupElement) ArrayList(java.util.ArrayList)

Example 2 with GroupElement

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

the class ProtobufSchemaUtils method toString.

private static String toString(Context ctx, OneOfElement type, boolean normalize) {
    StringBuilder sb = new StringBuilder();
    sb.append("oneof ");
    sb.append(type.getName());
    sb.append(" {");
    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');
        // Fields have already been sorted while sorting oneOfs in the calling method
        List<FieldElement> fields = type.getFields();
        for (FieldElement field : fields) {
            appendIndented(sb, toString(ctx, field, normalize));
        }
    }
    if (!type.getGroups().isEmpty()) {
        sb.append('\n');
        for (GroupElement group : type.getGroups()) {
            appendIndented(sb, group.toSchema());
        }
    }
    sb.append("}\n");
    return sb.toString();
}
Also used : OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) GroupElement(com.squareup.wire.schema.internal.parser.GroupElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement)

Example 3 with GroupElement

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

GroupElement (com.squareup.wire.schema.internal.parser.GroupElement)3 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)2 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)2 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Ascii (com.google.common.base.Ascii)1 ImmutableList (com.google.common.collect.ImmutableList)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 JsonFormat (com.google.protobuf.util.JsonFormat)1 Label (com.squareup.wire.schema.Field.Label)1 ProtoType (com.squareup.wire.schema.ProtoType)1 MAX_TAG_VALUE (com.squareup.wire.schema.internal.UtilKt.MAX_TAG_VALUE)1 EnumConstantElement (com.squareup.wire.schema.internal.parser.EnumConstantElement)1 EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)1 ExtendElement (com.squareup.wire.schema.internal.parser.ExtendElement)1 ExtensionsElement (com.squareup.wire.schema.internal.parser.ExtensionsElement)1