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>
diff --git a/encoding/prototext/encode_test.go b/encoding/prototext/encode_test.go
index 42f4e98..01e8a64 100644
--- a/encoding/prototext/encode_test.go
+++ b/encoding/prototext/encode_test.go
@@ -849,15 +849,18 @@
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 @@
`,
}, {
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 @@
`,
}, {
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