Reputation: 4216
I seem unable to find a way to verify the value of a field inside a protobuf message without explicitly invoking its getter.
I see examples around that make usage of Descriptors.FieldDescriptor
instances to reach inside the message map, but they are either iterator-based or driven by field number.
Once I have the map:
Map<Descriptors.FieldDescriptor, Object> allFields = myMsg.getAllFields();
how can I get the value of field "fieldXyz"
?
I know that I can use myMsg.getFieldXyz()
, but this is not usable in a systematic way.
If there is no way to access field values by their names, I'd like to know what is the rationale behind this choice. I may have still to understand the protobuf "philosophy" :-)
Upvotes: 34
Views: 58030
Reputation: 423
I know this is tagged for java but incase anyone is looking for a way to get the value in c++: (Assuming: field = FieldDescriptor* which contains a int32)
int32_t value = message_1.GetReflection()->GetInt32(message_1, field);
It took me a while to get this and didn't find any stackoverflow references hence adding it. Hope it helps. Thanks!
Upvotes: 16
Reputation: 11619
I am not sure you are looking for Descriptors#findFieldByName(name)
. You can try with followings:
FieldDescriptor fieldDescriptor = message.getDescriptorForType().findFieldByName("fieldXyz");
Object value = message.getField(fieldDescriptor);
Upvotes: 41