Search in sources :

Example 1 with MessageDefinition

use of com.github.os72.protobuf.dynamic.MessageDefinition in project firehose by odpf.

the class KafkaMetadataProtoMessageTest method shouldCreateKafkaMetadataMessageDefinition.

@Test
public void shouldCreateKafkaMetadataMessageDefinition() throws Descriptors.DescriptorValidationException {
    MessageDefinition messageDefinition = KafkaMetadataProtoMessage.createMessageDefinition();
    schemaBuilder.addMessageDefinition(messageDefinition);
    schemaBuilder.addMessageDefinition(TimestampMetadataProtoMessage.createMessageDefinition());
    DynamicSchema dynamicSchema = schemaBuilder.build();
    Descriptors.Descriptor descriptor = dynamicSchema.getMessageDescriptor(KafkaMetadataProtoMessage.getTypeName());
    assertNotNull(descriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_OFFSET_FIELD_NAME));
    assertNotNull(descriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_PARTITION_FIELD_NAME));
    assertNotNull(descriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_TOPIC_FIELD_NAME));
    assertNotNull(descriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_TIMESTAMP_FIELD_NAME));
    assertNotNull(descriptor.findFieldByName(KafkaMetadataProtoMessage.LOAD_TIME_FIELD_NAME));
}
Also used : MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.Test)

Example 2 with MessageDefinition

use of com.github.os72.protobuf.dynamic.MessageDefinition in project firehose by odpf.

the class NestedKafkaMetadataProtoMessageTest method shouldCreateDynamicMessage.

@Test
public void shouldCreateDynamicMessage() throws Descriptors.DescriptorValidationException {
    MessageDefinition kafkaMetadataMessageDefinition = KafkaMetadataProtoMessage.createMessageDefinition();
    schemaBuilder.addMessageDefinition(kafkaMetadataMessageDefinition);
    schemaBuilder.addMessageDefinition(TimestampMetadataProtoMessage.createMessageDefinition());
    MessageDefinition nestedKafkaMetadataMessageDefinition = NestedKafkaMetadataProtoMessage.createMessageDefinition(metadataColumnName, KafkaMetadataProtoMessage.getTypeName(), kafkaMetadataMessageDefinition);
    schemaBuilder.addMessageDefinition(nestedKafkaMetadataMessageDefinition);
    DynamicSchema dynamicSchema = schemaBuilder.build();
    Descriptors.Descriptor metadataDescriptor = dynamicSchema.getMessageDescriptor(KafkaMetadataProtoMessage.getTypeName());
    DynamicMessage metadataMessage = KafkaMetadataProtoMessage.newBuilder(metadataDescriptor).setOffset(1).setPartition(1).setTopic("default").setMessageTimestamp(Instant.EPOCH).setLoadTime(Instant.EPOCH).build();
    Descriptors.Descriptor nestedMetadataDescriptor = dynamicSchema.getMessageDescriptor(NestedKafkaMetadataProtoMessage.getTypeName());
    DynamicMessage nestedMetadataMessage = NestedKafkaMetadataProtoMessage.newMessageBuilder(nestedMetadataDescriptor).setMetadata(metadataMessage).setMetadataColumnName(metadataColumnName).build();
    assertEquals(metadataMessage, nestedMetadataMessage.getField(nestedMetadataDescriptor.findFieldByName(metadataColumnName)));
}
Also used : MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) Descriptors(com.google.protobuf.Descriptors) DynamicMessage(com.google.protobuf.DynamicMessage) Test(org.junit.Test)

Example 3 with MessageDefinition

use of com.github.os72.protobuf.dynamic.MessageDefinition in project firehose by odpf.

the class NestedKafkaMetadataProtoMessageTest method shouldGenerateMessageDefinition.

@Test
public void shouldGenerateMessageDefinition() throws Descriptors.DescriptorValidationException {
    MessageDefinition kafkaMetadataMessageDefinition = KafkaMetadataProtoMessage.createMessageDefinition();
    schemaBuilder.addMessageDefinition(kafkaMetadataMessageDefinition);
    schemaBuilder.addMessageDefinition(TimestampMetadataProtoMessage.createMessageDefinition());
    MessageDefinition nestedKafkaMetadataMessageDefinition = NestedKafkaMetadataProtoMessage.createMessageDefinition(metadataColumnName, KafkaMetadataProtoMessage.getTypeName(), kafkaMetadataMessageDefinition);
    schemaBuilder.addMessageDefinition(nestedKafkaMetadataMessageDefinition);
    DynamicSchema dynamicSchema = schemaBuilder.build();
    Descriptors.Descriptor nestedDescriptor = dynamicSchema.getMessageDescriptor(NestedKafkaMetadataProtoMessage.getTypeName());
    assertNotNull(nestedDescriptor.findFieldByName(metadataColumnName));
    Descriptors.Descriptor metadataDescriptor = nestedDescriptor.findFieldByName(metadataColumnName).getMessageType();
    assertNotNull(metadataDescriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_OFFSET_FIELD_NAME));
    assertNotNull(metadataDescriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_PARTITION_FIELD_NAME));
    assertNotNull(metadataDescriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_TOPIC_FIELD_NAME));
    assertNotNull(metadataDescriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_TIMESTAMP_FIELD_NAME));
    assertNotNull(metadataDescriptor.findFieldByName(KafkaMetadataProtoMessage.LOAD_TIME_FIELD_NAME));
}
Also used : MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.Test)

Example 4 with MessageDefinition

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

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

MessageDefinition (com.github.os72.protobuf.dynamic.MessageDefinition)8 DynamicSchema (com.github.os72.protobuf.dynamic.DynamicSchema)7 Descriptors (com.google.protobuf.Descriptors)7 Test (org.junit.Test)4 EnumDefinition (com.github.os72.protobuf.dynamic.EnumDefinition)2 DynamicMessage (com.google.protobuf.DynamicMessage)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 Timestamp (com.google.protobuf.Timestamp)1 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)1 OneOfElement (com.squareup.wire.schema.internal.parser.OneOfElement)1 ArrayList (java.util.ArrayList)1