是否有可能以编程方式检查给定的原型(prototype)字段是否被标记为required
vs optional
?我正在使用 python 并有一个 FieldDescriptor
对象,但找不到确定是否需要该字段的方法。
最佳答案
快速浏览 documentation表示您的 FieldDescriptor
应该有一个 label
属性,表明它是可选的、必需的还是重复的。
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
关于python - 以编程方式检查是否需要谷歌 Protocol Buffer 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086686/