Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not define two same enum name in the same proto file #5425

Closed
go4awalk opened this issue Dec 5, 2018 · 14 comments
Closed

Can not define two same enum name in the same proto file #5425

go4awalk opened this issue Dec 5, 2018 · 14 comments
Assignees

Comments

@go4awalk
Copy link

go4awalk commented Dec 5, 2018

Version: grpc-protobuf 1.14.0
Language: Java
OS: Windows 7

Steps to reproduce the behavior:
Define the following two enum in the same proto file, both TestEnum1 and TestEnum2 have a enum constant named UNKNOWN.

enum TestEnum1 {
    IDLE = 0;
    RUNNING = 1;
    UNKNOWN = -1;
}

enum TestEnum2 {
    MAN = 0;
    ANIMAL = 1;
    UNKNOWN = -1;
}

What did you expect to see
The proto will be compiled successfully.

What did you see instead?
I got the following compile error:
"UNKNOWN" is already defined in "xxx".

And it's interesting that we can't define two same enum constant in the same proto file, but protobuf will add a UNRECOGNIZED enum constant to every enum in the Java class generated after compiling.

@acozzette
Copy link
Member

This behavior is actually expected. The enum values go into the scope enclosing the whole enum definition, so in cases like your example, values in different enums can still conflict with each other. Probably the best way around it is just to put a prefix in front of UNKNOWN, like TEST_ENUM1_UNKNOWN for example.

@acozzette acozzette self-assigned this Dec 5, 2018
@go4awalk
Copy link
Author

go4awalk commented Dec 10, 2018

@acozzette
Although this behavior is expected, it's really not friendly to developer. Values in different enums don't conflict with each other in Java.

@TeBoring
Copy link
Contributor

TeBoring commented Dec 10, 2018

The general proto syntax has to taken all supported languages into account.

@ncioj10
Copy link

ncioj10 commented Apr 23, 2020

Isn't it possible to use scoped enums for c++ 11 and implement an option to specify which enums to be used. We mainly use Java and it's seriously utterly stupid that this requires these kinds of hacks.

@j-vizcaino
Copy link

j-vizcaino commented Dec 10, 2020

Same applies in Go where the enum value gets automatically prefixed with the enum name by the code generator.
Manually adding the enum name again as a prefix in the enum value results in stuttering and is very unpleasant to read and use 😢

@prakhar-ap
Copy link

Hi folks,
I was finding the best way to write enum but didn't found any solution what i am looking for.

I need to check that if this enum valid?

enum Scope {
  ...
 _ADMIN = 31;
}

paladin-devops added a commit to hashicorp/waypoint that referenced this issue Jul 12, 2023
This is necessary because OTHER and STORAGE are duplicated by enum ResourceCategoryDisplayHint, and this violates protobuf rules for enums.

protocolbuffers/protobuf#5425
paladin-devops added a commit to hashicorp/waypoint that referenced this issue Jul 17, 2023
This is necessary because OTHER and STORAGE are duplicated by enum ResourceCategoryDisplayHint, and this violates protobuf rules for enums.

protocolbuffers/protobuf#5425
@emeraldhieu
Copy link

emeraldhieu commented Jul 27, 2023

The design looks odd. Prefixing with the enum name sends me back to the old age of programming.

@sanan-fataliyev
Copy link

It is common use case to define UNSPECIFIED/UNKNOWN=0 in enums to avoid undesired defaults, and require explicit values.
Providing syntax to define multiple enum types without isolated scope for their values looks like an incomplete design.

@anthonyalayo
Copy link

Is there any plans to fix this? Is there any clean workaround for this in 2024? It really doesn't make any sense.

@hayyaun
Copy link

hayyaun commented Mar 15, 2024

+1

@jotalanusse
Copy link

JavaScript projects that use @grpc/proto-loader rely on being able to convert enum values into strings and use them as such. The fact that the current solution requires prefixing every enum value with the name of the enum is ridiculous.

Please fix the current design!

@domeniconappo
Copy link

For the series: things you won't find in the README.md.

@ZheruiL
Copy link

ZheruiL commented Jul 27, 2024

at least remove the prefix if the prefix has the same name as the enum name please, for example:

enum PhoneType {
  PHONE_TYPE_UNSPECIFIED = 0;
  PHONE_TYPE_MOBILE = 1;
  PHONE_TYPE_HOME = 2;
  PHONE_TYPE_WORK = 3;
}
type PhoneType int32

const (
	PhoneType_PHONE_TYPE_UNSPECIFIED PhoneType = 0
	PhoneType_PHONE_TYPE_MOBILE      PhoneType = 1
	PhoneType_PHONE_TYPE_HOME        PhoneType = 2
	PhoneType_PHONE_TYPE_WORK        PhoneType = 3
)

it repeats PhoneType twice.

can't it be just PhoneType_MOBILE as a result ?

@fastfend
Copy link

It looks like after this issue was closed it went directly into "do not elaborate further" trash bin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests