Skip to content

Commit 347cf4a

Browse files
authored
protoc-gen-go: fix oneof name mangling regression (#782)
The generator currently uses an unintuitive and stateful algorithm for name generation where it "fixes" name conflicts by appending "_" to the end of the new name. PR#657 refactored the generator code and noticed that the above algorithm was not properly taking into account that a Get method is generated for parent oneofs, fixing it in the same PR. While this is more correct, this breaks users (see #780) since it means that the generation of names can change. This PR changes the name mangling logic to be as it was previously. This does mean that some new proto files may be unbuildable, but that is arguably better than breaking existing proto files. Fixes #780
1 parent 1d3f30b commit 347cf4a

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

protoc-gen-go/generator/generator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,8 +2246,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
22462246
if oneof && oFields[*field.OneofIndex] == nil {
22472247
odp := message.OneofDecl[int(*field.OneofIndex)]
22482248
base := CamelCase(odp.GetName())
2249-
names := allocNames(base, "Get"+base)
2250-
fname, gname := names[0], names[1]
2249+
fname := allocNames(base)[0]
22512250

22522251
// This is the first field of a oneof we haven't seen before.
22532252
// Generate the union field.
@@ -2265,7 +2264,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
22652264
of := oneofField{
22662265
fieldCommon: fieldCommon{
22672266
goName: fname,
2268-
getterName: gname,
2267+
getterName: "Get"+fname,
22692268
goType: dname,
22702269
tags: tag,
22712270
protoName: odp.GetName(),

protoc-gen-go/testdata/issue780_oneof_conflict/test.pb.go

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto2";
2+
3+
package oneoftest;
4+
5+
message Foo {
6+
oneof bar {
7+
string get_bar = 1;
8+
}
9+
}

0 commit comments

Comments
 (0)