2
votes

I'm trying to parse a very big message (approx 25 fields) and serialize them. The fields in the message appear in the same order all the time and in the proto file I numbered them accordingly. Is there a method to set the fields with the tag value (the number in the proto file)?

Thanks, Chem.

1
"is there a method"... well, what implementation are you using, for starters - from memory there are something like 40 different implementations/tools - Marc Gravell♦
right now I'm comparing the corresponding names and setting the values, that is like a lot of IF conditions in the code. I want to get rid of them. - ShadowFax
btw I'm doing it in java - ShadowFax

1 Answers

7
votes
google::protobuf::Message myMessage;
const google::protobuf::Descriptor * myDescriptor = myMessage.GetDescriptor();
const google::protobuf::FieldDescriptor * myField = myDescriptor->FindFieldByNumber(9001);
const google::protobuf::Reflection * myReflection = myMessage.GetReflection();
myReflection->SetInt32( &myMessage, myField, 7);

Obviously you'll need to change the field number, type of the field, and value to which you want to set.