Is it possible to programmatically check if a given proto field is marked as required vs optional? I'm using python and have a FieldDescriptor object, but can't find a way to determine whether the field is required.
A quick look at the documentation indicates that your FieldDescriptor should have a label attribute indicating whether it's optional, required, or repeated.
from google.protobuf.descriptor import FieldDescriptor
if fd.label == FieldDescriptor.LABEL_OPTIONAL:
# do thing
elif fd.label == FieldDescriptor.LABEL_REQUIRED:
# do other thing
else:
# do third thing
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