Protocol Buffer 使用枚举(enum)。

huangapple go评论97阅读模式
英文:

Protocol Buffer use enum

问题

你好,我正在使用protobuf工作,但遇到了一个问题。

我有一些枚举函数,但其中两个函数具有相同的别名。当我尝试将文件编译为"go"语言时,编译器会返回一个错误。

我复制了protobuf文档中定义枚举的示例,但仍然无法工作。

以下是谷歌文档中的说明:如果你需要在不同的枚举中使用相同的别名,你需要在枚举中添加"option allow_alias = true;"选项。但是在尝试编译.proto文件后,编译器给出了以下响应:

example.proto:13:5: "UNKNOWN"已在"namespace"中定义。
example.proto:13:5: 请注意,枚举值使用C++作用域规则,这意味着枚举值是其类型的同级,而不是其子级。
因此,"UNKNOWN"必须在"kluso"中是唯一的,而不仅仅在"EnumNotAllowingAlias"中。

example.proto:14:5: "STARTED"已在"namespace"中定义。
example.proto:14:5: 请注意,枚举值使用C++作用域规则,这意味着枚举值是其类型的同级,而不是其子级。
因此,"STARTED"必须在"kluso"中是唯一的,而不仅仅在"EnumNotAllowingAlias"中。

我不知道出了什么问题。有人知道问题是什么吗?

英文:

Hello I'm working with protobuf but I have a problem.

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.

I copied the example in protobuf documentation to define the enum and still not working.

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;" in the enum but after try to compile the .proto file the compiler response.

> example.proto:13:5: "UNKNOWN" is already defined in "namespace".
> 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.
> Therefore, "UNKNOWN" must be unique within "kluso", not just within "EnumNotAllowingAlias".
>
> example.proto:14:5: "STARTED" is already defined in "namespace".
> 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.
> Therefore, "STARTED" must be unique within "kluso", not just within "EnumNotAllowingAlias".

I don't know what's happen. Someone know what is the problem?

答案1

得分: 4

allow_alias允许您为同一个值拥有两个不同的名称

然而,它仍然不允许您使用相同的名称两次!

在您从文档中提取的示例中,他们只是试图演示如何通过将allow_alias设置为true,在同一个枚举类型中使用STARTED = 1和RUNNING = 1。他们给出的两个枚举示例不是用于同一个包中。

但是,如果您已经在EnumAllowingAlias中有RUNNING,如果它们在同一个包中,您不能再在EnumNotAllowingAlias中使用RUNNING。

英文:

allow_alias allows you to have two different names for the same value.

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. 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.

答案2

得分: 3

你要找的答案在错误信息中 Protocol Buffer 使用枚举(enum)。 只是有点难以解析出它实际在告诉你什么。

基本上,enum values are siblings of their type, not children of it 的意思是,枚举值不是作为 MyEnum.FOOMyEnum2.FOO 这样的作用域存在,而是与 MyEnum 在同一级别的作用域。因此,同一 .proto 文件中的两个枚举值不能具有相同的名称;它们都试图作为 FOO 存在于该文件中,而不是作为 MyEnum.FOOMyEnum2.FOO

英文:

The answer you're looking for is in the error message Protocol Buffer 使用枚举(enum)。 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. So two enum values in the same .proto file cannot have the same name; they're both trying to exist as FOO within that file, rather than being MyEnum.FOO and MyEnum2.FOO.

答案3

得分: 1

EnumAllowingAlias和EnumNotAllowingAlias不能在同一个proto文件中定义,它们只是为了理解allow_alias的用法而提供的两个示例。

allow_alias只在同一个枚举中起作用,正如mahdt所说,allow_alias允许在枚举中为相同的值使用两个不同的名称,EnumAllowingAlias就是这种情况的示例:

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

如果allow_alias没有设置为true,EnumNotAllowingAlias将会出错:

enum EnumNotAllowingAlias {
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;  // 取消此行的注释将导致Google内部编译错误和外部的警告信息。
}

在Java中,枚举的使用没有这个限制,我认为这对大多数开发人员来说确实很令人困惑,所以我提了一个错误报告:
https://github.com/protocolbuffers/protobuf/issues/5425

英文:

EnumAllowingAlias and EnumNotAllowingAlias can't be defined in the same proto file, they are just two examples for understanding the usage of 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:

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:

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

答案4

得分: 0

我不理解你的观点,因为我有两个不同名称的枚举,第一个是EnumAllowingAlias,第二个是EnumNotAllowingAlias。这是在以下链接中的示例:

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

在示例之前,他们解释了:

"您可以通过将相同的值分配给不同的枚举常量来定义别名。要做到这一点,您需要将allow_alias选项设置为true,否则协议编译器在找到别名时会生成错误消息。"

在这种情况下,我可以创建两个像这样的枚举,编译器应该首先使用别名创建第一个枚举,然后创建第二个枚举时不使用别名,但是编译器给我发送了第一个答案中的错误信息。

英文:

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

> 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."

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.

huangapple
  • 本文由 发表于 2016年3月23日 23:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/36182638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定