Explorar el Código

Add support for plumbing json_name from protoc through to proto.Properties.

This will be used by the jsonpb package at a later date.
David Symonds hace 10 años
padre
commit
d20896fc31
Se han modificado 2 ficheros con 11 adiciones y 2 borrados
  1. 6 2
      proto/properties.go
  2. 5 0
      protoc-gen-go/generator/generator.go

+ 6 - 2
proto/properties.go

@@ -173,6 +173,7 @@ func (sp *StructProperties) Swap(i, j int) { sp.order[i], sp.order[j] = sp.order
 type Properties struct {
 	Name     string // name of the field, for error messages
 	OrigName string // original name before protocol compiler (always set)
+	JSONName string // name to use for JSON; determined by protoc
 	Wire     string
 	WireType int
 	Tag      int
@@ -229,8 +230,9 @@ func (p *Properties) String() string {
 	if p.Packed {
 		s += ",packed"
 	}
-	if p.OrigName != p.Name {
-		s += ",name=" + p.OrigName
+	s += ",name=" + p.OrigName
+	if p.JSONName != p.OrigName {
+		s += ",json=" + p.JSONName
 	}
 	if p.proto3 {
 		s += ",proto3"
@@ -310,6 +312,8 @@ func (p *Properties) Parse(s string) {
 			p.Packed = true
 		case strings.HasPrefix(f, "name="):
 			p.OrigName = f[5:]
+		case strings.HasPrefix(f, "json="):
+			p.JSONName = f[5:]
 		case strings.HasPrefix(f, "enum="):
 			p.Enum = f[5:]
 		case f == "proto3":

+ 5 - 0
protoc-gen-go/generator/generator.go

@@ -1517,6 +1517,11 @@ func (g *Generator) goTag(message *Descriptor, field *descriptor.FieldDescriptor
 			name = name[i+1:]
 		}
 	}
+	if json := field.GetJsonName(); json != "" && json != name {
+		// TODO: escaping might be needed, in which case
+		// perhaps this should be in its own "json" tag.
+		name += ",json=" + json
+	}
 	name = ",name=" + name
 	if message.proto3() {
 		// We only need the extra tag for []byte fields;