mirror of
https://github.com/protobuf-c/protobuf-c.git
synced 2024-12-27 13:31:02 +08:00
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
This commit is contained in:
parent
dfb6e300fa
commit
f486c8ed66
17
Makefile.am
17
Makefile.am
@ -226,6 +226,23 @@ t_version_version_SOURCES = \
|
||||
t_version_version_LDADD = \
|
||||
protobuf-c/libprotobuf-c.la
|
||||
|
||||
# Issue #204
|
||||
check_PROGRAMS += \
|
||||
t/issue204/issue204
|
||||
TESTS += \
|
||||
t/issue204/issue204
|
||||
t_issue204_issue204_SOURCES = \
|
||||
t/issue204/issue204.c \
|
||||
t/issue204/issue204.pb-c.c
|
||||
t_issue204_issue204_LDADD = \
|
||||
protobuf-c/libprotobuf-c.la
|
||||
t/issue204/issue204.pb-c.c t/issue204/issue204.pb-c.h: $(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) $(top_srcdir)/t/issue204/issue204.proto
|
||||
$(AM_V_GEN)@PROTOC@ --plugin=protoc-gen-c=$(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/issue204/issue204.proto
|
||||
BUILT_SOURCES += \
|
||||
t/issue204/issue204.pb-c.c t/issue204/issue204.pb-c.h
|
||||
EXTRA_DIST += \
|
||||
t/issue204/issue204.proto
|
||||
|
||||
# Issue #220
|
||||
check_PROGRAMS += \
|
||||
t/issue220/issue220
|
||||
|
@ -48,6 +48,10 @@ message ProtobufCFileOptions {
|
||||
|
||||
// Use const char * instead of char * for string fields
|
||||
optional bool const_strings = 4 [default = false];
|
||||
|
||||
// For oneof fields, set ProtobufCFieldDescriptor name field to the
|
||||
// name of the containing oneof, instead of the field name
|
||||
optional bool use_oneof_field_name = 5 [default = false];
|
||||
}
|
||||
|
||||
extend google.protobuf.FileOptions {
|
||||
|
@ -107,13 +107,17 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
|
||||
const std::string &descriptor_addr) const
|
||||
{
|
||||
std::map<std::string, std::string> variables;
|
||||
const OneofDescriptor *oneof = descriptor_->containing_oneof();
|
||||
const ProtobufCFileOptions opt = descriptor_->file()->options().GetExtension(pb_c_file);
|
||||
variables["TYPE"] = type_macro;
|
||||
variables["classname"] = FullNameToC(FieldScope(descriptor_)->full_name());
|
||||
variables["name"] = FieldName(descriptor_);
|
||||
variables["proto_name"] = descriptor_->name();
|
||||
if (opt.use_oneof_field_name())
|
||||
variables["proto_name"] = oneof->name();
|
||||
else
|
||||
variables["proto_name"] = descriptor_->name();
|
||||
variables["descriptor_addr"] = descriptor_addr;
|
||||
variables["value"] = SimpleItoa(descriptor_->number());
|
||||
const OneofDescriptor *oneof = descriptor_->containing_oneof();
|
||||
if (oneof != NULL)
|
||||
variables["oneofname"] = FullNameToLower(oneof->name());
|
||||
|
||||
|
1
t/issue204/.gitignore
vendored
Normal file
1
t/issue204/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
issue204
|
48
t/issue204/issue204.c
Normal file
48
t/issue204/issue204.c
Normal file
@ -0,0 +1,48 @@
|
||||
#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;
|
||||
}
|
15
t/issue204/issue204.proto
Normal file
15
t/issue204/issue204.proto
Normal file
@ -0,0 +1,15 @@
|
||||
import "protobuf-c/protobuf-c.proto";
|
||||
|
||||
option (pb_c_file).use_oneof_field_name = true;
|
||||
|
||||
message two_oneofs {
|
||||
oneof first_oneof {
|
||||
bool a = 10;
|
||||
bool b = 11;
|
||||
}
|
||||
|
||||
oneof second_oneof {
|
||||
bool c = 20;
|
||||
bool d = 21;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user