Search in sources :

Example 1 with EnumDefinition

use of com.github.os72.protobuf.dynamic.EnumDefinition in project thingsboard by thingsboard.

the class ProtoTransportPayloadConfiguration method getDynamicSchema.

public DynamicSchema getDynamicSchema(ProtoFileElement protoFileElement, String schemaName) {
    DynamicSchema.Builder schemaBuilder = DynamicSchema.newBuilder();
    schemaBuilder.setName(schemaName);
    schemaBuilder.setSyntax(PROTO_3_SYNTAX);
    schemaBuilder.setPackage(!isEmptyStr(protoFileElement.getPackageName()) ? protoFileElement.getPackageName() : schemaName.toLowerCase());
    List<TypeElement> types = protoFileElement.getTypes();
    List<MessageElement> messageTypes = getMessageTypes(types);
    if (!messageTypes.isEmpty()) {
        List<EnumElement> enumTypes = getEnumElements(types);
        if (!enumTypes.isEmpty()) {
            enumTypes.forEach(enumElement -> {
                EnumDefinition enumDefinition = getEnumDefinition(enumElement);
                schemaBuilder.addEnumDefinition(enumDefinition);
            });
        }
        List<MessageDefinition> messageDefinitions = getMessageDefinitions(messageTypes);
        messageDefinitions.forEach(schemaBuilder::addMessageDefinition);
        try {
            return schemaBuilder.build();
        } catch (Descriptors.DescriptorValidationException e) {
            throw new RuntimeException("Failed to create dynamic schema due to: " + e.getMessage());
        }
    } else {
        throw new RuntimeException("Failed to get Dynamic Schema! Message types is empty for schema:" + schemaName);
    }
}
Also used : TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) EnumDefinition(com.github.os72.protobuf.dynamic.EnumDefinition) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) Descriptors(com.google.protobuf.Descriptors)

Example 2 with EnumDefinition

use of com.github.os72.protobuf.dynamic.EnumDefinition 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

EnumDefinition (com.github.os72.protobuf.dynamic.EnumDefinition)2 MessageDefinition (com.github.os72.protobuf.dynamic.MessageDefinition)2 EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)2 MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)2 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)2 DynamicSchema (com.github.os72.protobuf.dynamic.DynamicSchema)1 Descriptors (com.google.protobuf.Descriptors)1 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)1 OneOfElement (com.squareup.wire.schema.internal.parser.OneOfElement)1 ArrayList (java.util.ArrayList)1