Search in sources :

Example 6 with MessageDefinition

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

the class KafkaMetadataProtoMessageUtils method createSchema.

private static DynamicSchema createSchema(String kafkaMetadataColumnName) {
    DynamicSchema.Builder schemaBuilder = DynamicSchema.newBuilder().setName(FILE_NAME).setPackage(PACKAGE);
    MessageDefinition timestampMessageDefinition = TimestampMetadataProtoMessage.createMessageDefinition();
    schemaBuilder.addMessageDefinition(timestampMessageDefinition);
    MessageDefinition kafkaMetadataMessageDefinition = KafkaMetadataProtoMessage.createMessageDefinition();
    schemaBuilder.addMessageDefinition(kafkaMetadataMessageDefinition);
    if (!kafkaMetadataColumnName.isEmpty()) {
        MessageDefinition kafkaNestedMetadataProtoMessageDefinition = NestedKafkaMetadataProtoMessage.createMessageDefinition(kafkaMetadataColumnName, KafkaMetadataProtoMessage.getTypeName(), kafkaMetadataMessageDefinition);
        schemaBuilder.addMessageDefinition(kafkaNestedMetadataProtoMessageDefinition);
    }
    DynamicSchema schema;
    try {
        schema = schemaBuilder.build();
    } catch (Descriptors.DescriptorValidationException e) {
        throw new IllegalArgumentException("Invalid proto schema", e);
    }
    return schema;
}
Also used : MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) Descriptors(com.google.protobuf.Descriptors)

Example 7 with MessageDefinition

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

the class TestProtoMessage method createSchema.

public static DynamicSchema createSchema() {
    DynamicSchema.Builder schemaBuilder = DynamicSchema.newBuilder().setName(FILE_NAME).setPackage(PACKAGE);
    MessageDefinition messageDefinition = new TestProtoMessage().createMessageDefinition();
    schemaBuilder.addMessageDefinition(messageDefinition);
    schemaBuilder.addMessageDefinition(TimestampMetadataProtoMessage.createMessageDefinition());
    DynamicSchema schema;
    try {
        schema = schemaBuilder.build();
    } catch (Descriptors.DescriptorValidationException e) {
        throw new RuntimeException(e);
    }
    return schema;
}
Also used : MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) Descriptors(com.google.protobuf.Descriptors)

Example 8 with MessageDefinition

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

the class KafkaMetadataProtoMessageTest method shouldCreateKafkaMetadataDynamicMessage.

@Test
public void shouldCreateKafkaMetadataDynamicMessage() 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());
    DynamicMessage dynamicMessage = KafkaMetadataProtoMessage.newBuilder(descriptor).setOffset(1).setPartition(1).setTopic("default").setMessageTimestamp(Instant.EPOCH).setLoadTime(Instant.EPOCH).build();
    assertEquals(1L, dynamicMessage.getField(descriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_OFFSET_FIELD_NAME)));
    assertEquals(1, dynamicMessage.getField(descriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_PARTITION_FIELD_NAME)));
    assertEquals("default", dynamicMessage.getField(descriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_TOPIC_FIELD_NAME)));
    Timestamp timestamp = Timestamp.newBuilder().setSeconds(0).setNanos(0).build();
    assertEquals(timestamp, dynamicMessage.getField(descriptor.findFieldByName(KafkaMetadataProtoMessage.MESSAGE_TIMESTAMP_FIELD_NAME)));
    assertEquals(timestamp, dynamicMessage.getField(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) DynamicMessage(com.google.protobuf.DynamicMessage) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

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