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