简体   繁体   English

协议缓冲区使用枚举

[英]Protocol Buffer use enum

Hello I'm working with protobuf but I have a problem.您好,我正在使用 protobuf,但我遇到了问题。

I have some enum functions but in tow of these I have the same alias, when I try to compile the file for some language "go" the compiler return an error.我有一些枚举函数,但在其中我有相同的别名,当我尝试为某种语言“go”编译文件时,编译器返回错误。

I copied the example in protobuf documentation to define the enum and still not working.我复制了 protobuf 文档中的示例来定义枚举,但仍然无法正常工作。

enum EnumAllowingAlias {
  option allow_alias = true;
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;
}
enum EnumNotAllowingAlias {
  UNKNOWN = 0;
  STARTED = 1;
  // RUNNING = 1;  // Uncommenting this line will cause a compile error inside Google and a warning message outside.
}

This is the google documentation said if you need to use the same alias in some different enums you need to add the option "option allow_alias = true;"这是谷歌文档说如果你需要在一些不同的枚举中使用相同的别名,你需要添加选项“option allow_alias = true;” in the enum but after try to compile the .proto file the compiler response.在枚举中,但在尝试编译 .proto 文件后,编译器响应。

example.proto:13:5: "UNKNOWN" is already defined in "namespace". example.proto:13:5:“UNKNOWN”已在“命名空间”中定义。
example.proto:13:5: Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it. example.proto:13:5:请注意,枚举值使用 C++ 范围规则,这意味着枚举值是其类型的同级,而不是其子级。
Therefore, "UNKNOWN" must be unique within "kluso", not just within "EnumNotAllowingAlias".因此,“UNKNOWN”在“kluso”中必须是唯一的,而不仅仅是在“EnumNotAllowingAlias”中。

example.proto:14:5: "STARTED" is already defined in "namespace". example.proto:14:5:“开始”已经在“命名空间”中定义。
example.proto:14:5: Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it. example.proto:14:5:请注意,枚举值使用 C++ 范围规则,这意味着枚举值是其类型的同级,而不是其子级。
Therefore, "STARTED" must be unique within "kluso", not just within "EnumNotAllowingAlias".因此,“STARTED”在“kluso”中必须是唯一的,而不仅仅是在“EnumNotAllowingAlias”中。

I don't know what's happen.我不知道发生了什么。 Someone know what is the problem?有人知道是什么问题吗?

allow_alias allows you to have two different names for the same value. allow_alias 允许您为相同的值使用两个不同的名称

However, it still doesn't allow you to use the same name twice!但是,它仍然不允许使用相同的名称两次!

In the example you took from the docs, they are just trying to demonstrate how you can use STARTED = 1 and RUNNING = 1 in the same enum type by setting allow_alias to be true.在您从文档中获取的示例中,他们只是试图通过将 allow_alias 设置为 true 来演示如何在相同的枚举类型中使用 STARTED = 1 和 RUNNING = 1 。 The two enums example they gave are not meant to be used in the same package.他们给出的两个枚举示例并不打算在同一个包中使用。

But if you already have RUNNING in EnumAllowingAlias, you cannot use RUNNING again in EnumNotAllowingAlias if they are in the same package.但是,如果您已经在 EnumAllowingAlias 中使用了 RUNNING,则如果它们在同一个包中,则不能在 EnumNotAllowingAlias 中再次使用 RUNNING。

The answer you're looking for is in the error message :) it's just a bit hard to parse out what it's actually telling you.您正在寻找的答案在错误消息中 :) 解析它实际告诉您的内容有点困难。

Basically, what基本上,什么

enum values are siblings of their type, not children of it枚举值是它们类型的兄弟,而不是它的孩子

means is that, rather than the enum values being scoped as MyEnum.FOO and MyEnum2.FOO , they are scoped at the same level as MyEnum .意思是,枚举值的范围MyEnum.FOOMyEnum2.FOOMyEnum2.FOO它们的范围与MyEnum2.FOO相同 So two enum values in the same .proto file cannot have the same name;所以同一个 .proto 文件中的两个枚举值不能具有相同的名称; they're both trying to exist as FOO within that file, rather than being MyEnum.FOO and MyEnum2.FOO .他们都试图在该文件中作为FOO存在,而不是MyEnum.FOOMyEnum2.FOO

I don' t understand your point because I have 2 enums with digfferent name the first is EnumAllowingAlias and the secund is EnumNotAllowingAlias this is the example in我不明白你的意思,因为我有两个名字不同的枚举,第一个是 EnumAllowingAlias,第二个是 EnumNotAllowingAlias 这是例子

https://developers.google.com/protocol-buffers/docs/proto#enum https://developers.google.com/protocol-buffers/docs/proto#enum

and before the example they explain在他们解释的例子之前

"You can define aliases by assigning the same value to different enum constants. To do this you need to set the allow_alias option to true, otherwise protocol compiler will generate an error message when aliases are found." “您可以通过为不同的枚举常量分配相同的值来定义别名。为此,您需要将 allow_alias 选项设置为 true,否则协议编译器将在找到别名时生成错误消息。”

In this case I can create 2 enums like these and the compiler should be create tow enums first using alias and the second without alias but instead of that the compiler sent me the error in the first answer.在这种情况下,我可以创建 2 个这样的枚举,编译器应该首先使用别名创建两个枚举,第二个没有别名,但编译器在第一个答案中向我发送了错误。

EnumAllowingAlias and EnumNotAllowingAlias can't be defined in the same proto file, they are just two examples for understanding the usage of allow_alias. EnumAllowingAlias 和 EnumNotAllowingAlias 不能定义在同一个 proto 文件中,它们只是理解 allow_alias 用法的两个例子。

allow_alias only work in the same enum, as mahdt said, allow_alias allows you to have two different names for the same value in a enum, the EnumAllowingAlias is the example for this case: allow_alias 只在同一个枚举中工作,正如 mahdt 所说,allow_alias 允许你在一个枚举中为相同的值使用两个不同的名称,EnumAllowingAlias 就是这种情况的例子:

enum EnumAllowingAlias {
  option allow_alias = true;
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;
}

If allow_alias is not set to true, the the EnumNotAllowingAlias will get an error:如果 allow_alias 未设置为 true,则 EnumNotAllowingAlias 将收到错误:

enum EnumNotAllowingAlias {
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;  // Uncommenting this line will cause a compile error inside Google and a warning message outside.
}

Enum usage don't have this limit in Java, I think it is really confusing for most developers, so I raised a bug report: https://github.com/protocolbuffers/protobuf/issues/5425 Enum的使用在Java中没有这个限制,我觉得对于大多数开发者来说真的很困惑,所以我提出了一个错误报告: https : //github.com/protocolbuffers/protobuf/issues/5425

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

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