Protobuf 枚举的奇怪行为

Protobuf strange behaviour with enums

我有以下 protobuf 定义:

syntax = "proto3";                                                                                                                                                                                                                                                                                                                                                                                                              
message ACL {                                                                                                                                                                                                   
  enum Foo {
    BAR = 0;
    BAZ = 1;
  }
  enum Corpus {
    UNIVERSAL = 0;
    WEB = 1;
  }
  Foo foo = 1;
  Corpus corpus = 2;
}

当我在 Python 中使用时,它将 store/show 语料库字段而不是 foo 字段:

>>> import acl_pb2
>>> p = acl_pb2.ACL(foo="BAR", corpus="WEB")
>>> p
corpus: WEB

在 proto3 中,零是默认值,默认值是零。不传输默认值(零),因此:没有为 foo = "Bar" 发送的实际数据,因为它是一个零。但是,您的模型应该能够发现 foo 存在并且 具有 Bar/0 的隐式值。如果不是,那听起来像是 python code-gen 的好奇心。