Просмотр исходного кода

goprotobuf: Avoid generating a struct that uses the same name for a field and a method.

R=r
CC=golang-dev
http://codereview.appspot.com/5645090
David Symonds 14 лет назад
Родитель
Сommit
3ce5303a35

+ 19 - 0
compiler/generator/generator.go

@@ -1125,6 +1125,17 @@ func (g *Generator) RecordTypeUse(t string) {
 	}
 }
 
+// Method names that may be generated.  Fields with these names get an
+// underscore appended.
+var methodNames = [...]string{
+	"Reset",
+	"String",
+	"Marshal",
+	"Unmarshal",
+	"ExtensionRangeArray",
+	"ExtensionMap",
+}
+
 // Generate the type and default constant definitions for this Descriptor.
 func (g *Generator) generateMessage(message *Descriptor) {
 	// The full type name
@@ -1132,6 +1143,10 @@ func (g *Generator) generateMessage(message *Descriptor) {
 	// The full type name, CamelCased.
 	ccTypeName := CamelCaseSlice(typeName)
 
+	usedNames := make(map[string]bool)
+	for _, n := range methodNames {
+		usedNames[n] = true
+	}
 	g.P("type ", ccTypeName, " struct {")
 	g.In()
 	for _, field := range message.Field {
@@ -1142,6 +1157,10 @@ func (g *Generator) generateMessage(message *Descriptor) {
 			continue
 		}
 		fieldname := CamelCase(*field.Name)
+		for usedNames[fieldname] {
+			fieldname += "_"
+		}
+		usedNames[fieldname] = true
 		typename, wiretype := g.GoType(message, field)
 		jsonName := *field.Name
 		tag := fmt.Sprintf("`protobuf:%s json:%q`", g.goTag(field, wiretype), jsonName+",omitempty")

+ 1 - 0
compiler/testdata/test.pb.go.golden

@@ -123,6 +123,7 @@ type Request struct {
 	Owner            *imp1.ImportedMessage_Owner `protobuf:"varint,6,opt,name=owner,enum=imp.ImportedMessage_Owner" json:"owner,omitempty"`
 	Deadline         *float32                    `protobuf:"fixed32,7,opt,name=deadline,def=inf" json:"deadline,omitempty"`
 	Somegroup        *Request_SomeGroup          `protobuf:"group,8,opt,name=SomeGroup" json:"somegroup,omitempty"`
+	Reset_           *int32                      `protobuf:"varint,12,opt,name=reset" json:"reset,omitempty"`
 	XXX_unrecognized []byte                      `json:",omitempty"`
 }
 

+ 2 - 0
compiler/testdata/test.proto

@@ -66,6 +66,8 @@ message Request {
   // which is publicly imported by imp.proto.
   // optional imp.PubliclyImportedMessage pub = 10;
 
+
+  optional int32 reset = 12;
 }
 
 message Reply {