diff options
author | Jie Luo <anandolee@gmail.com> | 2017-02-28 10:51:19 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-28 10:51:19 -0800 |
commit | 606cb7ed2c55d5bda8aad2c88e5b0af09230fb3b (patch) | |
tree | 1bc5c982a660f16604317f27365e769b3710b3ca /src/google | |
parent | d41c47fff992a7729a36ca61f8090aa40596e948 (diff) | |
download | protobuf-606cb7ed2c55d5bda8aad2c88e5b0af09230fb3b.tar.gz protobuf-606cb7ed2c55d5bda8aad2c88e5b0af09230fb3b.tar.bz2 protobuf-606cb7ed2c55d5bda8aad2c88e5b0af09230fb3b.zip |
There might be duplicated enum values when allow_alias is true. Add PreferredAlias into OriginalNameAttribute to remove the duplication (#2727)
Diffstat (limited to 'src/google')
-rw-r--r-- | src/google/protobuf/compiler/csharp/csharp_enum.cc | 17 | ||||
-rw-r--r-- | src/google/protobuf/unittest_proto3.proto | 3 |
2 files changed, 16 insertions, 4 deletions
diff --git a/src/google/protobuf/compiler/csharp/csharp_enum.cc b/src/google/protobuf/compiler/csharp/csharp_enum.cc index 9e4da1ed..9759e3ef 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum.cc +++ b/src/google/protobuf/compiler/csharp/csharp_enum.cc @@ -65,6 +65,7 @@ void EnumGenerator::Generate(io::Printer* printer) { "name", descriptor_->name()); printer->Indent(); std::set<string> used_names; + std::set<int> used_number; for (int i = 0; i < descriptor_->value_count(); i++) { WriteEnumValueDocComment(printer, descriptor_->value(i)); string original_name = descriptor_->value(i)->name(); @@ -76,10 +77,18 @@ void EnumGenerator::Generate(io::Printer* printer) { << ") in " << descriptor_->name() << "; adding underscore to distinguish"; name += "_"; } - printer->Print("[pbr::OriginalName(\"$original_name$\")] $name$ = $number$,\n", - "original_name", original_name, - "name", name, - "number", SimpleItoa(descriptor_->value(i)->number())); + int number = descriptor_->value(i)->number(); + if (!used_number.insert(number).second) { + printer->Print("[pbr::OriginalName(\"$original_name$\", PreferredAlias = false)] $name$ = $number$,\n", + "original_name", original_name, + "name", name, + "number", SimpleItoa(number)); + } else { + printer->Print("[pbr::OriginalName(\"$original_name$\")] $name$ = $number$,\n", + "original_name", original_name, + "name", name, + "number", SimpleItoa(number)); + } } printer->Outdent(); printer->Print("}\n"); diff --git a/src/google/protobuf/unittest_proto3.proto b/src/google/protobuf/unittest_proto3.proto index f59d2178..a27b1b26 100644 --- a/src/google/protobuf/unittest_proto3.proto +++ b/src/google/protobuf/unittest_proto3.proto @@ -200,6 +200,9 @@ message TestMutualRecursionB { int32 optional_int32 = 2; } +message TestEnumAllowAlias { + TestEnumWithDupValue value = 1; +} // Test an enum that has multiple values with the same number. enum TestEnumWithDupValue { |