Search in sources :

Example 1 with OneOfElement

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

the class ProtobufFile method processMessageElement.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void processMessageElement(String scope, MessageElement messageElement) {
    // reservedFields
    Set<Object> reservedFieldSet = new HashSet<>();
    for (ReservedElement reservedElement : messageElement.getReserveds()) {
        for (Object value : reservedElement.getValues()) {
            if (value instanceof Range) {
                reservedFieldSet.addAll(ContiguousSet.create((Range) value, DiscreteDomain.integers()));
            } else {
                reservedFieldSet.add(value);
            }
        }
    }
    if (!reservedFieldSet.isEmpty()) {
        reservedFields.put(scope + messageElement.getName(), reservedFieldSet);
    }
    // fieldMap, mapMap, FieldsIDName
    Map<String, FieldElement> fieldTypeMap = new HashMap<>();
    Map<String, FieldElement> mapMap = new HashMap<>();
    Map<Integer, String> idsToNames = new HashMap<>();
    for (FieldElement fieldElement : messageElement.getFields()) {
        fieldTypeMap.put(fieldElement.getName(), fieldElement);
        if (fieldElement.getType().startsWith("map<")) {
            mapMap.put(fieldElement.getName(), fieldElement);
        }
        idsToNames.put(fieldElement.getTag(), fieldElement.getName());
    }
    for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
        for (FieldElement fieldElement : oneOfElement.getFields()) {
            fieldTypeMap.put(fieldElement.getName(), fieldElement);
            if (fieldElement.getType().startsWith("map<")) {
                mapMap.put(fieldElement.getName(), fieldElement);
            }
            idsToNames.put(fieldElement.getTag(), fieldElement.getName());
        }
    }
    if (!fieldTypeMap.isEmpty()) {
        fieldMap.put(scope + messageElement.getName(), fieldTypeMap);
    }
    if (!mapMap.isEmpty()) {
        this.mapMap.put(scope + messageElement.getName(), mapMap);
    }
    if (!idsToNames.isEmpty()) {
        fieldsById.put(scope + messageElement.getName(), idsToNames);
    }
    // nonReservedFields
    Set<Object> fieldKeySet = new HashSet<>();
    for (FieldElement fieldElement : messageElement.getFields()) {
        fieldKeySet.add(fieldElement.getTag());
        fieldKeySet.add(fieldElement.getName());
    }
    for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
        for (FieldElement fieldElement : oneOfElement.getFields()) {
            fieldKeySet.add(fieldElement.getTag());
            fieldKeySet.add(fieldElement.getName());
        }
    }
    if (!fieldKeySet.isEmpty()) {
        nonReservedFields.put(scope + messageElement.getName(), fieldKeySet);
    }
    for (TypeElement typeElement : messageElement.getNestedTypes()) {
        if (typeElement instanceof MessageElement) {
            processMessageElement(messageElement.getName() + ".", (MessageElement) typeElement);
        } else if (typeElement instanceof EnumElement) {
            processEnumElement(messageElement.getName() + ".", (EnumElement) typeElement);
        }
    }
}
Also used : HashMap(java.util.HashMap) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) Range(com.google.common.collect.Range) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) HashSet(java.util.HashSet)

Example 2 with OneOfElement

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

the class ProtobufFile method processMessageElement.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void processMessageElement(String scope, MessageElement messageElement) {
    // reservedFields
    Set<Object> reservedFieldSet = new HashSet<>();
    for (ReservedElement reservedElement : messageElement.getReserveds()) {
        for (Object value : reservedElement.getValues()) {
            if (value instanceof Range) {
                reservedFieldSet.addAll(ContiguousSet.create((Range) value, DiscreteDomain.integers()));
            } else {
                reservedFieldSet.add(value);
            }
        }
    }
    if (!reservedFieldSet.isEmpty()) {
        reservedFields.put(scope + messageElement.getName(), reservedFieldSet);
    }
    // fieldMap, mapMap, FieldsIDName
    Map<String, FieldElement> fieldTypeMap = new HashMap<>();
    Map<String, FieldElement> mapMap = new HashMap<>();
    Map<Integer, String> idsToNames = new HashMap<>();
    for (FieldElement fieldElement : messageElement.getFields()) {
        fieldTypeMap.put(fieldElement.getName(), fieldElement);
        if (fieldElement.getType().startsWith("map<")) {
            mapMap.put(fieldElement.getName(), fieldElement);
        }
        idsToNames.put(fieldElement.getTag(), fieldElement.getName());
    }
    for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
        for (FieldElement fieldElement : oneOfElement.getFields()) {
            fieldTypeMap.put(fieldElement.getName(), fieldElement);
            if (fieldElement.getType().startsWith("map<")) {
                mapMap.put(fieldElement.getName(), fieldElement);
            }
            idsToNames.put(fieldElement.getTag(), fieldElement.getName());
        }
    }
    if (!fieldTypeMap.isEmpty()) {
        fieldMap.put(scope + messageElement.getName(), fieldTypeMap);
    }
    if (!mapMap.isEmpty()) {
        this.mapMap.put(scope + messageElement.getName(), mapMap);
    }
    if (!idsToNames.isEmpty()) {
        fieldsById.put(scope + messageElement.getName(), idsToNames);
    }
    // nonReservedFields
    Set<Object> fieldKeySet = new HashSet<>();
    for (FieldElement fieldElement : messageElement.getFields()) {
        fieldKeySet.add(fieldElement.getTag());
        fieldKeySet.add(fieldElement.getName());
    }
    for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
        for (FieldElement fieldElement : oneOfElement.getFields()) {
            fieldKeySet.add(fieldElement.getTag());
            fieldKeySet.add(fieldElement.getName());
        }
    }
    if (!fieldKeySet.isEmpty()) {
        nonReservedFields.put(scope + messageElement.getName(), fieldKeySet);
    }
    for (TypeElement typeElement : messageElement.getNestedTypes()) {
        if (typeElement instanceof MessageElement) {
            processMessageElement(messageElement.getName() + ".", (MessageElement) typeElement);
        } else if (typeElement instanceof EnumElement) {
            processEnumElement(messageElement.getName() + ".", (EnumElement) typeElement);
        }
    }
}
Also used : HashMap(java.util.HashMap) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) Range(com.google.common.collect.Range) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) HashSet(java.util.HashSet)

Example 3 with OneOfElement

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

the class MessageSchemaDiff method compare.

static void compare(final Context ctx, final MessageElement original, final MessageElement update) {
    try (Context.SchemaScope schemaScope = ctx.enterSchema(original)) {
        if (schemaScope != null) {
            Map<Integer, FieldElement> originalByTag = new HashMap<>();
            for (FieldElement field : original.getFields()) {
                originalByTag.put(field.getTag(), field);
            }
            Map<Integer, FieldElement> updateByTag = new HashMap<>();
            for (FieldElement field : update.getFields()) {
                updateByTag.put(field.getTag(), field);
            }
            Map<String, OneOfElement> originalOneOfs = new HashMap<>();
            Map<String, OneOfElement> updateOneOfs = new HashMap<>();
            for (OneOfElement oneOf : original.getOneOfs()) {
                originalOneOfs.put(oneOf.getName(), oneOf);
            }
            for (OneOfElement oneOf : update.getOneOfs()) {
                updateOneOfs.put(oneOf.getName(), oneOf);
            }
            // at most one field in the original message
            for (OneOfElement oneOf : update.getOneOfs()) {
                try (Context.PathScope pathScope = ctx.enterPath(oneOf.getName())) {
                    int numMatchingOriginal = 0;
                    for (FieldElement oneOfField : oneOf.getFields()) {
                        // Remove the field so that a FIELD_REMOVED difference is not generated
                        FieldElement originalField = originalByTag.remove(oneOfField.getTag());
                        if (originalField != null) {
                            numMatchingOriginal++;
                        }
                    }
                    if (numMatchingOriginal > 1) {
                        ctx.addDifference(MULTIPLE_FIELDS_MOVED_TO_ONEOF);
                    }
                }
            }
            Set<String> allOneOfs = new HashSet<>(originalOneOfs.keySet());
            allOneOfs.addAll(updateOneOfs.keySet());
            for (String oneOfName : allOneOfs) {
                try (Context.PathScope pathScope = ctx.enterPath(oneOfName)) {
                    OneOfElement originalOneOf = originalOneOfs.get(oneOfName);
                    OneOfElement updateOneOf = updateOneOfs.get(oneOfName);
                    if (updateOneOf == null) {
                        ctx.addDifference(ONEOF_REMOVED);
                    } else if (originalOneOf == null) {
                        ctx.addDifference(ONEOF_ADDED);
                    } else {
                        OneOfDiff.compare(ctx, originalOneOf, updateOneOf);
                    }
                }
            }
            Set<Integer> allTags = new HashSet<>(originalByTag.keySet());
            allTags.addAll(updateByTag.keySet());
            for (Integer tag : allTags) {
                try (Context.PathScope pathScope = ctx.enterPath(tag.toString())) {
                    FieldElement originalField = originalByTag.get(tag);
                    FieldElement updateField = updateByTag.get(tag);
                    if (updateField == null) {
                        if (originalField.getLabel() == Label.REQUIRED) {
                            ctx.addDifference(REQUIRED_FIELD_REMOVED);
                        } else {
                            ctx.addDifference(FIELD_REMOVED);
                        }
                    } else if (originalField == null) {
                        if (updateField.getLabel() == Label.REQUIRED) {
                            ctx.addDifference(REQUIRED_FIELD_ADDED);
                        } else {
                            ctx.addDifference(FIELD_ADDED);
                        }
                    } else {
                        FieldSchemaDiff.compare(ctx, originalField, updateField);
                    }
                }
            }
        }
        SchemaDiff.compareTypeElements(ctx, original.getNestedTypes(), update.getNestedTypes());
    }
}
Also used : HashMap(java.util.HashMap) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) HashSet(java.util.HashSet)

Example 4 with OneOfElement

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

the class FileDescriptorUtils method toDynamicMessage.

private static MessageDefinition toDynamicMessage(MessageElement messageElem) {
    MessageDefinition.Builder message = MessageDefinition.newBuilder(messageElem.getName());
    for (TypeElement type : messageElem.getNestedTypes()) {
        if (type instanceof MessageElement) {
            message.addMessageDefinition(toDynamicMessage((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 = findOption("json_name", field.getOptions()).map(o -> o.getValue().toString()).orElse(null);
            oneofBuilder.addField(field.getType(), field.getName(), field.getTag(), defaultVal, jsonName);
            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;
        String fieldType = field.getType();
        String defaultVal = field.getDefaultValue();
        String jsonName = field.getJsonName();
        Boolean isPacked = findOption("packed", field.getOptions()).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
        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.getSimpleName(), KEY_FIELD, 1, null);
            mapMessage.addField(null, valueType.getSimpleName(), VALUE_FIELD, 2, null);
            message.addMessageDefinition(mapMessage.build());
        }
        message.addField(label, fieldType, field.getName(), field.getTag(), defaultVal, jsonName, isPacked);
    }
    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);
            } else if (elem instanceof IntRange) {
                IntRange range = (IntRange) elem;
                message.addReservedRange(range.getStart(), range.getEndInclusive());
            } else {
                throw new IllegalStateException("Unsupported reserved type: " + elem.getClass().getName());
            }
        }
    }
    Boolean isMapEntry = findOption("map_entry", messageElem.getOptions()).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
    if (isMapEntry != null) {
        message.setMapEntry(isMapEntry);
    }
    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) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) 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) ProtoType(com.squareup.wire.schema.ProtoType) Field(com.squareup.wire.schema.Field) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with OneOfElement

use of com.squareup.wire.schema.internal.parser.OneOfElement in project thingsboard by thingsboard.

the class ProtoTransportPayloadConfiguration method getMessageDefinitions.

private List<MessageDefinition> getMessageDefinitions(List<MessageElement> messageElementsList) {
    if (!messageElementsList.isEmpty()) {
        List<MessageDefinition> messageDefinitions = new ArrayList<>();
        messageElementsList.forEach(messageElement -> {
            MessageDefinition.Builder messageDefinitionBuilder = MessageDefinition.newBuilder(messageElement.getName());
            List<TypeElement> nestedTypes = messageElement.getNestedTypes();
            if (!nestedTypes.isEmpty()) {
                List<EnumElement> nestedEnumTypes = getEnumElements(nestedTypes);
                if (!nestedEnumTypes.isEmpty()) {
                    nestedEnumTypes.forEach(enumElement -> {
                        EnumDefinition nestedEnumDefinition = getEnumDefinition(enumElement);
                        messageDefinitionBuilder.addEnumDefinition(nestedEnumDefinition);
                    });
                }
                List<MessageElement> nestedMessageTypes = getMessageTypes(nestedTypes);
                List<MessageDefinition> nestedMessageDefinitions = getMessageDefinitions(nestedMessageTypes);
                nestedMessageDefinitions.forEach(messageDefinitionBuilder::addMessageDefinition);
            }
            List<FieldElement> messageElementFields = messageElement.getFields();
            List<OneOfElement> oneOfs = messageElement.getOneOfs();
            if (!oneOfs.isEmpty()) {
                for (OneOfElement oneOfelement : oneOfs) {
                    MessageDefinition.OneofBuilder oneofBuilder = messageDefinitionBuilder.addOneof(oneOfelement.getName());
                    addMessageFieldsToTheOneOfDefinition(oneOfelement.getFields(), oneofBuilder);
                }
            }
            if (!messageElementFields.isEmpty()) {
                addMessageFieldsToTheMessageDefinition(messageElementFields, messageDefinitionBuilder);
            }
            messageDefinitions.add(messageDefinitionBuilder.build());
        });
        return messageDefinitions;
    } else {
        return Collections.emptyList();
    }
}
Also used : TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) ArrayList(java.util.ArrayList) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumDefinition(com.github.os72.protobuf.dynamic.EnumDefinition) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement)

Aggregations

FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)7 OneOfElement (com.squareup.wire.schema.internal.parser.OneOfElement)7 EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)6 MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)6 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)6 ReservedElement (com.squareup.wire.schema.internal.parser.ReservedElement)5 ArrayList (java.util.ArrayList)4 ProtoType (com.squareup.wire.schema.ProtoType)3 EnumConstantElement (com.squareup.wire.schema.internal.parser.EnumConstantElement)3 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)3 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)3 RpcElement (com.squareup.wire.schema.internal.parser.RpcElement)3 ServiceElement (com.squareup.wire.schema.internal.parser.ServiceElement)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 IntRange (kotlin.ranges.IntRange)3