encoding/textpb: fix handling of group field name

Group field name in textproto should be the type name.  Its field name
is derived from lowercasing its type name.

Change-Id: Ia12aafe934d3a59f3e07d09fe7939cfa6595a7b8
Reviewed-on: https://go-review.googlesource.com/c/157821
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Herbie Ong 2019-01-14 15:32:26 -08:00
parent 5681bb2587
commit 0dcfb9aa6a
4 changed files with 15 additions and 6 deletions

View File

@ -6,6 +6,7 @@ package textpb
import (
"fmt"
"strings"
"github.com/golang/protobuf/v2/internal/encoding/text"
"github.com/golang/protobuf/v2/internal/errors"
@ -103,6 +104,10 @@ func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message)
case text.Name:
name, _ = tkey.Name()
fd = fieldDescs.ByName(name)
if fd == nil {
// Check if this is a group field.
fd = fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
}
case text.String:
// TODO: Handle Any expansions here as well.

View File

@ -417,7 +417,7 @@ s_nested_enum: -0x80000000
inputMessage: &pb2.Nests{},
inputText: `
opt_nested: {}
optgroup: {}
OptGroup: {}
`,
wantMessage: &pb2.Nests{
OptNested: &pb2.Nested{},
@ -585,11 +585,11 @@ rpt_nested: {}
desc: "repeated group fields",
inputMessage: &pb2.Nests{},
inputText: `
rptgroup: {
RptGroup: {
rpt_bool: true
rpt_bool: false
}
rptgroup: {}
RptGroup: {}
`,
wantMessage: &pb2.Nests{
Rptgroup: []*pb2.Nests_RptGroup{

View File

@ -153,6 +153,10 @@ func (o MarshalOptions) appendField(msgFields [][2]text.Value, tname text.Value,
if !nerr.Merge(err) {
return msgFields, err
}
// Use type name for group field name.
if fd.Kind() == pref.GroupKind {
tname = text.ValueOf(fd.MessageType().Name())
}
msgFields = append(msgFields, [2]text.Value{tname, tval})
}

View File

@ -316,7 +316,7 @@ s_nested_enum: 47
Optgroup: &pb2.Nests_OptGroup{},
},
want: `opt_nested: {}
optgroup: {}
OptGroup: {}
`,
}, {
desc: "proto2 nested messages",
@ -349,13 +349,13 @@ optgroup: {}
},
},
},
want: `optgroup: {
want: `OptGroup: {
opt_bool: true
opt_string: "inside a group"
opt_nested: {
opt_string: "nested message inside a group"
}
optnestedgroup: {
OptNestedGroup: {
opt_enum: TENTH
}
}