Java Code Examples for com.google.protobuf.DynamicMessage#getField()

The following examples show how to use com.google.protobuf.DynamicMessage#getField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestProtobufTypeUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testNullRepeated() throws DataGeneratorException {
  Record r = RecordCreator.create();
  Map<String, Field> repeated = new HashMap<>();
  repeated.put("samples", Field.create(Field.Type.LIST, null));
  r.set(Field.create(repeated));
  Descriptors.Descriptor descriptor = RepeatedProto.getDescriptor().findMessageTypeByName("Repeated");
  // repeated field samples is null and ignored
  DynamicMessage dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg(
    r,
    descriptor,
    typeToExtensionMap,
    defaultValueMap
  );
  // null repeated fields are treated as empty arrays
  Object samples = dynamicMessage.getField(descriptor.findFieldByName("samples"));
  Assert.assertNotNull(samples);
  Assert.assertTrue(samples instanceof List);
  Assert.assertEquals(0, ((List) samples).size());
}
 
Example 2
Source File: TestProtobufTypeUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyRepeated() throws DataGeneratorException {
  Record r = RecordCreator.create();
  Map<String, Field> repeated = new HashMap<>();
  repeated.put("samples", Field.create(Field.Type.LIST, new ArrayList<>()));
  r.set(Field.create(repeated));
  Descriptors.Descriptor descriptor = RepeatedProto.getDescriptor().findMessageTypeByName("Repeated");
  // repeated field samples is null and ignored
  DynamicMessage dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg(
    r,
    descriptor,
    typeToExtensionMap,
    defaultValueMap
  );
  // null repeated fields are treated as empty arrays
  Object samples = dynamicMessage.getField(descriptor.findFieldByName("samples"));
  Assert.assertNotNull(samples);
  Assert.assertTrue(samples instanceof List);
  Assert.assertEquals(0, ((List)samples).size());
}
 
Example 3
Source File: DescritporTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDescriptor() throws IOException {
    Descriptor descriptor2 = AddressBookProtos.AddressBook.getDescriptor();

    FieldDescriptor stringMapFD = descriptor2.findFieldByName("person");
    byte[] bytes = getProtoBytes2();
    DynamicMessage parseFrom = DynamicMessage.parseFrom(descriptor2, bytes);
    Object field = parseFrom.getField(stringMapFD);
    Assert.assertTrue(field instanceof List);
    
    Codec<AddressBookProtosPOJO> codec = ProtobufProxy.create(AddressBookProtosPOJO.class, true);
    Descriptor descriptor = codec.getDescriptor();
    
    stringMapFD = descriptor.findFieldByName("list");

    bytes = getProtoBytes2();

    parseFrom = DynamicMessage.parseFrom(descriptor, bytes);

    Object field2 = parseFrom.getField(stringMapFD);
    Assert.assertTrue(field2 instanceof List);

}
 
Example 4
Source File: MetaDataEvolutionValidatorTestV3.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that it is legal to change the records descriptor from proto2 to proto3 as long as all of the records
 * contained within that file are still the same syntax.
 */
@Test
public void onlyFileProto2ToProto3() throws InvalidProtocolBufferException {
    assertNotEquals(TestRecords1Proto.getDescriptor().getSyntax(), TestRecords1ImportedProto.getDescriptor().getSyntax());
    MetaDataEvolutionValidator.getDefaultInstance().validateUnion(
            TestRecords1Proto.RecordTypeUnion.getDescriptor(),
            TestRecords1ImportedProto.RecordTypeUnion.getDescriptor()
    );
    MetaDataEvolutionValidator.getDefaultInstance().validateUnion(
            TestRecords1ImportedProto.RecordTypeUnion.getDescriptor(),
            TestRecords1Proto.RecordTypeUnion.getDescriptor()
    );

    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, TestRecords1ImportedProto.getDescriptor());
    MetaDataEvolutionValidator.getDefaultInstance().validate(metaData1, metaData2);

    // Validate that the nested proto2 records in the proto3 file have proper nullability semantics
    TestRecords1Proto.RecordTypeUnion unionRecordProto2 = TestRecords1Proto.RecordTypeUnion.newBuilder()
            .setMySimpleRecord(TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(0).setStrValueIndexed(""))
            .build();
    assertThat(unionRecordProto2.getMySimpleRecord().hasNumValue2(), is(false));
    assertThat(unionRecordProto2.getMySimpleRecord().hasStrValueIndexed(), is(true));

    TestRecords1ImportedProto.RecordTypeUnion unionRecordProto3 = TestRecords1ImportedProto.RecordTypeUnion.parseFrom(unionRecordProto2.toByteString());
    assertThat(unionRecordProto3.getMySimpleRecord().hasNumValue2(), is(false));
    assertThat(unionRecordProto3.getMySimpleRecord().hasStrValueIndexed(), is(true));

    final FieldDescriptor unionField = TestRecords1ImportedProto.RecordTypeUnion.getDescriptor().findFieldByName("_MySimpleRecord");
    final FieldDescriptor numValue2Field = TestRecords1Proto.MySimpleRecord.getDescriptor().findFieldByName("num_value_2");
    final FieldDescriptor strValueIndexedField = TestRecords1Proto.MySimpleRecord.getDescriptor().findFieldByName("str_value_indexed");
    DynamicMessage dynamicUnionRecordProto3 = DynamicMessage.parseFrom(TestRecords1ImportedProto.RecordTypeUnion.getDescriptor(), unionRecordProto2.toByteString());
    Message dynamicSimpleRecord = (Message)dynamicUnionRecordProto3.getField(unionField);
    assertThat(dynamicSimpleRecord.hasField(numValue2Field), is(false));
    assertThat(dynamicSimpleRecord.hasField(strValueIndexedField), is(true));
}
 
Example 5
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Get the field as a suitable Object. */
public Object getValue(DynamicMessage message, FieldDescriptor field) {
    Object raw = message.getField(field);
    if (raw == null) {
        return null;
    }
    else {
        return valueFromRaw(raw);
    }
}
 
Example 6
Source File: TestProtobufTypeUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonEmptyRepeated() throws DataGeneratorException {
  Record r = RecordCreator.create();
  Map<String, Field> repeated = new HashMap<>();
  repeated.put(
      "samples",
      Field.create(
          Field.Type.LIST,
          Arrays.asList(
              Field.create(1),
              Field.create(2),
              Field.create(3),
              Field.create(4),
              Field.create(5)
          )
      )
  );
  r.set(Field.create(repeated));
  Descriptors.Descriptor descriptor = RepeatedProto.getDescriptor().findMessageTypeByName("Repeated");
  // repeated field samples is null and ignored
  DynamicMessage dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg(
    r,
    descriptor,
    typeToExtensionMap,
    defaultValueMap
  );
  // null repeated fields are treated as empty arrays
  Object samples = dynamicMessage.getField(descriptor.findFieldByName("samples"));
  Assert.assertNotNull(samples);
  Assert.assertTrue(samples instanceof List);
  Assert.assertEquals(5, ((List)samples).size());
}
 
Example 7
Source File: SimpleMapTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDescriptor() throws IOException {
    Codec<SimpleMapPOJO> codec = ProtobufProxy.create(SimpleMapPOJO.class, false);

    Descriptor descriptor = codec.getDescriptor();
    
    String escapeBytes = StringUtils.escapeBytes(descriptor.toProto().toByteArray());
    System.out.println(escapeBytes);

    FieldDescriptor stringMapFD = descriptor.findFieldByName("stringMap");

    byte[] bytes = getProtoBytes2();

    DynamicMessage parseFrom = DynamicMessage.parseFrom(descriptor, bytes);

    Object field = parseFrom.getField(stringMapFD);
    Assert.assertTrue(field instanceof List);
    Assert.assertEquals(2, ((List) field).size());
    
    Descriptor descriptor2 = AddressBookProtos.Person.getDescriptor();
    
    stringMapFD = descriptor2.findFieldByName("stringMap");
    bytes = getProtoBytes2();
    parseFrom = DynamicMessage.parseFrom(descriptor2, bytes);
    field = parseFrom.getField(stringMapFD);
    Assert.assertTrue(field instanceof List);
    Assert.assertEquals(2, ((List) field).size());

}
 
Example 8
Source File: TestProtobufTypeUtil.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testOneofSdcToProtobuf() throws DataGeneratorException {

  Record r1 = RecordCreator.create();
  Map<String, Field> oneofInt = new HashMap<>();
  oneofInt.put("oneofInt", Field.create(5));
  r1.set(Field.create(oneofInt));

  Record r2 = RecordCreator.create();
  Map<String, Field> oneofString = new HashMap<>();
  oneofString.put("oneofString", Field.create("Hello"));
  r2.set(Field.create(oneofString));

  Record r3 = RecordCreator.create();
  Map<String, Field> oneof = new HashMap<>();
  oneof.put("oneofInt", Field.create(5));
  oneof.put("oneofString", Field.create("Hello"));
  r3.set(Field.create(oneof));

  Descriptors.Descriptor descriptor = OneofProto.getDescriptor().findMessageTypeByName("Oneof");

  // in r1 oneofInt field is set
  DynamicMessage dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg(
      r1,
      descriptor,
      typeToExtensionMap,
      defaultValueMap
  );
  Object oneof_name = dynamicMessage.getField(descriptor.findFieldByName("oneofString"));
  Assert.assertNotNull(oneof_name);
  oneof_name = dynamicMessage.getField(descriptor.findFieldByName("oneofInt"));
  Assert.assertNotNull(oneof_name);
  Assert.assertTrue(oneof_name instanceof Integer);
  Assert.assertEquals(5, (int) oneof_name);

  // in r2 oneofString field is set
  dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg(
      r2,
      descriptor,
      typeToExtensionMap,
      defaultValueMap
  );
  oneof_name = dynamicMessage.getField(descriptor.findFieldByName("oneofInt"));
  Assert.assertNotNull(oneof_name);
  oneof_name = dynamicMessage.getField(descriptor.findFieldByName("oneofString"));
  Assert.assertNotNull(oneof_name);
  Assert.assertTrue(oneof_name instanceof String);
  Assert.assertEquals("Hello", oneof_name);

  // Oneof.proto defines one fields in the this order:
  // oneof oneof_name {
  //    int32 oneofInt = 10;
  //    string oneofString = 2;
  // }
  // Therefore when both fields are set the String field is expected to take over because
  // ProtobufTypeUtil.sdcFieldToProtobufMsg sets fields in the order of declaration in the proto file
  // Note that field number does not matter, order of declaration matters.
  dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg(
    r3,
    descriptor,
    typeToExtensionMap,
    defaultValueMap
  );
  oneof_name = dynamicMessage.getField(descriptor.findFieldByName("oneofInt"));
  Assert.assertNotNull(oneof_name);
  oneof_name = dynamicMessage.getField(descriptor.findFieldByName("oneofString"));
  Assert.assertNotNull(oneof_name);
  Assert.assertTrue(oneof_name instanceof String);
  Assert.assertEquals("Hello", oneof_name);
}