use of com.google.protobuf.Descriptors.OneofDescriptor in project beam by apache.
the class ProtoSchemaTranslator method getSchema.
static Schema getSchema(Descriptors.Descriptor descriptor) {
/* OneOfComponentFields refers to the field number in the protobuf where the component subfields
* are. This is needed to prevent double inclusion of the component fields.*/
Set<Integer> oneOfComponentFields = Sets.newHashSet();
/* OneOfFieldLocation stores the field number of the first field in the OneOf. Using this, we can use the location
of the first field in the OneOf as the location of the entire OneOf.*/
Map<Integer, Field> oneOfFieldLocation = Maps.newHashMap();
List<Field> fields = Lists.newArrayListWithCapacity(descriptor.getFields().size());
for (OneofDescriptor oneofDescriptor : descriptor.getOneofs()) {
List<Field> subFields = Lists.newArrayListWithCapacity(oneofDescriptor.getFieldCount());
Map<String, Integer> enumIds = Maps.newHashMap();
for (FieldDescriptor fieldDescriptor : oneofDescriptor.getFields()) {
oneOfComponentFields.add(fieldDescriptor.getNumber());
// Store proto field number in a field option.
FieldType fieldType = beamFieldTypeFromProtoField(fieldDescriptor);
subFields.add(withFieldNumber(Field.nullable(fieldDescriptor.getName(), fieldType), fieldDescriptor.getNumber()));
checkArgument(enumIds.putIfAbsent(fieldDescriptor.getName(), fieldDescriptor.getNumber()) == null);
}
FieldType oneOfType = FieldType.logicalType(OneOfType.create(subFields, enumIds));
oneOfFieldLocation.put(oneofDescriptor.getFields().get(0).getNumber(), Field.of(oneofDescriptor.getName(), oneOfType));
}
for (Descriptors.FieldDescriptor fieldDescriptor : descriptor.getFields()) {
int fieldDescriptorNumber = fieldDescriptor.getNumber();
if (!oneOfComponentFields.contains(fieldDescriptorNumber)) {
// Store proto field number in metadata.
FieldType fieldType = beamFieldTypeFromProtoField(fieldDescriptor);
fields.add(withFieldNumber(Field.of(fieldDescriptor.getName(), fieldType), fieldDescriptorNumber).withOptions(getFieldOptions(fieldDescriptor)));
/* Note that descriptor.getFields() returns an iterator in the order of the fields in the .proto file, not
* in field number order. Therefore we can safely insert the OneOfField at the field of its first component.*/
} else {
Field oneOfField = oneOfFieldLocation.get(fieldDescriptorNumber);
if (oneOfField != null) {
fields.add(oneOfField);
}
}
}
return Schema.builder().addFields(fields).setOptions(getSchemaOptions(descriptor).setOption(SCHEMA_OPTION_META_TYPE_NAME, FieldType.STRING, descriptor.getFullName())).build();
}
use of com.google.protobuf.Descriptors.OneofDescriptor in project j2objc by google.
the class OneofTest method testFieldOrder.
public void testFieldOrder() throws Exception {
Descriptor descriptor = OneofMsg.getDescriptor();
List<FieldDescriptor> fields = descriptor.getFields();
assertEquals(3, fields.get(0).getNumber());
assertEquals(2, fields.get(1).getNumber());
assertEquals(5, fields.get(2).getNumber());
assertEquals(6, fields.get(3).getNumber());
assertEquals(4, fields.get(4).getNumber());
assertEquals(1, fields.get(5).getNumber());
OneofDescriptor oneof = descriptor.getOneofs().get(0);
List<FieldDescriptor> oneofFields = oneof.getFields();
assertEquals(6, oneofFields.get(0).getNumber());
assertEquals(4, oneofFields.get(1).getNumber());
assertEquals(1, oneofFields.get(2).getNumber());
assertEquals(oneof, fields.get(3).getContainingOneof());
assertEquals(oneof, fields.get(4).getContainingOneof());
assertEquals(oneof, fields.get(5).getContainingOneof());
}
use of com.google.protobuf.Descriptors.OneofDescriptor in project j2objc by google.
the class OneofTest method testOneofDescriptor.
public void testOneofDescriptor() throws Exception {
Descriptor descriptor = OneofMsg.getDescriptor();
List<OneofDescriptor> oneofs = descriptor.getOneofs();
assertEquals(1, oneofs.size());
OneofDescriptor oneof = oneofs.get(0);
assertEquals("oneof_group", oneof.getName());
assertEquals(descriptor, oneof.getContainingType());
}
Aggregations