encoding: avoid direct access to XXX_unrecognized

Use the SetUnknown API instead.

Change-Id: Id9dc81581084ccf8ef5453a80c75fcfd63289636
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/184877
Reviewed-by: Herbie Ong <herbie@google.com>
This commit is contained in:
Joe Tsai 2019-06-22 13:20:09 -07:00
parent 8d30bbeede
commit 28216c7cf2
2 changed files with 34 additions and 22 deletions

View File

@ -886,12 +886,15 @@ func TestMarshal(t *testing.T) {
}`,
}, {
desc: "unknown fields are ignored",
input: &pb2.Scalars{
OptString: scalar.String("no unknowns"),
XXX_unrecognized: pack.Message{
input: func() proto.Message {
m := &pb2.Scalars{
OptString: scalar.String("no unknowns"),
}
m.ProtoReflect().SetUnknown(pack.Message{
pack.Tag{101, pack.BytesType}, pack.String("hello world"),
}.Marshal(),
},
}.Marshal())
return m
}(),
want: `{
"optString": "no unknowns"
}`,

View File

@ -849,15 +849,18 @@ req_nested: {}
want: "oneof_nested: {}\n",
}, {
desc: "unknown varint and fixed types",
input: &pb2.Scalars{
OptString: scalar.String("this message contains unknown fields"),
XXX_unrecognized: pack.Message{
input: func() proto.Message {
m := &pb2.Scalars{
OptString: scalar.String("this message contains unknown fields"),
}
m.ProtoReflect().SetUnknown(pack.Message{
pack.Tag{101, pack.VarintType}, pack.Bool(true),
pack.Tag{102, pack.VarintType}, pack.Varint(0xff),
pack.Tag{103, pack.Fixed32Type}, pack.Uint32(47),
pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef),
}.Marshal(),
},
}.Marshal())
return m
}(),
want: `opt_string: "this message contains unknown fields"
101: 1
102: 255
@ -866,28 +869,32 @@ req_nested: {}
`,
}, {
desc: "unknown length-delimited",
input: &pb2.Scalars{
XXX_unrecognized: pack.Message{
input: func() proto.Message {
m := new(pb2.Scalars)
m.ProtoReflect().SetUnknown(pack.Message{
pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false)},
pack.Tag{102, pack.BytesType}, pack.String("hello world"),
pack.Tag{103, pack.BytesType}, pack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"),
}.Marshal(),
},
}.Marshal())
return m
}(),
want: `101: "\x01\x00"
102: "hello world"
103: "世界"
`,
}, {
desc: "unknown group type",
input: &pb2.Scalars{
XXX_unrecognized: pack.Message{
input: func() proto.Message {
m := new(pb2.Scalars)
m.ProtoReflect().SetUnknown(pack.Message{
pack.Tag{101, pack.StartGroupType}, pack.Tag{101, pack.EndGroupType},
pack.Tag{102, pack.StartGroupType},
pack.Tag{101, pack.VarintType}, pack.Bool(false),
pack.Tag{102, pack.BytesType}, pack.String("inside a group"),
pack.Tag{102, pack.EndGroupType},
}.Marshal(),
},
}.Marshal())
return m
}(),
want: `101: {}
102: {
101: 0
@ -896,14 +903,16 @@ req_nested: {}
`,
}, {
desc: "unknown unpack repeated field",
input: &pb2.Scalars{
XXX_unrecognized: pack.Message{
input: func() proto.Message {
m := new(pb2.Scalars)
m.ProtoReflect().SetUnknown(pack.Message{
pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Bool(true)},
pack.Tag{102, pack.BytesType}, pack.String("hello"),
pack.Tag{101, pack.VarintType}, pack.Bool(true),
pack.Tag{102, pack.BytesType}, pack.String("世界"),
}.Marshal(),
},
}.Marshal())
return m
}(),
want: `101: "\x01\x00\x01"
102: "hello"
101: 1