testing/protopack: make package publicly available

Change-Id: I342ed27df17867f18c58e60880bcac5a31a3096b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/219837
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2020-02-14 18:13:14 -08:00
parent cd108d00a8
commit cfd80493c5
14 changed files with 892 additions and 892 deletions

View File

@ -13,10 +13,10 @@ import (
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/internal/detrand" "google.golang.org/protobuf/internal/detrand"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
preg "google.golang.org/protobuf/reflect/protoregistry" preg "google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/testing/protopack"
fieldmaskpb "google.golang.org/protobuf/internal/testprotos/fieldmaskpb" fieldmaskpb "google.golang.org/protobuf/internal/testprotos/fieldmaskpb"
pb2 "google.golang.org/protobuf/internal/testprotos/textpb2" pb2 "google.golang.org/protobuf/internal/testprotos/textpb2"
@ -860,8 +860,8 @@ func TestMarshal(t *testing.T) {
m := &pb2.Scalars{ m := &pb2.Scalars{
OptString: proto.String("no unknowns"), OptString: proto.String("no unknowns"),
} }
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{101, pack.BytesType}, pack.String("hello world"), protopack.Tag{101, protopack.BytesType}, protopack.String("hello world"),
}.Marshal()) }.Marshal())
return m return m
}(), }(),

View File

@ -12,10 +12,10 @@ import (
"google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/internal/detrand" "google.golang.org/protobuf/internal/detrand"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
preg "google.golang.org/protobuf/reflect/protoregistry" preg "google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/testing/protopack"
pb2 "google.golang.org/protobuf/internal/testprotos/textpb2" pb2 "google.golang.org/protobuf/internal/testprotos/textpb2"
pb3 "google.golang.org/protobuf/internal/testprotos/textpb3" pb3 "google.golang.org/protobuf/internal/testprotos/textpb3"
@ -826,11 +826,11 @@ req_nested: {}
m := &pb2.Scalars{ m := &pb2.Scalars{
OptString: proto.String("this message contains unknown fields"), OptString: proto.String("this message contains unknown fields"),
} }
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{101, pack.VarintType}, pack.Bool(true), protopack.Tag{101, protopack.VarintType}, protopack.Bool(true),
pack.Tag{102, pack.VarintType}, pack.Varint(0xff), protopack.Tag{102, protopack.VarintType}, protopack.Varint(0xff),
pack.Tag{103, pack.Fixed32Type}, pack.Uint32(47), protopack.Tag{103, protopack.Fixed32Type}, protopack.Uint32(47),
pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef), protopack.Tag{104, protopack.Fixed64Type}, protopack.Int64(0xdeadbeef),
}.Marshal()) }.Marshal())
return m return m
}(), }(),
@ -843,11 +843,11 @@ req_nested: {}
m := &pb2.Scalars{ m := &pb2.Scalars{
OptString: proto.String("this message contains unknown fields"), OptString: proto.String("this message contains unknown fields"),
} }
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{101, pack.VarintType}, pack.Bool(true), protopack.Tag{101, protopack.VarintType}, protopack.Bool(true),
pack.Tag{102, pack.VarintType}, pack.Varint(0xff), protopack.Tag{102, protopack.VarintType}, protopack.Varint(0xff),
pack.Tag{103, pack.Fixed32Type}, pack.Uint32(0x47), protopack.Tag{103, protopack.Fixed32Type}, protopack.Uint32(0x47),
pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef), protopack.Tag{104, protopack.Fixed64Type}, protopack.Int64(0xdeadbeef),
}.Marshal()) }.Marshal())
return m return m
}(), }(),
@ -862,10 +862,10 @@ req_nested: {}
mo: prototext.MarshalOptions{EmitUnknown: true}, mo: prototext.MarshalOptions{EmitUnknown: true},
input: func() proto.Message { input: func() proto.Message {
m := new(pb2.Scalars) m := new(pb2.Scalars)
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false)}, protopack.Tag{101, protopack.BytesType}, protopack.LengthPrefix{protopack.Bool(true), protopack.Bool(false)},
pack.Tag{102, pack.BytesType}, pack.String("hello world"), protopack.Tag{102, protopack.BytesType}, protopack.String("hello world"),
pack.Tag{103, pack.BytesType}, pack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"), protopack.Tag{103, protopack.BytesType}, protopack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"),
}.Marshal()) }.Marshal())
return m return m
}(), }(),
@ -878,12 +878,12 @@ req_nested: {}
mo: prototext.MarshalOptions{EmitUnknown: true}, mo: prototext.MarshalOptions{EmitUnknown: true},
input: func() proto.Message { input: func() proto.Message {
m := new(pb2.Scalars) m := new(pb2.Scalars)
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{101, pack.StartGroupType}, pack.Tag{101, pack.EndGroupType}, protopack.Tag{101, protopack.StartGroupType}, protopack.Tag{101, protopack.EndGroupType},
pack.Tag{102, pack.StartGroupType}, protopack.Tag{102, protopack.StartGroupType},
pack.Tag{101, pack.VarintType}, pack.Bool(false), protopack.Tag{101, protopack.VarintType}, protopack.Bool(false),
pack.Tag{102, pack.BytesType}, pack.String("inside a group"), protopack.Tag{102, protopack.BytesType}, protopack.String("inside a group"),
pack.Tag{102, pack.EndGroupType}, protopack.Tag{102, protopack.EndGroupType},
}.Marshal()) }.Marshal())
return m return m
}(), }(),
@ -898,11 +898,11 @@ req_nested: {}
mo: prototext.MarshalOptions{EmitUnknown: true}, mo: prototext.MarshalOptions{EmitUnknown: true},
input: func() proto.Message { input: func() proto.Message {
m := new(pb2.Scalars) m := new(pb2.Scalars)
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Bool(true)}, protopack.Tag{101, protopack.BytesType}, protopack.LengthPrefix{protopack.Bool(true), protopack.Bool(false), protopack.Bool(true)},
pack.Tag{102, pack.BytesType}, pack.String("hello"), protopack.Tag{102, protopack.BytesType}, protopack.String("hello"),
pack.Tag{101, pack.VarintType}, pack.Bool(true), protopack.Tag{101, protopack.VarintType}, protopack.Bool(true),
pack.Tag{102, pack.BytesType}, pack.String("世界"), protopack.Tag{102, protopack.BytesType}, protopack.String("世界"),
}.Marshal()) }.Marshal())
return m return m
}(), }(),

View File

@ -18,11 +18,11 @@ import (
"strings" "strings"
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc" "google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/testing/protopack"
"google.golang.org/protobuf/types/descriptorpb" "google.golang.org/protobuf/types/descriptorpb"
) )
@ -127,7 +127,7 @@ func main() {
// Parse and print message structure. // Parse and print message structure.
defer log.Printf("fatal input: %q", buf) // debug printout if panic occurs defer log.Printf("fatal input: %q", buf) // debug printout if panic occurs
var m pack.Message var m protopack.Message
m.UnmarshalDescriptor(buf, desc) m.UnmarshalDescriptor(buf, desc)
if *printSource { if *printSource {
fmt.Printf("%#v\n", m) fmt.Printf("%#v\n", m)

View File

@ -11,9 +11,9 @@ import (
"testing" "testing"
"google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/testing/protopack"
testpb "google.golang.org/protobuf/internal/testprotos/test" testpb "google.golang.org/protobuf/internal/testprotos/test"
test3pb "google.golang.org/protobuf/internal/testprotos/test3" test3pb "google.golang.org/protobuf/internal/testprotos/test3"
@ -91,8 +91,8 @@ func TestDecodeInvalidMessages(t *testing.T) {
func TestDecodeZeroLengthBytes(t *testing.T) { func TestDecodeZeroLengthBytes(t *testing.T) {
// Verify that proto3 bytes fields don't give the mistaken // Verify that proto3 bytes fields don't give the mistaken
// impression that they preserve presence. // impression that they preserve presence.
wire := pack.Message{ wire := protopack.Message{
pack.Tag{15, pack.BytesType}, pack.Bytes(nil), protopack.Tag{15, protopack.BytesType}, protopack.Bytes(nil),
}.Marshal() }.Marshal()
m := &test3pb.TestAllTypes{} m := &test3pb.TestAllTypes{}
if err := proto.Unmarshal(wire, m); err != nil { if err := proto.Unmarshal(wire, m); err != nil {
@ -104,8 +104,8 @@ func TestDecodeZeroLengthBytes(t *testing.T) {
} }
func TestDecodeOneofNilWrapper(t *testing.T) { func TestDecodeOneofNilWrapper(t *testing.T) {
wire := pack.Message{ wire := protopack.Message{
pack.Tag{111, pack.VarintType}, pack.Varint(1111), protopack.Tag{111, protopack.VarintType}, protopack.Varint(1111),
}.Marshal() }.Marshal()
m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)} m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
if err := proto.Unmarshal(wire, m); err != nil { if err := proto.Unmarshal(wire, m); err != nil {
@ -121,8 +121,8 @@ func TestDecodeEmptyBytes(t *testing.T) {
// but we take care to produce non-nil []bytes for zero-length // but we take care to produce non-nil []bytes for zero-length
// byte strings, so test for it. // byte strings, so test for it.
m := &testpb.TestAllTypes{} m := &testpb.TestAllTypes{}
b := pack.Message{ b := protopack.Message{
pack.Tag{45, pack.BytesType}, pack.Bytes(nil), protopack.Tag{45, protopack.BytesType}, protopack.Bytes(nil),
}.Marshal() }.Marshal()
if err := proto.Unmarshal(b, m); err != nil { if err := proto.Unmarshal(b, m); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -9,8 +9,8 @@ import (
"testing" "testing"
"google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protopack"
testpb "google.golang.org/protobuf/internal/testprotos/test" testpb "google.golang.org/protobuf/internal/testprotos/test"
test3pb "google.golang.org/protobuf/internal/testprotos/test3" test3pb "google.golang.org/protobuf/internal/testprotos/test3"
@ -416,15 +416,15 @@ func TestEqual(t *testing.T) {
// Unknown fields. // Unknown fields.
{ {
x: build(&testpb.TestAllTypes{}, unknown(pack.Message{ x: build(&testpb.TestAllTypes{}, unknown(protopack.Message{
pack.Tag{100000, pack.VarintType}, pack.Varint(1), protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
}.Marshal())), }.Marshal())),
y: build(&testpb.TestAllTypes{}, unknown(pack.Message{ y: build(&testpb.TestAllTypes{}, unknown(protopack.Message{
pack.Tag{100000, pack.VarintType}, pack.Varint(2), protopack.Tag{100000, protopack.VarintType}, protopack.Varint(2),
}.Marshal())), }.Marshal())),
}, { }, {
x: build(&testpb.TestAllTypes{}, unknown(pack.Message{ x: build(&testpb.TestAllTypes{}, unknown(protopack.Message{
pack.Tag{100000, pack.VarintType}, pack.Varint(1), protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
}.Marshal())), }.Marshal())),
y: &testpb.TestAllTypes{}, y: &testpb.TestAllTypes{},
}, },

View File

@ -13,11 +13,11 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/internal/protobuild" "google.golang.org/protobuf/internal/protobuild"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/testing/protocmp" "google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/testing/protopack"
"google.golang.org/protobuf/types/dynamicpb" "google.golang.org/protobuf/types/dynamicpb"
legacypb "google.golang.org/protobuf/internal/testprotos/legacy" legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
@ -511,19 +511,19 @@ var testMerges = []testMerge{{
}, { }, {
desc: "merge unknown fields", desc: "merge unknown fields",
dst: protobuild.Message{ dst: protobuild.Message{
protobuild.Unknown: pack.Message{ protobuild.Unknown: protopack.Message{
pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Svarint(-5), protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Svarint(-5),
}.Marshal(), }.Marshal(),
}, },
src: protobuild.Message{ src: protobuild.Message{
protobuild.Unknown: pack.Message{ protobuild.Unknown: protopack.Message{
pack.Tag{Number: 500000, Type: pack.VarintType}, pack.Svarint(-50), protopack.Tag{Number: 500000, Type: protopack.VarintType}, protopack.Svarint(-50),
}.Marshal(), }.Marshal(),
}, },
want: protobuild.Message{ want: protobuild.Message{
protobuild.Unknown: pack.Message{ protobuild.Unknown: protopack.Message{
pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Svarint(-5), protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Svarint(-5),
pack.Tag{Number: 500000, Type: pack.VarintType}, pack.Svarint(-50), protopack.Tag{Number: 500000, Type: protopack.VarintType}, protopack.Svarint(-50),
}.Marshal(), }.Marshal(),
}, },
}, { }, {
@ -786,8 +786,8 @@ func TestMergeRace(t *testing.T) {
}}, }},
func() *testpb.TestAllTypes { func() *testpb.TestAllTypes {
m := new(testpb.TestAllTypes) m := new(testpb.TestAllTypes)
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Svarint(-5), protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Svarint(-5),
}.Marshal()) }.Marshal())
return m return m
}(), }(),
@ -816,8 +816,8 @@ func TestMergeSelf(t *testing.T) {
A: proto.Int32(5), A: proto.Int32(5),
}, },
} }
got.ProtoReflect().SetUnknown(pack.Message{ got.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Svarint(-5), protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Svarint(-5),
}.Marshal()) }.Marshal())
proto.Merge(got, got) proto.Merge(got, got)
@ -833,9 +833,9 @@ func TestMergeSelf(t *testing.T) {
A: proto.Int32(5), A: proto.Int32(5),
}, },
} }
want.ProtoReflect().SetUnknown(pack.Message{ want.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Svarint(-5), protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Svarint(-5),
pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Svarint(-5), protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Svarint(-5),
}.Marshal()) }.Marshal())
if !proto.Equal(got, want) { if !proto.Equal(got, want) {

View File

@ -6,9 +6,9 @@ package proto_test
import ( import (
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protopack"
messagesetpb "google.golang.org/protobuf/internal/testprotos/messageset/messagesetpb" messagesetpb "google.golang.org/protobuf/internal/testprotos/messageset/messagesetpb"
msetextpb "google.golang.org/protobuf/internal/testprotos/messageset/msetextpb" msetextpb "google.golang.org/protobuf/internal/testprotos/messageset/msetextpb"
@ -31,14 +31,14 @@ var messageSetTestProtos = []testProto{
}) })
return m return m
}()}, }()},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1000), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -51,14 +51,14 @@ var messageSetTestProtos = []testProto{
}) })
return m return m
}()}, }()},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
pack.Tag{2, pack.VarintType}, pack.Varint(1000), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -70,34 +70,34 @@ var messageSetTestProtos = []testProto{
Ext1Field1: proto.Int32(10), Ext1Field1: proto.Int32(10),
}), }),
)}, )},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1000), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
// Unknown field // Unknown field
pack.Tag{4, pack.VarintType}, pack.Varint(30), protopack.Tag{4, protopack.VarintType}, protopack.Varint(30),
}.Marshal(), }.Marshal(),
}, },
{ {
desc: "MessageSet with unknown type_id", desc: "MessageSet with unknown type_id",
decodeTo: []proto.Message{build( decodeTo: []proto.Message{build(
&messagesetpb.MessageSet{}, &messagesetpb.MessageSet{},
unknown(pack.Message{ unknown(protopack.Message{
pack.Tag{999, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{999, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
}.Marshal()), }.Marshal()),
)}, )},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(999), protopack.Tag{2, protopack.VarintType}, protopack.Varint(999),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}.Marshal(), }.Marshal(),
}, },
{ {
@ -109,16 +109,16 @@ var messageSetTestProtos = []testProto{
Ext1Field2: proto.Int32(20), Ext1Field2: proto.Int32(20),
}), }),
)}, )},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1000), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{2, pack.VarintType}, pack.Varint(20), protopack.Tag{2, protopack.VarintType}, protopack.Varint(20),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}.Marshal(), }.Marshal(),
}, },
{ {
@ -133,28 +133,28 @@ var messageSetTestProtos = []testProto{
Ext2Field1: proto.Int32(30), Ext2Field1: proto.Int32(30),
}), }),
)}, )},
wire: pack.Message{ wire: protopack.Message{
// Ext1, field1 // Ext1, field1
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1000), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
// Ext2, field1 // Ext2, field1
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1001), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1001),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(30), protopack.Tag{1, protopack.VarintType}, protopack.Varint(30),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
// Ext2, field2 // Ext2, field2
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1000), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{2, pack.VarintType}, pack.Varint(20), protopack.Tag{2, protopack.VarintType}, protopack.Varint(20),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}.Marshal(), }.Marshal(),
}, },
{ {
@ -162,12 +162,12 @@ var messageSetTestProtos = []testProto{
decodeTo: []proto.Message{build( decodeTo: []proto.Message{build(
&messagesetpb.MessageSet{}, &messagesetpb.MessageSet{},
)}, )},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}.Marshal(), }.Marshal(),
}, },
{ {
@ -176,10 +176,10 @@ var messageSetTestProtos = []testProto{
&messagesetpb.MessageSet{}, &messagesetpb.MessageSet{},
extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{}), extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{}),
)}, )},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1000), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}.Marshal(), }.Marshal(),
}, },
{ {
@ -189,12 +189,12 @@ var messageSetTestProtos = []testProto{
proto.SetExtension(m.MessageSet, msetextpb.E_ExtLargeNumber_MessageSetExtension, &msetextpb.ExtLargeNumber{}) proto.SetExtension(m.MessageSet, msetextpb.E_ExtLargeNumber_MessageSetExtension, &msetextpb.ExtLargeNumber{})
return m return m
}()}, }()},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(protowire.MaxValidNumber + 1), protopack.Tag{2, protopack.VarintType}, protopack.Varint(protowire.MaxValidNumber + 1),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{}), protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -203,18 +203,18 @@ var messageSetTestProtos = []testProto{
decodeTo: []proto.Message{func() proto.Message { decodeTo: []proto.Message{func() proto.Message {
m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}} m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
m.MessageSet.ProtoReflect().SetUnknown( m.MessageSet.ProtoReflect().SetUnknown(
pack.Message{ protopack.Message{
pack.Tag{protowire.MaxValidNumber + 2, pack.BytesType}, pack.LengthPrefix{}, protopack.Tag{protowire.MaxValidNumber + 2, protopack.BytesType}, protopack.LengthPrefix{},
}.Marshal(), }.Marshal(),
) )
return m return m
}()}, }()},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(protowire.MaxValidNumber + 2), protopack.Tag{2, protopack.VarintType}, protopack.Varint(protowire.MaxValidNumber + 2),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{}), protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -227,15 +227,15 @@ var messageSetTestProtos = []testProto{
}) })
return m return m
}()}, }()},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1000), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(10), protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
}), }),
pack.Tag{4, pack.VarintType}, pack.Varint(0), protopack.Tag{4, protopack.VarintType}, protopack.Varint(0),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -249,14 +249,14 @@ var messageSetTestProtos = []testProto{
}) })
return m return m
}()}, }()},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1002), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1), protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
}), }),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -269,12 +269,12 @@ var messageSetTestProtos = []testProto{
proto.SetExtension(m.MessageSet, msetextpb.E_ExtRequired_MessageSetExtension, &msetextpb.ExtRequired{}) proto.SetExtension(m.MessageSet, msetextpb.E_ExtRequired_MessageSetExtension, &msetextpb.ExtRequired{})
return m return m
}()}, }()},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Varint(1002), protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{}), protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -286,12 +286,12 @@ var messageSetInvalidTestProtos = []testProto{
decodeTo: []proto.Message{ decodeTo: []proto.Message{
(*messagesetpb.MessageSetContainer)(nil), (*messagesetpb.MessageSetContainer)(nil),
}, },
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Uvarint(0), protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(0),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{}), protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -300,12 +300,12 @@ var messageSetInvalidTestProtos = []testProto{
decodeTo: []proto.Message{ decodeTo: []proto.Message{
(*messagesetpb.MessageSetContainer)(nil), (*messagesetpb.MessageSetContainer)(nil),
}, },
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.StartGroupType}, protopack.Tag{1, protopack.StartGroupType},
pack.Tag{2, pack.VarintType}, pack.Uvarint(0x80000000), protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(0x80000000),
pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{}), protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
pack.Tag{1, pack.EndGroupType}, protopack.Tag{1, protopack.EndGroupType},
}), }),
}.Marshal(), }.Marshal(),
}, },

View File

@ -8,13 +8,13 @@ import (
"reflect" "reflect"
"google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/internal/filedesc" "google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc" "google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoimpl" "google.golang.org/protobuf/runtime/protoimpl"
"google.golang.org/protobuf/testing/protopack"
"google.golang.org/protobuf/types/descriptorpb" "google.golang.org/protobuf/types/descriptorpb"
) )
@ -33,8 +33,8 @@ var noEnforceUTF8TestProtos = []testProto{
decodeTo: []proto.Message{&TestNoEnforceUTF8{ decodeTo: []proto.Message{&TestNoEnforceUTF8{
OptionalString: string("abc\xff"), OptionalString: string("abc\xff"),
}}, }},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.String("abc\xff"), protopack.Tag{1, protopack.BytesType}, protopack.String("abc\xff"),
}.Marshal(), }.Marshal(),
}, },
{ {
@ -42,8 +42,8 @@ var noEnforceUTF8TestProtos = []testProto{
decodeTo: []proto.Message{&TestNoEnforceUTF8{ decodeTo: []proto.Message{&TestNoEnforceUTF8{
OptionalBytes: []byte("abc\xff"), OptionalBytes: []byte("abc\xff"),
}}, }},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{2, pack.BytesType}, pack.String("abc\xff"), protopack.Tag{2, protopack.BytesType}, protopack.String("abc\xff"),
}.Marshal(), }.Marshal(),
}, },
{ {
@ -51,9 +51,9 @@ var noEnforceUTF8TestProtos = []testProto{
decodeTo: []proto.Message{&TestNoEnforceUTF8{ decodeTo: []proto.Message{&TestNoEnforceUTF8{
RepeatedString: []string{string("foo"), string("abc\xff")}, RepeatedString: []string{string("foo"), string("abc\xff")},
}}, }},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{3, pack.BytesType}, pack.String("foo"), protopack.Tag{3, protopack.BytesType}, protopack.String("foo"),
pack.Tag{3, pack.BytesType}, pack.String("abc\xff"), protopack.Tag{3, protopack.BytesType}, protopack.String("abc\xff"),
}.Marshal(), }.Marshal(),
}, },
{ {
@ -61,9 +61,9 @@ var noEnforceUTF8TestProtos = []testProto{
decodeTo: []proto.Message{&TestNoEnforceUTF8{ decodeTo: []proto.Message{&TestNoEnforceUTF8{
RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")}, RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
}}, }},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{4, pack.BytesType}, pack.String("foo"), protopack.Tag{4, protopack.BytesType}, protopack.String("foo"),
pack.Tag{4, pack.BytesType}, pack.String("abc\xff"), protopack.Tag{4, protopack.BytesType}, protopack.String("abc\xff"),
}.Marshal(), }.Marshal(),
}, },
{ {
@ -71,14 +71,14 @@ var noEnforceUTF8TestProtos = []testProto{
decodeTo: []proto.Message{ decodeTo: []proto.Message{
&TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}}, &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
}, },
wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(), wire: protopack.Message{protopack.Tag{5, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
}, },
{ {
desc: "invalid UTF-8 in oneof string field of Go bytes", desc: "invalid UTF-8 in oneof string field of Go bytes",
decodeTo: []proto.Message{ decodeTo: []proto.Message{
&TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}}, &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
}, },
wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(), wire: protopack.Message{protopack.Tag{6, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
}, },
} }

File diff suppressed because it is too large Load Diff

View File

@ -7,10 +7,10 @@ package proto_test
import ( import (
"testing" "testing"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/internal/protobuild" "google.golang.org/protobuf/internal/protobuild"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protopack"
testpb "google.golang.org/protobuf/internal/testprotos/test" testpb "google.golang.org/protobuf/internal/testprotos/test"
weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1" weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
@ -36,20 +36,20 @@ var testWeakValidMessages = []testProto{
m.SetWeakMessage1(&weakpb.WeakImportMessage1{ m.SetWeakMessage1(&weakpb.WeakImportMessage1{
A: proto.Int32(1000), A: proto.Int32(1000),
}) })
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2000), protopack.Tag{1, protopack.VarintType}, protopack.Varint(2000),
}), }),
}.Marshal()) }.Marshal())
return m return m
}(), }(),
}, },
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1000), protopack.Tag{1, protopack.VarintType}, protopack.Varint(1000),
}), }),
pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2000), protopack.Tag{1, protopack.VarintType}, protopack.Varint(2000),
}), }),
}.Marshal(), }.Marshal(),
}, },
@ -59,9 +59,9 @@ var testWeakInvalidMessages = []testProto{
{ {
desc: "invalid field number 0 in weak message", desc: "invalid field number 0 in weak message",
decodeTo: []proto.Message{(*testpb.TestWeak)(nil)}, decodeTo: []proto.Message{(*testpb.TestWeak)(nil)},
wire: pack.Message{ wire: protopack.Message{
pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{0, pack.VarintType}, pack.Varint(1000), protopack.Tag{0, protopack.VarintType}, protopack.Varint(1000),
}), }),
}.Marshal(), }.Marshal(),
}, },

View File

@ -12,9 +12,9 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/testing/protopack"
"google.golang.org/protobuf/types/dynamicpb" "google.golang.org/protobuf/types/dynamicpb"
testpb "google.golang.org/protobuf/internal/testprotos/test" testpb "google.golang.org/protobuf/internal/testprotos/test"
@ -208,8 +208,8 @@ func TestEqual(t *testing.T) {
}}...) }}...)
// Test IgnoreUnknown. // Test IgnoreUnknown.
raw := pack.Message{ raw := protopack.Message{
pack.Tag{1, pack.BytesType}, pack.String("Hello, goodbye!"), protopack.Tag{1, protopack.BytesType}, protopack.String("Hello, goodbye!"),
}.Marshal() }.Marshal()
tests = append(tests, []test{{ tests = append(tests, []test{{
x: apply(&testpb.TestAllTypes{OptionalSint64: proto.Int64(5)}, setUnknown{raw}), x: apply(&testpb.TestAllTypes{OptionalSint64: proto.Int64(5)}, setUnknown{raw}),

View File

@ -10,9 +10,9 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/internal/detrand" "google.golang.org/protobuf/internal/detrand"
"google.golang.org/protobuf/internal/encoding/pack"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/testing/protopack"
testpb "google.golang.org/protobuf/internal/testprotos/test" testpb "google.golang.org/protobuf/internal/testprotos/test"
) )
@ -210,51 +210,51 @@ func TestTransform(t *testing.T) {
}, { }, {
in: func() proto.Message { in: func() proto.Message {
m := &testpb.TestAllTypes{} m := &testpb.TestAllTypes{}
m.ProtoReflect().SetUnknown(pack.Message{ m.ProtoReflect().SetUnknown(protopack.Message{
pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Uvarint(100), protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Uvarint(100),
pack.Tag{Number: 50001, Type: pack.Fixed32Type}, pack.Uint32(200), protopack.Tag{Number: 50001, Type: protopack.Fixed32Type}, protopack.Uint32(200),
pack.Tag{Number: 50002, Type: pack.Fixed64Type}, pack.Uint64(300), protopack.Tag{Number: 50002, Type: protopack.Fixed64Type}, protopack.Uint64(300),
pack.Tag{Number: 50003, Type: pack.BytesType}, pack.String("hello"), protopack.Tag{Number: 50003, Type: protopack.BytesType}, protopack.String("hello"),
pack.Message{ protopack.Message{
pack.Tag{Number: 50004, Type: pack.StartGroupType}, protopack.Tag{Number: 50004, Type: protopack.StartGroupType},
pack.Tag{Number: 1, Type: pack.VarintType}, pack.Uvarint(100), protopack.Tag{Number: 1, Type: protopack.VarintType}, protopack.Uvarint(100),
pack.Tag{Number: 1, Type: pack.Fixed32Type}, pack.Uint32(200), protopack.Tag{Number: 1, Type: protopack.Fixed32Type}, protopack.Uint32(200),
pack.Tag{Number: 1, Type: pack.Fixed64Type}, pack.Uint64(300), protopack.Tag{Number: 1, Type: protopack.Fixed64Type}, protopack.Uint64(300),
pack.Tag{Number: 1, Type: pack.BytesType}, pack.String("hello"), protopack.Tag{Number: 1, Type: protopack.BytesType}, protopack.String("hello"),
pack.Message{ protopack.Message{
pack.Tag{Number: 1, Type: pack.StartGroupType}, protopack.Tag{Number: 1, Type: protopack.StartGroupType},
pack.Tag{Number: 1, Type: pack.VarintType}, pack.Uvarint(100), protopack.Tag{Number: 1, Type: protopack.VarintType}, protopack.Uvarint(100),
pack.Tag{Number: 1, Type: pack.Fixed32Type}, pack.Uint32(200), protopack.Tag{Number: 1, Type: protopack.Fixed32Type}, protopack.Uint32(200),
pack.Tag{Number: 1, Type: pack.Fixed64Type}, pack.Uint64(300), protopack.Tag{Number: 1, Type: protopack.Fixed64Type}, protopack.Uint64(300),
pack.Tag{Number: 1, Type: pack.BytesType}, pack.String("hello"), protopack.Tag{Number: 1, Type: protopack.BytesType}, protopack.String("hello"),
pack.Tag{Number: 1, Type: pack.EndGroupType}, protopack.Tag{Number: 1, Type: protopack.EndGroupType},
}, },
pack.Tag{Number: 50004, Type: pack.EndGroupType}, protopack.Tag{Number: 50004, Type: protopack.EndGroupType},
}, },
}.Marshal()) }.Marshal())
return m return m
}(), }(),
want: Message{ want: Message{
messageTypeKey: messageTypeOf(&testpb.TestAllTypes{}), messageTypeKey: messageTypeOf(&testpb.TestAllTypes{}),
"50000": protoreflect.RawFields(pack.Message{pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Uvarint(100)}.Marshal()), "50000": protoreflect.RawFields(protopack.Message{protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Uvarint(100)}.Marshal()),
"50001": protoreflect.RawFields(pack.Message{pack.Tag{Number: 50001, Type: pack.Fixed32Type}, pack.Uint32(200)}.Marshal()), "50001": protoreflect.RawFields(protopack.Message{protopack.Tag{Number: 50001, Type: protopack.Fixed32Type}, protopack.Uint32(200)}.Marshal()),
"50002": protoreflect.RawFields(pack.Message{pack.Tag{Number: 50002, Type: pack.Fixed64Type}, pack.Uint64(300)}.Marshal()), "50002": protoreflect.RawFields(protopack.Message{protopack.Tag{Number: 50002, Type: protopack.Fixed64Type}, protopack.Uint64(300)}.Marshal()),
"50003": protoreflect.RawFields(pack.Message{pack.Tag{Number: 50003, Type: pack.BytesType}, pack.String("hello")}.Marshal()), "50003": protoreflect.RawFields(protopack.Message{protopack.Tag{Number: 50003, Type: protopack.BytesType}, protopack.String("hello")}.Marshal()),
"50004": protoreflect.RawFields(pack.Message{ "50004": protoreflect.RawFields(protopack.Message{
pack.Tag{Number: 50004, Type: pack.StartGroupType}, protopack.Tag{Number: 50004, Type: protopack.StartGroupType},
pack.Tag{Number: 1, Type: pack.VarintType}, pack.Uvarint(100), protopack.Tag{Number: 1, Type: protopack.VarintType}, protopack.Uvarint(100),
pack.Tag{Number: 1, Type: pack.Fixed32Type}, pack.Uint32(200), protopack.Tag{Number: 1, Type: protopack.Fixed32Type}, protopack.Uint32(200),
pack.Tag{Number: 1, Type: pack.Fixed64Type}, pack.Uint64(300), protopack.Tag{Number: 1, Type: protopack.Fixed64Type}, protopack.Uint64(300),
pack.Tag{Number: 1, Type: pack.BytesType}, pack.String("hello"), protopack.Tag{Number: 1, Type: protopack.BytesType}, protopack.String("hello"),
pack.Message{ protopack.Message{
pack.Tag{Number: 1, Type: pack.StartGroupType}, protopack.Tag{Number: 1, Type: protopack.StartGroupType},
pack.Tag{Number: 1, Type: pack.VarintType}, pack.Uvarint(100), protopack.Tag{Number: 1, Type: protopack.VarintType}, protopack.Uvarint(100),
pack.Tag{Number: 1, Type: pack.Fixed32Type}, pack.Uint32(200), protopack.Tag{Number: 1, Type: protopack.Fixed32Type}, protopack.Uint32(200),
pack.Tag{Number: 1, Type: pack.Fixed64Type}, pack.Uint64(300), protopack.Tag{Number: 1, Type: protopack.Fixed64Type}, protopack.Uint64(300),
pack.Tag{Number: 1, Type: pack.BytesType}, pack.String("hello"), protopack.Tag{Number: 1, Type: protopack.BytesType}, protopack.String("hello"),
pack.Tag{Number: 1, Type: pack.EndGroupType}, protopack.Tag{Number: 1, Type: protopack.EndGroupType},
}, },
pack.Tag{Number: 50004, Type: pack.EndGroupType}, protopack.Tag{Number: 50004, Type: protopack.EndGroupType},
}.Marshal()), }.Marshal()),
}, },
wantString: `{50000:100, 50001:200, 50002:300, 50003:"hello", 50004:{1:[100, 200, 300, "hello", {1:[100, 200, 300, "hello"]}]}}`, wantString: `{50000:100, 50001:200, 50002:300, 50003:"hello", 50004:{1:[100, 200, 300, "hello", {1:[100, 200, 300, "hello"]}]}}`,

View File

@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package pack enables manual encoding and decoding of protobuf wire data. // Package protopack enables manual encoding and decoding of protobuf wire data.
// //
// This package is intended for use in debugging and/or creation of test data. // This package is intended for use in debugging and/or creation of test data.
// Proper usage of this package requires knowledge of the wire format. // Proper usage of this package requires knowledge of the wire format.
// //
// See https://developers.google.com/protocol-buffers/docs/encoding. // See https://developers.google.com/protocol-buffers/docs/encoding.
package pack package protopack
import ( import (
"fmt" "fmt"
@ -25,10 +25,10 @@ import (
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
) )
// Number is the field number; aliased from the wire package for convenience. // Number is the field number; aliased from the protowire package for convenience.
type Number = protowire.Number type Number = protowire.Number
// Number type constants; copied from the wire package for convenience. // Number type constants; copied from the protowire package for convenience.
const ( const (
MinValidNumber Number = protowire.MinValidNumber MinValidNumber Number = protowire.MinValidNumber
FirstReservedNumber Number = protowire.FirstReservedNumber FirstReservedNumber Number = protowire.FirstReservedNumber
@ -36,10 +36,10 @@ const (
MaxValidNumber Number = protowire.MaxValidNumber MaxValidNumber Number = protowire.MaxValidNumber
) )
// Type is the wire type; aliased from the wire package for convenience. // Type is the wire type; aliased from the protowire package for convenience.
type Type = protowire.Type type Type = protowire.Type
// Wire type constants; copied from the wire package for convenience. // Wire type constants; copied from the protowire package for convenience.
const ( const (
VarintType Type = protowire.VarintType VarintType Type = protowire.VarintType
Fixed32Type Type = protowire.Fixed32Type Fixed32Type Type = protowire.Fixed32Type

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package pack package protopack
import ( import (
"bytes" "bytes"
@ -81,11 +81,11 @@ func TestPack(t *testing.T) {
Tag{1, VarintType}, Denormalized{5, Uvarint(2)}, Tag{1, VarintType}, Denormalized{5, Uvarint(2)},
Tag{1, BytesType}, LengthPrefix{Bool(true), Bool(false), Uvarint(2), Denormalized{5, Uvarint(2)}}, Tag{1, BytesType}, LengthPrefix{Bool(true), Bool(false), Uvarint(2), Denormalized{5, Uvarint(2)}},
}, },
wantOutSource: `pack.Message{ wantOutSource: `protopack.Message{
pack.Tag{1, pack.VarintType}, pack.Bool(false), protopack.Tag{1, protopack.VarintType}, protopack.Bool(false),
pack.Denormalized{+5, pack.Tag{1, pack.VarintType}}, pack.Uvarint(2), protopack.Denormalized{+5, protopack.Tag{1, protopack.VarintType}}, protopack.Uvarint(2),
pack.Tag{1, pack.VarintType}, pack.Denormalized{+5, pack.Uvarint(2)}, protopack.Tag{1, protopack.VarintType}, protopack.Denormalized{+5, protopack.Uvarint(2)},
pack.Tag{1, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Uvarint(2), pack.Denormalized{+5, pack.Uvarint(2)}}, protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix{protopack.Bool(true), protopack.Bool(false), protopack.Uvarint(2), protopack.Denormalized{+5, protopack.Uvarint(2)}},
}`, }`,
}, { }, {
raw: dhex("100010828080808000121980808080808080808001ffffffffffffffff7f828080808000"), raw: dhex("100010828080808000121980808080808080808001ffffffffffffffff7f828080808000"),
@ -114,10 +114,10 @@ func TestPack(t *testing.T) {
Tag{4, VarintType}, Denormalized{5, Uvarint(+1)}, Tag{4, VarintType}, Denormalized{5, Uvarint(+1)},
Tag{4, BytesType}, LengthPrefix{Uvarint(0), Uvarint(math.MaxUint64), Denormalized{5, Uvarint(+1)}}, Tag{4, BytesType}, LengthPrefix{Uvarint(0), Uvarint(math.MaxUint64), Denormalized{5, Uvarint(+1)}},
}, },
wantOutSource: `pack.Message{ wantOutSource: `protopack.Message{
pack.Tag{4, pack.VarintType}, pack.Uvarint(1), protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(1),
pack.Tag{4, pack.VarintType}, pack.Denormalized{+5, pack.Uvarint(1)}, protopack.Tag{4, protopack.VarintType}, protopack.Denormalized{+5, protopack.Uvarint(1)},
pack.Tag{4, pack.BytesType}, pack.LengthPrefix{pack.Uvarint(0), pack.Uvarint(18446744073709551615), pack.Denormalized{+5, pack.Uvarint(1)}}, protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix{protopack.Uvarint(0), protopack.Uvarint(18446744073709551615), protopack.Denormalized{+5, protopack.Uvarint(1)}},
}`, }`,
}, { }, {
raw: dhex("2d010000002a0800000000ffffffff"), raw: dhex("2d010000002a0800000000ffffffff"),
@ -142,9 +142,9 @@ func TestPack(t *testing.T) {
Tag{7, Fixed32Type}, Float32(math.Pi), Tag{7, Fixed32Type}, Float32(math.Pi),
Tag{7, BytesType}, LengthPrefix{Float32(math.SmallestNonzeroFloat32), Float32(math.MaxFloat32), Float32(math.Inf(+1)), Float32(math.Inf(-1))}, Tag{7, BytesType}, LengthPrefix{Float32(math.SmallestNonzeroFloat32), Float32(math.MaxFloat32), Float32(math.Inf(+1)), Float32(math.Inf(-1))},
}, },
wantOutSource: `pack.Message{ wantOutSource: `protopack.Message{
pack.Tag{7, pack.Fixed32Type}, pack.Float32(3.1415927), protopack.Tag{7, protopack.Fixed32Type}, protopack.Float32(3.1415927),
pack.Tag{7, pack.BytesType}, pack.LengthPrefix{pack.Float32(1e-45), pack.Float32(3.4028235e+38), pack.Float32(math.Inf(+1)), pack.Float32(math.Inf(-1))}, protopack.Tag{7, protopack.BytesType}, protopack.LengthPrefix{protopack.Float32(1e-45), protopack.Float32(3.4028235e+38), protopack.Float32(math.Inf(+1)), protopack.Float32(math.Inf(-1))},
}`, }`,
}, { }, {
raw: dhex("41010000000000000042100000000000000000ffffffffffffffff"), raw: dhex("41010000000000000042100000000000000000ffffffffffffffff"),
@ -201,14 +201,14 @@ func TestPack(t *testing.T) {
Tag{100, StartGroupType}, Tag{100, EndGroupType}, Tag{100, StartGroupType}, Tag{100, EndGroupType},
}), }),
}, },
wantOutSource: `pack.Message{ wantOutSource: `protopack.Message{
pack.Tag{13, pack.BytesType}, pack.LengthPrefix(pack.Message{ protopack.Tag{13, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
pack.Tag{100, pack.VarintType}, pack.Uvarint(18446744073709551615), protopack.Tag{100, protopack.VarintType}, protopack.Uvarint(18446744073709551615),
pack.Tag{100, pack.Fixed32Type}, pack.Uint32(4294967295), protopack.Tag{100, protopack.Fixed32Type}, protopack.Uint32(4294967295),
pack.Tag{100, pack.Fixed64Type}, pack.Uint64(18446744073709551615), protopack.Tag{100, protopack.Fixed64Type}, protopack.Uint64(18446744073709551615),
pack.Tag{100, pack.BytesType}, pack.Bytes("bytes"), protopack.Tag{100, protopack.BytesType}, protopack.Bytes("bytes"),
pack.Tag{100, pack.StartGroupType}, protopack.Tag{100, protopack.StartGroupType},
pack.Tag{100, pack.EndGroupType}, protopack.Tag{100, protopack.EndGroupType},
}), }),
}`, }`,
}, { }, {
@ -269,16 +269,16 @@ func TestPack(t *testing.T) {
func() uint32 { return 0x7fe5d008 }(), func() uint32 { return 0x7fe5d008 }(),
)), )),
}, },
wantOutSource: `pack.Message{ wantOutSource: `protopack.Message{
pack.Tag{7, pack.Fixed32Type}, pack.Float32(math.Float32frombits(0x7fe5d008)), protopack.Tag{7, protopack.Fixed32Type}, protopack.Float32(math.Float32frombits(0x7fe5d008)),
}`, }`,
}, { }, {
raw: dhex("51a8d65110771bf97f"), raw: dhex("51a8d65110771bf97f"),
msg: Message{ msg: Message{
Tag{10, Fixed64Type}, Float64(math.Float64frombits(0x7ff91b771051d6a8)), Tag{10, Fixed64Type}, Float64(math.Float64frombits(0x7ff91b771051d6a8)),
}, },
wantOutSource: `pack.Message{ wantOutSource: `protopack.Message{
pack.Tag{10, pack.Fixed64Type}, pack.Float64(math.Float64frombits(0x7ff91b771051d6a8)), protopack.Tag{10, protopack.Fixed64Type}, protopack.Float64(math.Float64frombits(0x7ff91b771051d6a8)),
}`, }`,
}, { }, {
raw: dhex("ab2c14481ab3e9a76d937fb4dd5e6c616ef311f62b7fe888785fca5609ffe81c1064e50dd7a9edb408d317e2891c0d54c719446938d41ab0ccf8e61dc28b0ebb"), raw: dhex("ab2c14481ab3e9a76d937fb4dd5e6c616ef311f62b7fe888785fca5609ffe81c1064e50dd7a9edb408d317e2891c0d54c719446938d41ab0ccf8e61dc28b0ebb"),