最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

protocol buffers - Why does protoc fail with enum name conflict in C++ for valid .proto definitions? - Stack Overflow

programmeradmin2浏览0评论

I have a public proto files which cant be altered here is this for Refrence

syntax = "proto2";

package openrtb;

enum ContentCategory {
  IAB1 = 1;
  IAB1_1 = 2;
  IAB1_2 = 3;
  IAB11 = 191;
  IAB11_1 = 192;
}

When I run:

protoc   --proto_path=.   --cpp_out=./src   --grpc_out=./src   --plugin=protoc-gen-grpc=`which grpc_cpp_plugin`   test2.proto

It says Warning message and doesn't generate the file

test2.proto:9:3: Enum name IAB11 has the same name as IAB1_1 if you ignore case and strip out the enum name prefix (if any). (If you are using allow_alias, please assign the same number to each enum value name.)

For generating in Go same message but it's able to generate the file, I'm just trying to get valid C++ code generation without modifying the proto file.

I have a public proto files which cant be altered here is this for Refrence

syntax = "proto2";

package openrtb;

enum ContentCategory {
  IAB1 = 1;
  IAB1_1 = 2;
  IAB1_2 = 3;
  IAB11 = 191;
  IAB11_1 = 192;
}

When I run:

protoc   --proto_path=.   --cpp_out=./src   --grpc_out=./src   --plugin=protoc-gen-grpc=`which grpc_cpp_plugin`   test2.proto

It says Warning message and doesn't generate the file

test2.proto:9:3: Enum name IAB11 has the same name as IAB1_1 if you ignore case and strip out the enum name prefix (if any). (If you are using allow_alias, please assign the same number to each enum value name.)

For generating in Go same message but it's able to generate the file, I'm just trying to get valid C++ code generation without modifying the proto file.

Share Improve this question edited Mar 30 at 12:09 anish asked Mar 30 at 11:51 anishanish 7,44814 gold badges84 silver badges153 bronze badges 14
  • 2 It is failing for an invalid .proto definition. There are two ways to fix it: change the enumerator name, or have the enumerator value match the aliasing value. – Eljay Commented Mar 30 at 12:00
  • @Eljay cant change the Enumerator name as this is a shared public one. can you be more specific on "or have the enumerator value match the aliasing value" ?? – anish Commented Mar 30 at 12:08
  • My bad, I think during transformation IAB1_1 = 2; is treated as IAB11 how can i stop this – anish Commented Mar 30 at 12:10
  • 1 Fix the problem in OpenRTB, submit a pull request, wait for the change to be approved, fetch the change. – Eljay Commented Mar 30 at 14:00
  • 1 I am able to repro the error using the OP proto and the openrtb.proto referenced by @3cxezivlq using protoc v30.2 (latest). However, I believe the issue is in the outdated (5 year old) proto used by the bid-valuator-endpoint-java code. The current (!?) RTB Protos openrtb-proto compiles successfully. The issue is confusingly reported but appears to be this and a couple of other relateds. – DazWilkin Commented Mar 30 at 17:20
 |  Show 9 more comments

1 Answer 1

Reset to default 2

This confusing behavior of protoc has been reported upstream in multiple issues: #9272, #16375 and #18149.

The underlying cause is not common prefixes, as the error message would lead you to believe, but the underscores. Protoc strips them out in the comparison, because some language generators (at least C# and JSON) convert them to case differences. For example MY_ENUM becomes MyEnum and IAB1_1 becomes Iab11.

Protobuf specification mentions this in the section JSON Name Conflicts.

Apparently the Go generator for protobuf does not check for this, because it does not do this name transformation in its output.

The most straightforward option is to have a local copy of the file, and change the names to use a letter in place of the underscore. Normally the encoded format of protobuf only uses the numbers, to changing the names wouldn't be a problem for compatibility. However, in this specific case it looks like it is the names that are used in string arrays, and not the enum value itself. That is a weird design for a proto file. But in this case you can just discard the enumeration and hard-code the texts.

Ideally you would report this bug to the project containing the .proto files, but it may be difficult for them to make the change as the names would affect many projects. According to a comment by DazWilkin, the latest version openrtb-proto does not contain the problem, but on the other hand it might not be compatible with the project you are using.

发布评论

评论列表(0)

  1. 暂无评论