proto: wire decoding support

Add proto.Unmarshal.

Test cases all produce identical results to the v1 unmarshaller.

Change-Id: I42259266018a14e88a650c5d83a043cb17a3a15d
Reviewed-on: https://go-review.googlesource.com/c/153918
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
This commit is contained in:
Damien Neil 2018-12-07 14:38:17 -08:00
parent 2c6f09887d
commit ba23aa55b2
8 changed files with 4561 additions and 0 deletions

View File

@ -26,6 +26,7 @@ var run = flag.Bool("execute", false, "Write generated files to destination.")
func main() {
flag.Parse()
chdirRoot()
writeSource("proto/decode_gen.go", generateProtoDecode())
writeSource("reflect/prototype/protofile_list_gen.go", generateListTypes())
}
@ -198,8 +199,10 @@ func writeSource(file, src string) {
var imports []string
for _, pkg := range []string{
"fmt",
"math",
"sync",
"",
"github.com/golang/protobuf/v2/internal/encoding/wire",
"github.com/golang/protobuf/v2/internal/pragma",
"github.com/golang/protobuf/v2/internal/typefmt",
"github.com/golang/protobuf/v2/reflect/protoreflect",

View File

@ -0,0 +1,210 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "text/template"
type WireType string
const (
WireVarint WireType = "Varint"
WireFixed32 WireType = "Fixed32"
WireFixed64 WireType = "Fixed64"
WireBytes WireType = "Bytes"
WireGroup WireType = "Group"
)
func (w WireType) Expr() Expr {
if w == WireGroup {
return "wire.StartGroupType"
}
return "wire." + Expr(w) + "Type"
}
func (w WireType) Packable() bool {
return w == WireVarint || w == WireFixed32 || w == WireFixed64
}
type ProtoKind struct {
Name string
WireType WireType
ToValue Expr
}
func (k ProtoKind) Expr() Expr {
return "protoreflect." + Expr(k.Name) + "Kind"
}
var ProtoKinds = []ProtoKind{
{
Name: "Bool",
WireType: WireVarint,
ToValue: "wire.DecodeBool(v)",
},
{
Name: "Enum",
WireType: WireVarint,
ToValue: "protoreflect.EnumNumber(v)",
},
{
Name: "Int32",
WireType: WireVarint,
ToValue: "int32(v)",
},
{
Name: "Sint32",
WireType: WireVarint,
ToValue: "int32(wire.DecodeZigZag(v & math.MaxUint32))",
},
{
Name: "Uint32",
WireType: WireVarint,
ToValue: "uint32(v)",
},
{
Name: "Int64",
WireType: WireVarint,
ToValue: "int64(v)",
},
{
Name: "Sint64",
WireType: WireVarint,
ToValue: "wire.DecodeZigZag(v)",
},
{
Name: "Uint64",
WireType: WireVarint,
ToValue: "v",
},
{
Name: "Sfixed32",
WireType: WireFixed32,
ToValue: "int32(v)",
},
{
Name: "Fixed32",
WireType: WireFixed32,
ToValue: "uint32(v)",
},
{
Name: "Float",
WireType: WireFixed32,
ToValue: "math.Float32frombits(uint32(v))",
},
{
Name: "Sfixed64",
WireType: WireFixed64,
ToValue: "int64(v)",
},
{
Name: "Fixed64",
WireType: WireFixed64,
ToValue: "v",
},
{
Name: "Double",
WireType: WireFixed64,
ToValue: "math.Float64frombits(v)",
},
{
Name: "String",
WireType: WireBytes,
ToValue: "string(v)",
},
{
Name: "Bytes",
WireType: WireBytes,
ToValue: "append(([]byte)(nil), v...)",
},
{
Name: "Message",
WireType: WireBytes,
ToValue: "v",
},
{
Name: "Group",
WireType: WireGroup,
ToValue: "v",
},
}
func generateProtoDecode() string {
return mustExecute(protoDecodeTemplate, ProtoKinds)
}
var protoDecodeTemplate = template.Must(template.New("").Parse(`
// unmarshalScalar decodes a value of the given kind.
//
// Message values are decoded into a []byte which aliases the input data.
func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp wire.Type, num wire.Number, kind protoreflect.Kind) (val protoreflect.Value, n int, err error) {
switch kind {
{{- range .}}
case {{.Expr}}:
if wtyp != {{.WireType.Expr}} {
return val, 0, errUnknown
}
{{if (eq .WireType "Group") -}}
v, n := wire.ConsumeGroup(num, b)
{{- else -}}
v, n := wire.Consume{{.WireType}}(b)
{{- end}}
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf({{.ToValue}}), n, nil
{{- end}}
default:
return val, 0, errUnknown
}
}
func (o UnmarshalOptions) unmarshalList(b []byte, wtyp wire.Type, num wire.Number, list protoreflect.List, kind protoreflect.Kind) (n int, err error) {
switch kind {
{{- range .}}
case {{.Expr}}:
{{- if .WireType.Packable}}
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.Consume{{.WireType}}(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf({{.ToValue}}))
}
return n, nil
}
{{- end}}
if wtyp != {{.WireType.Expr}} {
return 0, errUnknown
}
{{if (eq .WireType "Group") -}}
v, n := wire.ConsumeGroup(num, b)
{{- else -}}
v, n := wire.Consume{{.WireType}}(b)
{{- end}}
if n < 0 {
return 0, wire.ParseError(n)
}
{{if or (eq .Name "Message") (eq .Name "Group") -}}
m := list.NewMessage().ProtoReflect()
if err := o.unmarshalMessage(v, m); err != nil {
return 0, err
}
list.Append(protoreflect.ValueOf(m))
{{- else -}}
list.Append(protoreflect.ValueOf({{.ToValue}}))
{{- end}}
return n, nil
{{- end}}
default:
return 0, errUnknown
}
}
`))

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,147 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
syntax = "proto2";
package goproto.proto.test;
option go_package = "github.com/golang/protobuf/v2/proto/testpb";
message TestAllTypes {
message NestedMessage {
optional int32 a = 1;
optional TestAllTypes corecursive = 2;
}
enum NestedEnum {
FOO = 0;
BAR = 1;
BAZ = 2;
NEG = -1; // Intentionally negative.
}
optional int32 optional_int32 = 1;
optional int64 optional_int64 = 2;
optional uint32 optional_uint32 = 3;
optional uint64 optional_uint64 = 4;
optional sint32 optional_sint32 = 5;
optional sint64 optional_sint64 = 6;
optional fixed32 optional_fixed32 = 7;
optional fixed64 optional_fixed64 = 8;
optional sfixed32 optional_sfixed32 = 9;
optional sfixed64 optional_sfixed64 = 10;
optional float optional_float = 11;
optional double optional_double = 12;
optional bool optional_bool = 13;
optional string optional_string = 14;
optional bytes optional_bytes = 15;
optional group OptionalGroup = 16 {
optional int32 a = 17;
}
optional NestedMessage optional_nested_message = 18;
optional NestedEnum optional_nested_enum = 21;
repeated int32 repeated_int32 = 31;
repeated int64 repeated_int64 = 32;
repeated uint32 repeated_uint32 = 33;
repeated uint64 repeated_uint64 = 34;
repeated sint32 repeated_sint32 = 35;
repeated sint64 repeated_sint64 = 36;
repeated fixed32 repeated_fixed32 = 37;
repeated fixed64 repeated_fixed64 = 38;
repeated sfixed32 repeated_sfixed32 = 39;
repeated sfixed64 repeated_sfixed64 = 40;
repeated float repeated_float = 41;
repeated double repeated_double = 42;
repeated bool repeated_bool = 43;
repeated string repeated_string = 44;
repeated bytes repeated_bytes = 45;
repeated group RepeatedGroup = 46 {
optional int32 a = 47;
}
repeated NestedMessage repeated_nested_message = 48;
repeated NestedEnum repeated_nested_enum = 51;
map < int32, int32> map_int32_int32 = 56;
map < int64, int64> map_int64_int64 = 57;
map < uint32, uint32> map_uint32_uint32 = 58;
map < uint64, uint64> map_uint64_uint64 = 59;
map < sint32, sint32> map_sint32_sint32 = 60;
map < sint64, sint64> map_sint64_sint64 = 61;
map < fixed32, fixed32> map_fixed32_fixed32 = 62;
map < fixed64, fixed64> map_fixed64_fixed64 = 63;
map <sfixed32, sfixed32> map_sfixed32_sfixed32 = 64;
map <sfixed64, sfixed64> map_sfixed64_sfixed64 = 65;
map < int32, float> map_int32_float = 66;
map < int32, double> map_int32_double = 67;
map < bool, bool> map_bool_bool = 68;
map < string, string> map_string_string = 69;
map < string, bytes> map_string_bytes = 70;
map < string, NestedMessage> map_string_nested_message = 71;
map < string, NestedEnum> map_string_nested_enum = 73;
oneof oneof_field {
uint32 oneof_uint32 = 111;
NestedMessage oneof_nested_message = 112;
string oneof_string = 113;
bytes oneof_bytes = 114;
bool oneof_bool = 115;
uint64 oneof_uint64 = 116;
float oneof_float = 117;
double oneof_double = 118;
NestedEnum oneof_enum = 119;
}
}
message TestAllExtensions {
extensions 1 to max;
}
extend TestAllExtensions {
optional int32 optional_int32_extension = 1;
optional int64 optional_int64_extension = 2;
optional uint32 optional_uint32_extension = 3;
optional uint64 optional_uint64_extension = 4;
optional sint32 optional_sint32_extension = 5;
optional sint64 optional_sint64_extension = 6;
optional fixed32 optional_fixed32_extension = 7;
optional fixed64 optional_fixed64_extension = 8;
optional sfixed32 optional_sfixed32_extension = 9;
optional sfixed64 optional_sfixed64_extension = 10;
optional float optional_float_extension = 11;
optional double optional_double_extension = 12;
optional bool optional_bool_extension = 13;
optional string optional_string_extension = 14;
optional bytes optional_bytes_extension = 15;
optional group OptionalGroup_extension = 16 {
optional int32 a = 17;
}
optional TestAllTypes.NestedMessage optional_nested_message_extension = 18;
optional TestAllTypes.NestedEnum optional_nested_enum_extension = 21;
repeated int32 repeated_int32_extension = 31;
repeated int64 repeated_int64_extension = 32;
repeated uint32 repeated_uint32_extension = 33;
repeated uint64 repeated_uint64_extension = 34;
repeated sint32 repeated_sint32_extension = 35;
repeated sint64 repeated_sint64_extension = 36;
repeated fixed32 repeated_fixed32_extension = 37;
repeated fixed64 repeated_fixed64_extension = 38;
repeated sfixed32 repeated_sfixed32_extension = 39;
repeated sfixed64 repeated_sfixed64_extension = 40;
repeated float repeated_float_extension = 41;
repeated double repeated_double_extension = 42;
repeated bool repeated_bool_extension = 43;
repeated string repeated_string_extension = 44;
repeated bytes repeated_bytes_extension = 45;
repeated group RepeatedGroup_extension = 46 {
optional int32 a = 47;
}
repeated TestAllTypes.NestedMessage repeated_nested_message_extension = 48;
repeated TestAllTypes.NestedEnum repeated_nested_enum_extension = 51;
}

191
proto/decode.go Normal file
View File

@ -0,0 +1,191 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style.
// license that can be found in the LICENSE file.
package proto
import (
"errors"
"github.com/golang/protobuf/v2/internal/encoding/wire"
"github.com/golang/protobuf/v2/internal/pragma"
"github.com/golang/protobuf/v2/reflect/protoreflect"
)
// UnmarshalOptions configures the unmarshaler.
//
// Example usage:
// err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
type UnmarshalOptions struct {
// If DiscardUnknown is set, unknown fields are ignored.
DiscardUnknown bool
pragma.NoUnkeyedLiterals
}
// Unmarshal parses the wire-format message in b and places the result in m.
func Unmarshal(b []byte, m Message) error {
return UnmarshalOptions{}.Unmarshal(b, m)
}
// Unmarshal parses the wire-format message in b and places the result in m.
func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
// TODO: Reset m?
return o.unmarshalMessage(b, m.ProtoReflect())
}
func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
messageType := m.Type()
fieldTypes := messageType.Fields()
knownFields := m.KnownFields()
unknownFields := m.UnknownFields()
for len(b) > 0 {
// Parse the tag (field number and wire type).
num, wtyp, tagLen := wire.ConsumeTag(b)
if tagLen < 0 {
return wire.ParseError(tagLen)
}
// Parse the field value.
fieldType := fieldTypes.ByNumber(num)
var err error
var valLen int
switch {
case fieldType == nil:
err = errUnknown
case fieldType.Cardinality() != protoreflect.Repeated:
valLen, err = o.unmarshalScalarField(b[tagLen:], wtyp, num, knownFields, fieldType)
case !fieldType.IsMap():
valLen, err = o.unmarshalList(b[tagLen:], wtyp, num, knownFields.Get(num).List(), fieldType.Kind())
default:
valLen, err = o.unmarshalMap(b[tagLen:], wtyp, num, knownFields.Get(num).Map(), fieldType)
}
if err == errUnknown {
valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
if valLen < 0 {
return wire.ParseError(valLen)
}
unknownFields.Set(num, append(unknownFields.Get(num), b[:tagLen+valLen]...))
} else if err != nil {
return err
}
b = b[tagLen+valLen:]
}
// TODO: required field checks
return nil
}
func (o UnmarshalOptions) unmarshalScalarField(b []byte, wtyp wire.Type, num wire.Number, knownFields protoreflect.KnownFields, field protoreflect.FieldDescriptor) (n int, err error) {
v, n, err := o.unmarshalScalar(b, wtyp, num, field.Kind())
if err != nil {
return 0, err
}
switch field.Kind() {
case protoreflect.GroupKind, protoreflect.MessageKind:
// Messages are merged with any existing message value,
// unless the message is part of a oneof.
//
// TODO: C++ merges into oneofs, while v1 does not.
// Evaluate which behavior to pick.
var m protoreflect.Message
if knownFields.Has(num) && field.OneofType() == nil {
m = knownFields.Get(num).Message()
} else {
m = knownFields.NewMessage(num).ProtoReflect()
knownFields.Set(num, protoreflect.ValueOf(m))
}
if err := o.unmarshalMessage(v.Bytes(), m); err != nil {
return 0, err
}
default:
// Non-message scalars replace the previous value.
knownFields.Set(num, v)
}
return n, nil
}
func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, num wire.Number, mapv protoreflect.Map, field protoreflect.FieldDescriptor) (n int, err error) {
if wtyp != wire.BytesType {
return 0, errUnknown
}
b, n = wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
var (
keyField = field.MessageType().Fields().ByNumber(1)
valField = field.MessageType().Fields().ByNumber(2)
key protoreflect.Value
val protoreflect.Value
haveKey bool
haveVal bool
)
switch valField.Kind() {
case protoreflect.GroupKind, protoreflect.MessageKind:
val = protoreflect.ValueOf(mapv.NewMessage().ProtoReflect())
}
// Map entries are represented as a two-element message with fields
// containing the key and value.
for len(b) > 0 {
num, wtyp, n := wire.ConsumeTag(b)
if n < 0 {
return 0, wire.ParseError(n)
}
b = b[n:]
err = errUnknown
switch num {
case 1:
key, n, err = o.unmarshalScalar(b, wtyp, num, keyField.Kind())
if err != nil {
break
}
haveKey = true
case 2:
var v protoreflect.Value
v, n, err = o.unmarshalScalar(b, wtyp, num, valField.Kind())
if err != nil {
break
}
switch valField.Kind() {
case protoreflect.GroupKind, protoreflect.MessageKind:
if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
return 0, err
}
default:
val = v
}
haveVal = true
}
if err == errUnknown {
n = wire.ConsumeFieldValue(num, wtyp, b)
if n < 0 {
return 0, wire.ParseError(n)
}
} else if err != nil {
return 0, err
}
b = b[n:]
}
// Every map entry should have entries for key and value, but this is not strictly required.
if !haveKey {
key = keyField.Default()
}
if !haveVal {
switch valField.Kind() {
case protoreflect.GroupKind, protoreflect.MessageKind:
// Trigger required field checks by unmarshaling an empty message.
if err := o.unmarshalMessage(nil, val.Message()); err != nil {
return 0, err
}
default:
val = valField.Default()
}
}
mapv.Set(key.MapKey(), val)
return n, nil
}
// errUnknown is used internally to indicate fields which should be added
// to the unknown field set of a message. It is never returned from an exported
// function.
var errUnknown = errors.New("unknown")

591
proto/decode_gen.go Normal file
View File

@ -0,0 +1,591 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style.
// license that can be found in the LICENSE file.
// Code generated by generate-types. DO NOT EDIT.
package proto
import (
"math"
"github.com/golang/protobuf/v2/internal/encoding/wire"
"github.com/golang/protobuf/v2/reflect/protoreflect"
)
// unmarshalScalar decodes a value of the given kind.
//
// Message values are decoded into a []byte which aliases the input data.
func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp wire.Type, num wire.Number, kind protoreflect.Kind) (val protoreflect.Value, n int, err error) {
switch kind {
case protoreflect.BoolKind:
if wtyp != wire.VarintType {
return val, 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(wire.DecodeBool(v)), n, nil
case protoreflect.EnumKind:
if wtyp != wire.VarintType {
return val, 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(protoreflect.EnumNumber(v)), n, nil
case protoreflect.Int32Kind:
if wtyp != wire.VarintType {
return val, 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(int32(v)), n, nil
case protoreflect.Sint32Kind:
if wtyp != wire.VarintType {
return val, 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(int32(wire.DecodeZigZag(v & math.MaxUint32))), n, nil
case protoreflect.Uint32Kind:
if wtyp != wire.VarintType {
return val, 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(uint32(v)), n, nil
case protoreflect.Int64Kind:
if wtyp != wire.VarintType {
return val, 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(int64(v)), n, nil
case protoreflect.Sint64Kind:
if wtyp != wire.VarintType {
return val, 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(wire.DecodeZigZag(v)), n, nil
case protoreflect.Uint64Kind:
if wtyp != wire.VarintType {
return val, 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(v), n, nil
case protoreflect.Sfixed32Kind:
if wtyp != wire.Fixed32Type {
return val, 0, errUnknown
}
v, n := wire.ConsumeFixed32(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(int32(v)), n, nil
case protoreflect.Fixed32Kind:
if wtyp != wire.Fixed32Type {
return val, 0, errUnknown
}
v, n := wire.ConsumeFixed32(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(uint32(v)), n, nil
case protoreflect.FloatKind:
if wtyp != wire.Fixed32Type {
return val, 0, errUnknown
}
v, n := wire.ConsumeFixed32(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(math.Float32frombits(uint32(v))), n, nil
case protoreflect.Sfixed64Kind:
if wtyp != wire.Fixed64Type {
return val, 0, errUnknown
}
v, n := wire.ConsumeFixed64(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(int64(v)), n, nil
case protoreflect.Fixed64Kind:
if wtyp != wire.Fixed64Type {
return val, 0, errUnknown
}
v, n := wire.ConsumeFixed64(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(v), n, nil
case protoreflect.DoubleKind:
if wtyp != wire.Fixed64Type {
return val, 0, errUnknown
}
v, n := wire.ConsumeFixed64(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(math.Float64frombits(v)), n, nil
case protoreflect.StringKind:
if wtyp != wire.BytesType {
return val, 0, errUnknown
}
v, n := wire.ConsumeBytes(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(string(v)), n, nil
case protoreflect.BytesKind:
if wtyp != wire.BytesType {
return val, 0, errUnknown
}
v, n := wire.ConsumeBytes(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(append(([]byte)(nil), v...)), n, nil
case protoreflect.MessageKind:
if wtyp != wire.BytesType {
return val, 0, errUnknown
}
v, n := wire.ConsumeBytes(b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(v), n, nil
case protoreflect.GroupKind:
if wtyp != wire.StartGroupType {
return val, 0, errUnknown
}
v, n := wire.ConsumeGroup(num, b)
if n < 0 {
return val, 0, wire.ParseError(n)
}
return protoreflect.ValueOf(v), n, nil
default:
return val, 0, errUnknown
}
}
func (o UnmarshalOptions) unmarshalList(b []byte, wtyp wire.Type, num wire.Number, list protoreflect.List, kind protoreflect.Kind) (n int, err error) {
switch kind {
case protoreflect.BoolKind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeVarint(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(wire.DecodeBool(v)))
}
return n, nil
}
if wtyp != wire.VarintType {
return 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(wire.DecodeBool(v)))
return n, nil
case protoreflect.EnumKind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeVarint(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(protoreflect.EnumNumber(v)))
}
return n, nil
}
if wtyp != wire.VarintType {
return 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(protoreflect.EnumNumber(v)))
return n, nil
case protoreflect.Int32Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeVarint(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(int32(v)))
}
return n, nil
}
if wtyp != wire.VarintType {
return 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(int32(v)))
return n, nil
case protoreflect.Sint32Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeVarint(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(int32(wire.DecodeZigZag(v & math.MaxUint32))))
}
return n, nil
}
if wtyp != wire.VarintType {
return 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(int32(wire.DecodeZigZag(v & math.MaxUint32))))
return n, nil
case protoreflect.Uint32Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeVarint(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(uint32(v)))
}
return n, nil
}
if wtyp != wire.VarintType {
return 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(uint32(v)))
return n, nil
case protoreflect.Int64Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeVarint(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(int64(v)))
}
return n, nil
}
if wtyp != wire.VarintType {
return 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(int64(v)))
return n, nil
case protoreflect.Sint64Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeVarint(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(wire.DecodeZigZag(v)))
}
return n, nil
}
if wtyp != wire.VarintType {
return 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(wire.DecodeZigZag(v)))
return n, nil
case protoreflect.Uint64Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeVarint(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(v))
}
return n, nil
}
if wtyp != wire.VarintType {
return 0, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(v))
return n, nil
case protoreflect.Sfixed32Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeFixed32(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(int32(v)))
}
return n, nil
}
if wtyp != wire.Fixed32Type {
return 0, errUnknown
}
v, n := wire.ConsumeFixed32(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(int32(v)))
return n, nil
case protoreflect.Fixed32Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeFixed32(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(uint32(v)))
}
return n, nil
}
if wtyp != wire.Fixed32Type {
return 0, errUnknown
}
v, n := wire.ConsumeFixed32(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(uint32(v)))
return n, nil
case protoreflect.FloatKind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeFixed32(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(math.Float32frombits(uint32(v))))
}
return n, nil
}
if wtyp != wire.Fixed32Type {
return 0, errUnknown
}
v, n := wire.ConsumeFixed32(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(math.Float32frombits(uint32(v))))
return n, nil
case protoreflect.Sfixed64Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeFixed64(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(int64(v)))
}
return n, nil
}
if wtyp != wire.Fixed64Type {
return 0, errUnknown
}
v, n := wire.ConsumeFixed64(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(int64(v)))
return n, nil
case protoreflect.Fixed64Kind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeFixed64(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(v))
}
return n, nil
}
if wtyp != wire.Fixed64Type {
return 0, errUnknown
}
v, n := wire.ConsumeFixed64(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(v))
return n, nil
case protoreflect.DoubleKind:
if wtyp == wire.BytesType {
buf, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
for len(buf) > 0 {
v, n := wire.ConsumeFixed64(buf)
if n < 0 {
return 0, wire.ParseError(n)
}
buf = buf[n:]
list.Append(protoreflect.ValueOf(math.Float64frombits(v)))
}
return n, nil
}
if wtyp != wire.Fixed64Type {
return 0, errUnknown
}
v, n := wire.ConsumeFixed64(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(math.Float64frombits(v)))
return n, nil
case protoreflect.StringKind:
if wtyp != wire.BytesType {
return 0, errUnknown
}
v, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(string(v)))
return n, nil
case protoreflect.BytesKind:
if wtyp != wire.BytesType {
return 0, errUnknown
}
v, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
list.Append(protoreflect.ValueOf(append(([]byte)(nil), v...)))
return n, nil
case protoreflect.MessageKind:
if wtyp != wire.BytesType {
return 0, errUnknown
}
v, n := wire.ConsumeBytes(b)
if n < 0 {
return 0, wire.ParseError(n)
}
m := list.NewMessage().ProtoReflect()
if err := o.unmarshalMessage(v, m); err != nil {
return 0, err
}
list.Append(protoreflect.ValueOf(m))
return n, nil
case protoreflect.GroupKind:
if wtyp != wire.StartGroupType {
return 0, errUnknown
}
v, n := wire.ConsumeGroup(num, b)
if n < 0 {
return 0, wire.ParseError(n)
}
m := list.NewMessage().ProtoReflect()
if err := o.unmarshalMessage(v, m); err != nil {
return 0, err
}
list.Append(protoreflect.ValueOf(m))
return n, nil
default:
return 0, errUnknown
}
}

749
proto/decode_test.go Normal file
View File

@ -0,0 +1,749 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style.
// license that can be found in the LICENSE file.
package proto
import (
"fmt"
"reflect"
"testing"
protoV1 "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/v2/internal/encoding/pack"
_ "github.com/golang/protobuf/v2/internal/legacy"
"github.com/golang/protobuf/v2/internal/scalar"
testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
)
type testProto struct {
desc string
decodeTo []Message
wire []byte
}
func TestDecode(t *testing.T) {
for _, test := range testProtos {
for _, want := range test.decodeTo {
t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
wire := append(([]byte)(nil), test.wire...)
got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(Message)
if err := Unmarshal(wire, got); err != nil {
t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
return
}
// Aliasing check: Modifying the original wire bytes shouldn't
// affect the unmarshaled message.
for i := range wire {
wire[i] = 0
}
if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message)))
}
})
}
}
}
var testProtos = []testProto{
{
desc: "basic scalar types",
decodeTo: []Message{&testpb.TestAllTypes{
OptionalInt32: scalar.Int32(1001),
OptionalInt64: scalar.Int64(1002),
OptionalUint32: scalar.Uint32(1003),
OptionalUint64: scalar.Uint64(1004),
OptionalSint32: scalar.Int32(1005),
OptionalSint64: scalar.Int64(1006),
OptionalFixed32: scalar.Uint32(1007),
OptionalFixed64: scalar.Uint64(1008),
OptionalSfixed32: scalar.Int32(1009),
OptionalSfixed64: scalar.Int64(1010),
OptionalFloat: scalar.Float32(1011.5),
OptionalDouble: scalar.Float64(1012.5),
OptionalBool: scalar.Bool(true),
OptionalString: scalar.String("string"),
OptionalBytes: []byte("bytes"),
OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
extend(testpb.E_OptionalStringExtension, scalar.String("string")),
extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
)},
wire: pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1001),
pack.Tag{2, pack.VarintType}, pack.Varint(1002),
pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
pack.Tag{13, pack.VarintType}, pack.Bool(true),
pack.Tag{14, pack.BytesType}, pack.String("string"),
pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
}.Marshal(),
},
{
desc: "groups",
decodeTo: []Message{&testpb.TestAllTypes{
Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
A: scalar.Int32(1017),
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
A: scalar.Int32(1017),
}),
)},
wire: pack.Message{
pack.Tag{16, pack.StartGroupType},
pack.Tag{17, pack.VarintType}, pack.Varint(1017),
pack.Tag{16, pack.EndGroupType},
}.Marshal(),
},
{
desc: "groups (field overridden)",
decodeTo: []Message{&testpb.TestAllTypes{
Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
A: scalar.Int32(2),
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
A: scalar.Int32(2),
}),
)},
wire: pack.Message{
pack.Tag{16, pack.StartGroupType},
pack.Tag{17, pack.VarintType}, pack.Varint(1),
pack.Tag{16, pack.EndGroupType},
pack.Tag{16, pack.StartGroupType},
pack.Tag{17, pack.VarintType}, pack.Varint(2),
pack.Tag{16, pack.EndGroupType},
}.Marshal(),
},
{
desc: "messages",
decodeTo: []Message{&testpb.TestAllTypes{
OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
A: scalar.Int32(42),
Corecursive: &testpb.TestAllTypes{
OptionalInt32: scalar.Int32(43),
},
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
A: scalar.Int32(42),
Corecursive: &testpb.TestAllTypes{
OptionalInt32: scalar.Int32(43),
},
}),
)},
wire: pack.Message{
pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(42),
pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(43),
}),
}),
}.Marshal(),
},
{
desc: "messages (split across multiple tags)",
decodeTo: []Message{&testpb.TestAllTypes{
OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
A: scalar.Int32(42),
Corecursive: &testpb.TestAllTypes{
OptionalInt32: scalar.Int32(43),
},
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
A: scalar.Int32(42),
Corecursive: &testpb.TestAllTypes{
OptionalInt32: scalar.Int32(43),
},
}),
)},
wire: pack.Message{
pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(42),
}),
pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(43),
}),
}),
}.Marshal(),
},
{
desc: "messages (field overridden)",
decodeTo: []Message{&testpb.TestAllTypes{
OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
A: scalar.Int32(2),
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
A: scalar.Int32(2),
}),
)},
wire: pack.Message{
pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1),
}),
pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2),
}),
}.Marshal(),
},
{
desc: "basic repeated types",
decodeTo: []Message{&testpb.TestAllTypes{
RepeatedInt32: []int32{1001, 2001},
RepeatedInt64: []int64{1002, 2002},
RepeatedUint32: []uint32{1003, 2003},
RepeatedUint64: []uint64{1004, 2004},
RepeatedSint32: []int32{1005, 2005},
RepeatedSint64: []int64{1006, 2006},
RepeatedFixed32: []uint32{1007, 2007},
RepeatedFixed64: []uint64{1008, 2008},
RepeatedSfixed32: []int32{1009, 2009},
RepeatedSfixed64: []int64{1010, 2010},
RepeatedFloat: []float32{1011.5, 2011.5},
RepeatedDouble: []float64{1012.5, 2012.5},
RepeatedBool: []bool{true, false},
RepeatedString: []string{"foo", "bar"},
RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
testpb.TestAllTypes_FOO,
testpb.TestAllTypes_BAR,
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
testpb.TestAllTypes_FOO,
testpb.TestAllTypes_BAR,
}),
)},
wire: pack.Message{
pack.Tag{31, pack.VarintType}, pack.Varint(1001),
pack.Tag{31, pack.VarintType}, pack.Varint(2001),
pack.Tag{32, pack.VarintType}, pack.Varint(1002),
pack.Tag{32, pack.VarintType}, pack.Varint(2002),
pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
pack.Tag{43, pack.VarintType}, pack.Bool(true),
pack.Tag{43, pack.VarintType}, pack.Bool(false),
pack.Tag{44, pack.BytesType}, pack.String("foo"),
pack.Tag{44, pack.BytesType}, pack.String("bar"),
pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
}.Marshal(),
},
{
desc: "basic repeated types (packed encoding)",
decodeTo: []Message{&testpb.TestAllTypes{
RepeatedInt32: []int32{1001, 2001},
RepeatedInt64: []int64{1002, 2002},
RepeatedUint32: []uint32{1003, 2003},
RepeatedUint64: []uint64{1004, 2004},
RepeatedSint32: []int32{1005, 2005},
RepeatedSint64: []int64{1006, 2006},
RepeatedFixed32: []uint32{1007, 2007},
RepeatedFixed64: []uint64{1008, 2008},
RepeatedSfixed32: []int32{1009, 2009},
RepeatedSfixed64: []int64{1010, 2010},
RepeatedFloat: []float32{1011.5, 2011.5},
RepeatedDouble: []float64{1012.5, 2012.5},
RepeatedBool: []bool{true, false},
RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
testpb.TestAllTypes_FOO,
testpb.TestAllTypes_BAR,
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
testpb.TestAllTypes_FOO,
testpb.TestAllTypes_BAR,
}),
)},
wire: pack.Message{
pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
pack.Varint(1001), pack.Varint(2001),
},
pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
pack.Varint(1002), pack.Varint(2002),
},
pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
pack.Uvarint(1003), pack.Uvarint(2003),
},
pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
pack.Uvarint(1004), pack.Uvarint(2004),
},
pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
pack.Svarint(1005), pack.Svarint(2005),
},
pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
pack.Svarint(1006), pack.Svarint(2006),
},
pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
pack.Uint32(1007), pack.Uint32(2007),
},
pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
pack.Uint64(1008), pack.Uint64(2008),
},
pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
pack.Int32(1009), pack.Int32(2009),
},
pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
pack.Int64(1010), pack.Int64(2010),
},
pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
pack.Float32(1011.5), pack.Float32(2011.5),
},
pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
pack.Float64(1012.5), pack.Float64(2012.5),
},
pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
pack.Bool(true), pack.Bool(false),
},
pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
pack.Varint(int(testpb.TestAllTypes_FOO)),
pack.Varint(int(testpb.TestAllTypes_BAR)),
},
}.Marshal(),
},
{
desc: "repeated messages",
decodeTo: []Message{&testpb.TestAllTypes{
RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
{A: scalar.Int32(1)},
{A: scalar.Int32(2)},
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
{A: scalar.Int32(1)},
{A: scalar.Int32(2)},
}),
)},
wire: pack.Message{
pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1),
}),
pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2),
}),
}.Marshal(),
},
{
desc: "repeated groups",
decodeTo: []Message{&testpb.TestAllTypes{
Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
{A: scalar.Int32(1017)},
{A: scalar.Int32(2017)},
},
}, build(
&testpb.TestAllExtensions{},
extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
{A: scalar.Int32(1017)},
{A: scalar.Int32(2017)},
}),
)},
wire: pack.Message{
pack.Tag{46, pack.StartGroupType},
pack.Tag{47, pack.VarintType}, pack.Varint(1017),
pack.Tag{46, pack.EndGroupType},
pack.Tag{46, pack.StartGroupType},
pack.Tag{47, pack.VarintType}, pack.Varint(2017),
pack.Tag{46, pack.EndGroupType},
}.Marshal(),
},
{
desc: "maps",
decodeTo: []Message{&testpb.TestAllTypes{
MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
MapBoolBool: map[bool]bool{true: false, false: true},
MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
"71.1.key": {A: scalar.Int32(1171)},
"71.2.key": {A: scalar.Int32(2171)},
},
MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
"73.1.key": testpb.TestAllTypes_FOO,
"73.2.key": testpb.TestAllTypes_BAR,
},
}},
wire: pack.Message{
pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1056),
pack.Tag{2, pack.VarintType}, pack.Varint(1156),
}),
pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2056),
pack.Tag{2, pack.VarintType}, pack.Varint(2156),
}),
pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1057),
pack.Tag{2, pack.VarintType}, pack.Varint(1157),
}),
pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2057),
pack.Tag{2, pack.VarintType}, pack.Varint(2157),
}),
pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1058),
pack.Tag{2, pack.VarintType}, pack.Varint(1158),
}),
pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2058),
pack.Tag{2, pack.VarintType}, pack.Varint(2158),
}),
pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1059),
pack.Tag{2, pack.VarintType}, pack.Varint(1159),
}),
pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2059),
pack.Tag{2, pack.VarintType}, pack.Varint(2159),
}),
pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
}),
pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
}),
pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
}),
pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
}),
pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
}),
pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
}),
pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
}),
pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
}),
pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
}),
pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
}),
pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
}),
pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
}),
pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1066),
pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
}),
pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2066),
pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
}),
pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1067),
pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
}),
pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2067),
pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
}),
pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Bool(true),
pack.Tag{2, pack.VarintType}, pack.Bool(false),
}),
pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Bool(false),
pack.Tag{2, pack.VarintType}, pack.Bool(true),
}),
pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
}),
pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
}),
pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
}),
pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
}),
pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1171),
}),
}),
pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(2171),
}),
}),
pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
}),
pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
}),
}.Marshal(),
},
{
desc: "oneof (uint32)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}}},
wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
},
{
desc: "oneof (message)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
&testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
}}},
wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
})}.Marshal(),
},
{
desc: "oneof (overridden message)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
&testpb.TestAllTypes_NestedMessage{
Corecursive: &testpb.TestAllTypes{
OptionalInt32: scalar.Int32(43),
},
},
}}},
wire: pack.Message{
pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
}),
pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(43),
}),
}),
}.Marshal(),
},
{
desc: "oneof (string)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}}},
wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
},
{
desc: "oneof (bytes)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}}},
wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
},
{
desc: "oneof (bool)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}}},
wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
},
{
desc: "oneof (uint64)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}}},
wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
},
{
desc: "oneof (float)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}}},
wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
},
{
desc: "oneof (double)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}}},
wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
},
{
desc: "oneof (enum)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}}},
wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
},
{
desc: "oneof (overridden value)",
decodeTo: []Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}}},
wire: pack.Message{
pack.Tag{111, pack.VarintType}, pack.Varint(1),
pack.Tag{116, pack.VarintType}, pack.Varint(2),
}.Marshal(),
},
// TODO: More unknown field tests for ordering, repeated fields, etc.
//
// It is currently impossible to produce results that the v1 Equal
// considers equivalent to those of the v1 decoder. Figure out if
// that's a problem or not.
{
desc: "unknown fields",
decodeTo: []Message{build(
&testpb.TestAllTypes{},
unknown(100000, pack.Message{
pack.Tag{100000, pack.VarintType}, pack.Varint(1),
}.Marshal()),
)},
wire: pack.Message{
pack.Tag{100000, pack.VarintType}, pack.Varint(1),
}.Marshal(),
},
{
desc: "field type mismatch",
decodeTo: []Message{build(
&testpb.TestAllTypes{},
unknown(1, pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("string"),
}.Marshal()),
)},
wire: pack.Message{
pack.Tag{1, pack.BytesType}, pack.String("string"),
}.Marshal(),
},
{
desc: "map field element mismatch",
decodeTo: []Message{
&testpb.TestAllTypes{
MapInt32Int32: map[int32]int32{1: 0},
},
},
wire: pack.Message{
pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1),
pack.Tag{2, pack.BytesType}, pack.String("string"),
}),
}.Marshal(),
},
}
func build(m Message, opts ...buildOpt) Message {
for _, opt := range opts {
opt(m)
}
return m
}
type buildOpt func(Message)
func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt {
return func(m Message) {
m.ProtoReflect().UnknownFields().Set(num, raw)
}
}
func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
return func(m Message) {
if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
panic(err)
}
}
}

View File

@ -5,6 +5,8 @@
set -e
go generate internal/cmd/generate-types/main.go
# Install the working tree's protoc-gen-gen in a tempdir.
tmpdir=$(mktemp -d -t protobuf-regen.XXXXXX)
trap 'rm -rf $tmpdir' EXIT
@ -17,6 +19,7 @@ GOBIN=$tmpdir/bin go install ./cmd/protoc-gen-go-grpc
PROTO_DIRS=(
cmd/protoc-gen-go/testdata
cmd/protoc-gen-go-grpc/testdata
internal/testprotos/test
)
for dir in ${PROTO_DIRS[@]}; do
for p in `find $dir -name "*.proto"`; do