Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically check if google protocol buffer field is required

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.

like image 507
TheoretiCAL Avatar asked Oct 25 '25 15:10

TheoretiCAL


1 Answers

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
like image 85
user2357112 supports Monica Avatar answered Oct 28 '25 04:10

user2357112 supports Monica