ChengZhang
ChengZhang

Reputation: 41

How to get all fields in a protobuf message?

I am using protobuf these days, and I have met a problem. I want to get all fields in a protobuf message and I know a method, use field_count() to get the field count of a message, and then use the function FindFieldByNumber() to get all of the fields. But, if the field number of a message is discontinuous, for example:

message MyPb
{
uint32    id   =1;
int32 score    =2;
string name    =5;
uint32  high   =6;
}

Then,the field count of MyPb is 4, and I use

for(int i=1; i<=count; ++i)
{
    descriptor->FindFieldByNumber(i);
}

where count=4.

using this method, can I get the field name and high? If not, does somebody know a better method? Thank you very much.

Upvotes: 3

Views: 7445

Answers (1)

Yang Yang
Yang Yang

Reputation: 441

You can use descriptor->field(i) instead of FindFieldByNumber(). Please check the documentation for the difference between these two functions.

Upvotes: 3

Related Questions