get all enum fields and their possible values

23 views
Skip to first unread message

Tuppy Hamper

unread,
Oct 2, 2009, 11:23:31 AM10/2/09
to Protocol Buffers
is it possible to dynmically get allthe enum fields associated with a
protocol buffer message and all the valid values defined in that enum
fields?

Tuppy Hamper

unread,
Oct 2, 2009, 11:33:09 AM10/2/09
to Protocol Buffers
I am looking for a way to get all the enums defined, not just the ones
that are set.

Kenton Varda

unread,
Oct 2, 2009, 1:59:33 PM10/2/09
to Tuppy Hamper, Protocol Buffers
using google::protobuf::Descriptor;
using google::protobuf::FieldDescriptor;
using google::protobuf::EnumDescriptor;
using google::protobuf::EnumValueDescriptor;

const Descriptor* descriptor = message.GetDescriptor();
for (int i = 0; i < descriptor->field_count(); i++) {
  const FieldDescriptor* field = descriptor->field(i);
  if (field->type() == FieldDescriptor::TYPE_ENUM) {
    // this is an enum field.
    const EnumDescriptor* type = field->enum_type();
    for (int j = 0; j < type->value_count(); j++) {
      const EnumValueDescriptor* value = type->value(i);
      // value->name() and value->number() describe this value.

Tuppy Hamper

unread,
Oct 2, 2009, 4:07:22 PM10/2/09
to Protocol Buffers
and how would this change if the message contained fields that were
repeatable or single messages which contained their own ENUMS. I know
I would have to have a branch to the IF statement that if type ==
MESSAGE, then recurse, but would I have to handle single and repeatble
message fields differently?

Thanks
> > fields?- Hide quoted text -
>
> - Show quoted text -

Kenton Varda

unread,
Oct 2, 2009, 7:10:58 PM10/2/09
to Tuppy Hamper, Protocol Buffers
On Fri, Oct 2, 2009 at 1:07 PM, Tuppy Hamper <ham...@gmail.com> wrote:

and how would this change if the message contained fields that were
repeatable or single messages which contained their own ENUMS. I know
I would have to have a branch to the IF statement that if type ==
MESSAGE, then recurse, but would I have to handle single and repeatble
message fields differently?

field->is_repeated() returns true or false.  There's nothing else different about the descriptors (though the resulting messages are obviously pretty different).
Reply all
Reply to author
Forward
0 new messages