protobuf-c/t/issue204/issue204.c
Ilya Lipnitskiy f486c8ed66
protoc-c: add use_oneof_field_name option
Allows for generic oneof offset lookups based on the name of the oneof,
instead of having to know the field name. All oneof fields should have
the same offset, since they are members of a union.

Fixes #204
2021-03-21 21:11:50 -07:00

49 lines
1.1 KiB
C

#include <stdlib.h>
#include "t/issue251/issue251.pb-c.h"
int main(void)
{
TwoOneofs msg = TWO_ONEOFS__INIT;
const ProtobufCFieldDescriptor *field;
unsigned off1, off2, off_name;
field = protobuf_c_message_descriptor_get_field_by_name(
msg.base.descriptor,
"first_oneof");
assert (field);
off_name = field->offset;
field = protobuf_c_message_descriptor_get_field(
msg.base.descriptor,
10);
assert (field);
off1 = field->offset;
field = protobuf_c_message_descriptor_get_field(
msg.base.descriptor,
11);
assert (field);
off2 = field->offset;
assert (off_name == off1);
assert (off1 == off2);
field = protobuf_c_message_descriptor_get_field_by_name(
msg.base.descriptor,
"second_oneof");
assert (field);
off_name = field->offset;
field = protobuf_c_message_descriptor_get_field(
msg.base.descriptor,
20);
assert (field);
off1 = field->offset;
field = protobuf_c_message_descriptor_get_field(
msg.base.descriptor,
21);
assert (field);
off2 = field->offset;
assert (off_name == off1);
assert (off1 == off2);
return EXIT_SUCCESS;
}