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" :-)
descriptor. Descriptors essentially contain exactly the information found in a . proto file, in types that make this information accessible in Python.
In the binary format, the field number is combined with a type identifier. Field numbers from 1 to 15 can be encoded with their type as a single byte. Numbers from 16 to 2,047 take 2 bytes. You can go higher if you need more than 2,047 fields on a message for any reason.
The Best Answer is In proto3, all fields are "optional" (in that it is not an error if the sender fails to set them). But, fields are no longer "nullable", in that there's no way to tell the difference between a field being explicitly set to its default value vs. not having been set at all.
Adding Comments To add comments to your .proto files, use C/C++-style // and /* ... */ syntax.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With