Search in sources :

Example 6 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class DynamicProtoCoderTest method testDynamicNestedRepeatedMessage.

@Test
public void testDynamicNestedRepeatedMessage() throws Exception {
    DynamicMessage message = DynamicMessage.newBuilder(MessageA.getDescriptor()).setField(MessageA.getDescriptor().findFieldByNumber(MessageA.FIELD1_FIELD_NUMBER), "foo").addRepeatedField(MessageA.getDescriptor().findFieldByNumber(MessageA.FIELD2_FIELD_NUMBER), DynamicMessage.newBuilder(MessageB.getDescriptor()).setField(MessageB.getDescriptor().findFieldByNumber(MessageB.FIELD1_FIELD_NUMBER), true).build()).addRepeatedField(MessageA.getDescriptor().findFieldByNumber(MessageA.FIELD2_FIELD_NUMBER), DynamicMessage.newBuilder(MessageB.getDescriptor()).setField(MessageB.getDescriptor().findFieldByNumber(MessageB.FIELD1_FIELD_NUMBER), false).build()).build();
    Coder<DynamicMessage> coder = DynamicProtoCoder.of(message.getDescriptorForType());
    // Special code to check the DynamicMessage equality (@see IsDynamicMessageEqual)
    for (Coder.Context context : ALL_CONTEXTS) {
        CoderProperties.coderDecodeEncodeInContext(coder, context, message, IsDynamicMessageEqual.equalTo(message));
    }
}
Also used : Coder(org.apache.beam.sdk.coders.Coder) DynamicMessage(com.google.protobuf.DynamicMessage) Test(org.junit.Test)

Example 7 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class ProtoDynamicMessageSchemaTest method testNestedRowToProto.

@Test
public void testNestedRowToProto() throws InvalidProtocolBufferException {
    ProtoDynamicMessageSchema schemaProvider = schemaFromDescriptor(Nested.getDescriptor());
    SerializableFunction<Row, DynamicMessage> fromRow = schemaProvider.getFromRowFunction();
    Nested proto = parseFrom(fromRow.apply(NESTED_ROW).toString(), Nested.newBuilder()).build();
    assertEquals(NESTED_PROTO, proto);
}
Also used : Nested(org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.Nested) Row(org.apache.beam.sdk.values.Row) DynamicMessage(com.google.protobuf.DynamicMessage) Test(org.junit.Test)

Example 8 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class ProtoDynamicMessageSchemaTest method testMapRowToProto.

@Test
public void testMapRowToProto() {
    ProtoDynamicMessageSchema schemaProvider = schemaFromDescriptor(MapPrimitive.getDescriptor());
    SerializableFunction<Row, DynamicMessage> fromRow = schemaProvider.getFromRowFunction();
    MapPrimitive proto = parseFrom(fromRow.apply(MAP_PRIMITIVE_ROW).toString(), MapPrimitive.newBuilder()).build();
    assertEquals(MAP_PRIMITIVE_PROTO, proto);
}
Also used : MapPrimitive(org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.MapPrimitive) Row(org.apache.beam.sdk.values.Row) DynamicMessage(com.google.protobuf.DynamicMessage) Test(org.junit.Test)

Example 9 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class TableRowToStorageApiProto method messageFromMap.

public static DynamicMessage messageFromMap(Descriptor descriptor, AbstractMap<String, Object> map) {
    DynamicMessage.Builder builder = DynamicMessage.newBuilder(descriptor);
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        @Nullable FieldDescriptor fieldDescriptor = descriptor.findFieldByName(entry.getKey().toLowerCase());
        if (fieldDescriptor == null) {
            throw new RuntimeException("TableRow contained unexpected field with name " + entry.getKey());
        }
        @Nullable Object value = messageValueFromFieldValue(fieldDescriptor, entry.getValue());
        if (value != null) {
            builder.setField(fieldDescriptor, value);
        }
    }
    return builder.build();
}
Also used : DynamicMessage(com.google.protobuf.DynamicMessage) ByteString(com.google.protobuf.ByteString) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) Nullable(javax.annotation.Nullable) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 10 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class TableRowToStorageApiProto method messageFromTableRow.

/**
 * Given a BigQuery TableRow, returns a protocol-buffer message that can be used to write data
 * using the BigQuery Storage API.
 */
public static DynamicMessage messageFromTableRow(Descriptor descriptor, TableRow tableRow) {
    @Nullable List<TableCell> cells = tableRow.getF();
    if (cells != null) {
        DynamicMessage.Builder builder = DynamicMessage.newBuilder(descriptor);
        if (cells.size() > descriptor.getFields().size()) {
            throw new RuntimeException("TableRow contained too many fields");
        }
        for (int i = 0; i < cells.size(); ++i) {
            TableCell cell = cells.get(i);
            FieldDescriptor fieldDescriptor = descriptor.getFields().get(i);
            @Nullable Object value = messageValueFromFieldValue(fieldDescriptor, cell.getV());
            if (value != null) {
                builder.setField(fieldDescriptor, value);
            }
        }
        return builder.build();
    } else {
        return messageFromMap(descriptor, tableRow);
    }
}
Also used : TableCell(com.google.api.services.bigquery.model.TableCell) DynamicMessage(com.google.protobuf.DynamicMessage) Nullable(javax.annotation.Nullable) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

DynamicMessage (com.google.protobuf.DynamicMessage)29 Test (org.junit.Test)12 ByteString (com.google.protobuf.ByteString)11 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)11 Descriptor (com.google.protobuf.Descriptors.Descriptor)9 IOException (java.io.IOException)6 List (java.util.List)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)5 Message (com.google.protobuf.Message)5 Map (java.util.Map)4 TableRow (com.google.api.services.bigquery.model.TableRow)3 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)3 Builder (com.google.protobuf.DynamicMessage.Builder)3 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 DataHandler (javax.activation.DataHandler)3 Nullable (javax.annotation.Nullable)3 Descriptors (com.google.protobuf.Descriptors)2 JsonFormat (com.google.protobuf.util.JsonFormat)2