简体   繁体   English

使用protobuf枚举值作为字段编号

[英]Using protobuf enum value as a field number

I'm wondering if it is possible to use Google Protocol Buffers' enum constants as a field number of other messages, like 我想知道是否可以使用Google Protocol Buffers的枚举常量作为其他消息的字段数,例如

enum Code {
  FOO = 100;
  BAR = 101;
}

message Message {
  required string foo = FOO;
}

This code doesn't work because FOO 's type is enum Code and only a number can be used as a field number. 此代码不起作用,因为FOO的类型是enum Code ,只有一个数字可以用作字段编号。

I am trying to build polymorphic message definitions like this animal example , that defines Cat = 1; 我正在尝试构建多态消息定义,如动物示例 ,定义Cat = 1; in enum Type and required Cat animal = 100; enum Typerequired Cat animal = 100; as a unique extension number. 作为唯一的分机号码。

I thought it'd be nice to do 我觉得这样做很好

message Message {
  required string foo = FOO.value;
}

, so that I can ensure the uniqueness of the extension field number without introducing another magic number. ,这样我就可以确保扩展字段编号的唯一性,而不会引入另一个幻数。

So the question: is it possible to refer an enum's integer value in the protocol buffer language? 所以问题是:是否可以在协议缓冲区语言中引用枚举的整数值?

No, there is no way to do this. 不,没有办法做到这一点。 Sorry. 抱歉。

BTW, two enumerants of the same enum type can actually have the same numeric value, so defining these values in an enum does not actually ensure uniqueness. 顺便说一下,两个相同枚举类型的枚举实际上可以具有相同的数值,因此在枚举中定义这些值实际上并不能确保唯一性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM