Search in sources :

Example 26 with DynamicMessage

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

the class ProtoDynamicMessageSchemaTest method testNullMapRowToProto.

@Test
public void testNullMapRowToProto() {
    ProtoDynamicMessageSchema schemaProvider = schemaFromDescriptor(MapPrimitive.getDescriptor());
    SerializableFunction<Row, DynamicMessage> fromRow = schemaProvider.getFromRowFunction();
    MapPrimitive proto = parseFrom(fromRow.apply(NULL_MAP_PRIMITIVE_ROW).toString(), MapPrimitive.newBuilder()).build();
    assertEquals(NULL_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 27 with DynamicMessage

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

the class DynamicProtoCoder method getParser.

/**
 * Get the memoized {@link Parser}, possibly initializing it lazily.
 */
@Override
protected Parser<DynamicMessage> getParser() {
    if (memoizedParser == null) {
        DynamicMessage protoMessageInstance = DynamicMessage.newBuilder(domain.getDescriptor(messageName)).build();
        memoizedParser = protoMessageInstance.getParserForType();
    }
    return memoizedParser;
}
Also used : DynamicMessage(com.google.protobuf.DynamicMessage)

Example 28 with DynamicMessage

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

the class DynamicProtoCoderTest method testDynamicMessage.

@Test
public void testDynamicMessage() throws Exception {
    DynamicMessage message = DynamicMessage.newBuilder(MessageA.getDescriptor()).setField(MessageA.getDescriptor().findFieldByNumber(MessageA.FIELD1_FIELD_NUMBER), "foo").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 29 with DynamicMessage

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

the class BeamRowToStorageApiProtoTest method testMessageFromTableRow.

@Test
public void testMessageFromTableRow() throws Exception {
    Descriptor descriptor = BeamRowToStorageApiProto.getDescriptorFromSchema(NESTED_SCHEMA);
    DynamicMessage msg = BeamRowToStorageApiProto.messageFromBeamRow(descriptor, NESTED_ROW);
    assertEquals(3, msg.getAllFields().size());
    Map<String, FieldDescriptor> fieldDescriptors = descriptor.getFields().stream().collect(Collectors.toMap(FieldDescriptor::getName, Functions.identity()));
    DynamicMessage nestedMsg = (DynamicMessage) msg.getField(fieldDescriptors.get("nested"));
    assertBaseRecord(nestedMsg);
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) DynamicMessage(com.google.protobuf.DynamicMessage) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.Test)

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