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));
}
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)));
}
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));
}
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);
}
}
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();
}
}
Aggregations