@@ -1607,18 +1607,36 @@ func (g *Generator) generateMessage(message *Descriptor) {
16071607 g .P ("type " , ccTypeName , " struct {" )
16081608 g .In ()
16091609
1610- allocName := func (basis string ) string {
1611- n := CamelCase (basis )
1612- for usedNames [n ] {
1613- n += "_"
1610+ // allocNames finds a conflict-free variation of the given strings,
1611+ // consistently mutating their suffixes.
1612+ // It returns the same number of strings.
1613+ allocNames := func (ns ... string ) []string {
1614+ Loop:
1615+ for {
1616+ for _ , n := range ns {
1617+ if usedNames [n ] {
1618+ for i := range ns {
1619+ ns [i ] += "_"
1620+ }
1621+ continue Loop
1622+ }
1623+ }
1624+ for _ , n := range ns {
1625+ usedNames [n ] = true
1626+ }
1627+ return ns
16141628 }
1615- usedNames [n ] = true
1616- return n
16171629 }
16181630
16191631 for i , field := range message .Field {
1620- fieldName := allocName (* field .Name )
1621- fieldGetterName := fieldName
1632+ // Allocate the getter and the field at the same time so name
1633+ // collisions create field/method consistent names.
1634+ // TODO: This allocation occurs based on the order of the fields
1635+ // in the proto file, meaning that a change in the field
1636+ // ordering can change generated Method/Field names.
1637+ base := CamelCase (* field .Name )
1638+ ns := allocNames (base , "Get" + base )
1639+ fieldName , fieldGetterName := ns [0 ], ns [1 ]
16221640 typename , wiretype := g .GoType (message , field )
16231641 jsonName := * field .Name
16241642 tag := fmt .Sprintf ("protobuf:%s json:%q" , g .goTag (message , field , wiretype ), jsonName + ",omitempty" )
@@ -1629,7 +1647,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
16291647 oneof := field .OneofIndex != nil
16301648 if oneof && oneofFieldName [* field .OneofIndex ] == "" {
16311649 odp := message .OneofDecl [int (* field .OneofIndex )]
1632- fname := allocName ( odp .GetName ())
1650+ fname := allocNames ( CamelCase ( odp .GetName ()))[ 0 ]
16331651
16341652 // This is the first field of a oneof we haven't seen before.
16351653 // Generate the union field.
@@ -1907,7 +1925,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
19071925 if t , ok := mapFieldTypes [field ]; ok {
19081926 typename = t
19091927 }
1910- mname := "Get" + fieldGetterNames [field ]
1928+ mname := fieldGetterNames [field ]
19111929 star := ""
19121930 if needsStar (* field .Type ) && typename [0 ] == '*' {
19131931 typename = typename [1 :]
0 commit comments