mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-03-15 22:20:52 +00:00
encoding/textpb: add tests for nil message in repeated and map
Updates golang/protobuf#798 by adding testcases to show the intention. Also, slightly move code blocks in encode.go w/o affecting logic to make it cleaner. Change-Id: I14575f6e7139a0908483bd318b599339c2daf8ad Reviewed-on: https://go-review.googlesource.com/c/161717 Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
6470ea6e0f
commit
f5db2df407
@ -46,9 +46,7 @@ func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
|
||||
}
|
||||
|
||||
var nerr errors.NonFatal
|
||||
var v text.Value
|
||||
var err error
|
||||
v, err = o.marshalMessage(m.ProtoReflect())
|
||||
v, err := o.marshalMessage(m.ProtoReflect())
|
||||
if !nerr.Merge(err) {
|
||||
return nil, err
|
||||
}
|
||||
@ -99,10 +97,14 @@ func (o MarshalOptions) marshalMessage(m pref.Message) (text.Value, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
tname := text.ValueOf(fd.Name())
|
||||
name := text.ValueOf(fd.Name())
|
||||
// Use type name for group field name.
|
||||
if fd.Kind() == pref.GroupKind {
|
||||
name = text.ValueOf(fd.MessageType().Name())
|
||||
}
|
||||
pval := knownFields.Get(num)
|
||||
var err error
|
||||
msgFields, err = o.appendField(msgFields, tname, pval, fd)
|
||||
msgFields, err = o.appendField(msgFields, name, pval, fd)
|
||||
if !nerr.Merge(err) {
|
||||
return text.Value{}, err
|
||||
}
|
||||
@ -126,14 +128,9 @@ func (o MarshalOptions) marshalMessage(m pref.Message) (text.Value, error) {
|
||||
}
|
||||
|
||||
// appendField marshals a protoreflect.Value and appends it to the given [][2]text.Value.
|
||||
func (o MarshalOptions) appendField(msgFields [][2]text.Value, tname text.Value, pval pref.Value, fd pref.FieldDescriptor) ([][2]text.Value, error) {
|
||||
func (o MarshalOptions) appendField(msgFields [][2]text.Value, name text.Value, pval pref.Value, fd pref.FieldDescriptor) ([][2]text.Value, error) {
|
||||
var nerr errors.NonFatal
|
||||
|
||||
// Use type name for group field name.
|
||||
if fd.Kind() == pref.GroupKind {
|
||||
tname = text.ValueOf(fd.MessageType().Name())
|
||||
}
|
||||
|
||||
if fd.Cardinality() == pref.Repeated {
|
||||
// Map or repeated fields.
|
||||
var items []text.Value
|
||||
@ -152,7 +149,7 @@ func (o MarshalOptions) appendField(msgFields [][2]text.Value, tname text.Value,
|
||||
|
||||
// Add each item as key: value field.
|
||||
for _, item := range items {
|
||||
msgFields = append(msgFields, [2]text.Value{tname, item})
|
||||
msgFields = append(msgFields, [2]text.Value{name, item})
|
||||
}
|
||||
} else {
|
||||
// Required or optional fields.
|
||||
@ -160,7 +157,7 @@ func (o MarshalOptions) appendField(msgFields [][2]text.Value, tname text.Value,
|
||||
if !nerr.Merge(err) {
|
||||
return msgFields, err
|
||||
}
|
||||
msgFields = append(msgFields, [2]text.Value{tname, tval})
|
||||
msgFields = append(msgFields, [2]text.Value{name, tval})
|
||||
}
|
||||
|
||||
return msgFields, nerr.E
|
||||
|
@ -529,6 +529,14 @@ rpt_nested: {
|
||||
}
|
||||
}
|
||||
rpt_nested: {}
|
||||
`,
|
||||
}, {
|
||||
desc: "repeated nested messages contains nil value",
|
||||
input: &pb2.Nests{
|
||||
RptNested: []*pb2.Nested{nil, {}},
|
||||
},
|
||||
want: `rpt_nested: {}
|
||||
rpt_nested: {}
|
||||
`,
|
||||
}, {
|
||||
desc: "repeated group fields",
|
||||
@ -677,6 +685,18 @@ str_to_oneofs: {
|
||||
str: "hello"
|
||||
}
|
||||
}
|
||||
`,
|
||||
}, {
|
||||
desc: "map field nil message value",
|
||||
input: &pb2.Maps{
|
||||
StrToNested: map[string]*pb2.Nested{
|
||||
"nil": nil,
|
||||
},
|
||||
},
|
||||
want: `str_to_nested: {
|
||||
key: "nil"
|
||||
value: {}
|
||||
}
|
||||
`,
|
||||
}, {
|
||||
desc: "proto2 required fields not set",
|
||||
|
Loading…
x
Reference in New Issue
Block a user