123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737 |
- // Copyright 2019 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 impl
- import (
- "fmt"
- "reflect"
- "sync"
- "google.golang.org/protobuf/internal/encoding/wire"
- "google.golang.org/protobuf/proto"
- pref "google.golang.org/protobuf/reflect/protoreflect"
- preg "google.golang.org/protobuf/reflect/protoregistry"
- )
- type errInvalidUTF8 struct{}
- func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" }
- func (errInvalidUTF8) InvalidUTF8() bool { return true }
- func makeOneofFieldCoder(fd pref.FieldDescriptor, si structInfo) pointerCoderFuncs {
- ot := si.oneofWrappersByNumber[fd.Number()]
- funcs := fieldCoder(fd, ot.Field(0).Type)
- fs := si.oneofsByName[fd.ContainingOneof().Name()]
- ft := fs.Type
- wiretag := wire.EncodeTag(fd.Number(), wireTypes[fd.Kind()])
- tagsize := wire.SizeVarint(wiretag)
- getInfo := func(p pointer) (pointer, bool) {
- v := p.AsValueOf(ft).Elem()
- if v.IsNil() {
- return pointer{}, false
- }
- v = v.Elem() // interface -> *struct
- if v.IsNil() || v.Elem().Type() != ot {
- return pointer{}, false
- }
- return pointerOfValue(v).Apply(zeroOffset), true
- }
- pcf := pointerCoderFuncs{
- size: func(p pointer, _ int, opts marshalOptions) int {
- v, ok := getInfo(p)
- if !ok {
- return 0
- }
- return funcs.size(v, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- v, ok := getInfo(p)
- if !ok {
- return b, nil
- }
- return funcs.marshal(b, v, wiretag, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- var vw reflect.Value // pointer to wrapper type
- vi := p.AsValueOf(ft).Elem() // oneof field value of interface kind
- if !vi.IsNil() && !vi.Elem().IsNil() && vi.Elem().Elem().Type() == ot {
- vw = vi.Elem()
- } else {
- vw = reflect.New(ot)
- }
- n, err := funcs.unmarshal(b, pointerOfValue(vw).Apply(zeroOffset), wtyp, opts)
- if err != nil {
- return 0, err
- }
- vi.Set(vw)
- return n, nil
- },
- }
- if funcs.isInit != nil {
- pcf.isInit = func(p pointer) error {
- v, ok := getInfo(p)
- if !ok {
- return nil
- }
- return funcs.isInit(v)
- }
- }
- return pcf
- }
- func makeWeakMessageFieldCoder(fd pref.FieldDescriptor) pointerCoderFuncs {
- var once sync.Once
- var messageType pref.MessageType
- lazyInit := func() {
- once.Do(func() {
- messageName := fd.Message().FullName()
- messageType, _ = preg.GlobalTypes.FindMessageByName(messageName)
- })
- }
- num := fd.Number()
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- m, ok := p.WeakFields().get(num)
- if !ok {
- return 0
- }
- lazyInit()
- if messageType == nil {
- panic(fmt.Sprintf("weak message %v is not linked in", fd.Message().FullName()))
- }
- return sizeMessage(m, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- m, ok := p.WeakFields().get(num)
- if !ok {
- return b, nil
- }
- lazyInit()
- if messageType == nil {
- panic(fmt.Sprintf("weak message %v is not linked in", fd.Message().FullName()))
- }
- return appendMessage(b, m, wiretag, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- fs := p.WeakFields()
- m, ok := fs.get(num)
- if !ok {
- lazyInit()
- if messageType == nil {
- return 0, errUnknown
- }
- m = messageType.New().Interface()
- fs.set(num, m)
- }
- return consumeMessage(b, m, wtyp, opts)
- },
- isInit: func(p pointer) error {
- m, ok := p.WeakFields().get(num)
- if !ok {
- return nil
- }
- return proto.IsInitialized(m)
- },
- }
- }
- func makeMessageFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
- if mi := getMessageInfo(ft); mi != nil {
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- return sizeMessageInfo(p, mi, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- return appendMessageInfo(b, p, wiretag, mi, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- return consumeMessageInfo(b, p, mi, wtyp, opts)
- },
- isInit: func(p pointer) error {
- return mi.isInitializedPointer(p.Elem())
- },
- }
- } else {
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- m := asMessage(p.AsValueOf(ft).Elem())
- return sizeMessage(m, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- m := asMessage(p.AsValueOf(ft).Elem())
- return appendMessage(b, m, wiretag, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- mp := p.AsValueOf(ft).Elem()
- if mp.IsNil() {
- mp.Set(reflect.New(ft.Elem()))
- }
- return consumeMessage(b, asMessage(mp), wtyp, opts)
- },
- isInit: func(p pointer) error {
- m := asMessage(p.AsValueOf(ft).Elem())
- return proto.IsInitialized(m)
- },
- }
- }
- }
- func sizeMessageInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int {
- return wire.SizeBytes(mi.sizePointer(p.Elem(), opts)) + tagsize
- }
- func appendMessageInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) {
- b = wire.AppendVarint(b, wiretag)
- b = wire.AppendVarint(b, uint64(mi.sizePointer(p.Elem(), opts)))
- return mi.marshalAppendPointer(b, p.Elem(), opts)
- }
- func consumeMessageInfo(b []byte, p pointer, mi *MessageInfo, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- if wtyp != wire.BytesType {
- return 0, errUnknown
- }
- v, n := wire.ConsumeBytes(b)
- if n < 0 {
- return 0, wire.ParseError(n)
- }
- if p.Elem().IsNil() {
- p.SetPointer(pointerOfValue(reflect.New(mi.GoReflectType.Elem())))
- }
- if _, err := mi.unmarshalPointer(v, p.Elem(), 0, opts); err != nil {
- return 0, err
- }
- return n, nil
- }
- func sizeMessage(m proto.Message, tagsize int, _ marshalOptions) int {
- return wire.SizeBytes(proto.Size(m)) + tagsize
- }
- func appendMessage(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) {
- b = wire.AppendVarint(b, wiretag)
- b = wire.AppendVarint(b, uint64(proto.Size(m)))
- return opts.Options().MarshalAppend(b, m)
- }
- func consumeMessage(b []byte, m proto.Message, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- if wtyp != wire.BytesType {
- return 0, errUnknown
- }
- v, n := wire.ConsumeBytes(b)
- if n < 0 {
- return 0, wire.ParseError(n)
- }
- if err := opts.Options().Unmarshal(v, m); err != nil {
- return 0, err
- }
- return n, nil
- }
- func sizeMessageValue(v pref.Value, tagsize int, opts marshalOptions) int {
- m := v.Message().Interface()
- return sizeMessage(m, tagsize, opts)
- }
- func appendMessageValue(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
- m := v.Message().Interface()
- return appendMessage(b, m, wiretag, opts)
- }
- func consumeMessageValue(b []byte, v pref.Value, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) {
- m := v.Message().Interface()
- n, err := consumeMessage(b, m, wtyp, opts)
- return v, n, err
- }
- func isInitMessageValue(v pref.Value) error {
- m := v.Message().Interface()
- return proto.IsInitialized(m)
- }
- var coderMessageValue = valueCoderFuncs{
- size: sizeMessageValue,
- marshal: appendMessageValue,
- unmarshal: consumeMessageValue,
- isInit: isInitMessageValue,
- }
- func sizeGroupValue(v pref.Value, tagsize int, opts marshalOptions) int {
- m := v.Message().Interface()
- return sizeGroup(m, tagsize, opts)
- }
- func appendGroupValue(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
- m := v.Message().Interface()
- return appendGroup(b, m, wiretag, opts)
- }
- func consumeGroupValue(b []byte, v pref.Value, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) {
- m := v.Message().Interface()
- n, err := consumeGroup(b, m, num, wtyp, opts)
- return v, n, err
- }
- var coderGroupValue = valueCoderFuncs{
- size: sizeGroupValue,
- marshal: appendGroupValue,
- unmarshal: consumeGroupValue,
- isInit: isInitMessageValue,
- }
- func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
- num := fd.Number()
- if mi := getMessageInfo(ft); mi != nil {
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- return sizeGroupType(p, mi, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- return appendGroupType(b, p, wiretag, mi, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- return consumeGroupType(b, p, mi, num, wtyp, opts)
- },
- isInit: func(p pointer) error {
- return mi.isInitializedPointer(p.Elem())
- },
- }
- } else {
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- m := asMessage(p.AsValueOf(ft).Elem())
- return sizeGroup(m, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- m := asMessage(p.AsValueOf(ft).Elem())
- return appendGroup(b, m, wiretag, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- mp := p.AsValueOf(ft).Elem()
- if mp.IsNil() {
- mp.Set(reflect.New(ft.Elem()))
- }
- return consumeGroup(b, asMessage(mp), num, wtyp, opts)
- },
- isInit: func(p pointer) error {
- m := asMessage(p.AsValueOf(ft).Elem())
- return proto.IsInitialized(m)
- },
- }
- }
- }
- func sizeGroupType(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int {
- return 2*tagsize + mi.sizePointer(p.Elem(), opts)
- }
- func appendGroupType(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) {
- b = wire.AppendVarint(b, wiretag) // start group
- b, err := mi.marshalAppendPointer(b, p.Elem(), opts)
- b = wire.AppendVarint(b, wiretag+1) // end group
- return b, err
- }
- func consumeGroupType(b []byte, p pointer, mi *MessageInfo, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- if wtyp != wire.StartGroupType {
- return 0, errUnknown
- }
- if p.Elem().IsNil() {
- p.SetPointer(pointerOfValue(reflect.New(mi.GoReflectType.Elem())))
- }
- return mi.unmarshalPointer(b, p.Elem(), num, opts)
- }
- func sizeGroup(m proto.Message, tagsize int, _ marshalOptions) int {
- return 2*tagsize + proto.Size(m)
- }
- func appendGroup(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) {
- b = wire.AppendVarint(b, wiretag) // start group
- b, err := opts.Options().MarshalAppend(b, m)
- b = wire.AppendVarint(b, wiretag+1) // end group
- return b, err
- }
- func consumeGroup(b []byte, m proto.Message, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- if wtyp != wire.StartGroupType {
- return 0, errUnknown
- }
- b, n := wire.ConsumeGroup(num, b)
- if n < 0 {
- return 0, wire.ParseError(n)
- }
- return n, opts.Options().Unmarshal(b, m)
- }
- func makeMessageSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
- if mi := getMessageInfo(ft); mi != nil {
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- return sizeMessageSliceInfo(p, mi, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- return appendMessageSliceInfo(b, p, wiretag, mi, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- return consumeMessageSliceInfo(b, p, mi, wtyp, opts)
- },
- isInit: func(p pointer) error {
- return isInitMessageSliceInfo(p, mi)
- },
- }
- }
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- return sizeMessageSlice(p, ft, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- return appendMessageSlice(b, p, wiretag, ft, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- return consumeMessageSlice(b, p, ft, wtyp, opts)
- },
- isInit: func(p pointer) error {
- return isInitMessageSlice(p, ft)
- },
- }
- }
- func sizeMessageSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int {
- s := p.PointerSlice()
- n := 0
- for _, v := range s {
- n += wire.SizeBytes(mi.sizePointer(v, opts)) + tagsize
- }
- return n
- }
- func appendMessageSliceInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) {
- s := p.PointerSlice()
- var err error
- for _, v := range s {
- b = wire.AppendVarint(b, wiretag)
- siz := mi.sizePointer(v, opts)
- b = wire.AppendVarint(b, uint64(siz))
- b, err = mi.marshalAppendPointer(b, v, opts)
- if err != nil {
- return b, err
- }
- }
- return b, nil
- }
- func consumeMessageSliceInfo(b []byte, p pointer, mi *MessageInfo, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- if wtyp != wire.BytesType {
- return 0, errUnknown
- }
- v, n := wire.ConsumeBytes(b)
- if n < 0 {
- return 0, wire.ParseError(n)
- }
- m := reflect.New(mi.GoReflectType.Elem()).Interface()
- mp := pointerOfIface(m)
- if _, err := mi.unmarshalPointer(v, mp, 0, opts); err != nil {
- return 0, err
- }
- p.AppendPointerSlice(mp)
- return n, nil
- }
- func isInitMessageSliceInfo(p pointer, mi *MessageInfo) error {
- s := p.PointerSlice()
- for _, v := range s {
- if err := mi.isInitializedPointer(v); err != nil {
- return err
- }
- }
- return nil
- }
- func sizeMessageSlice(p pointer, goType reflect.Type, tagsize int, _ marshalOptions) int {
- s := p.PointerSlice()
- n := 0
- for _, v := range s {
- m := asMessage(v.AsValueOf(goType.Elem()))
- n += wire.SizeBytes(proto.Size(m)) + tagsize
- }
- return n
- }
- func appendMessageSlice(b []byte, p pointer, wiretag uint64, goType reflect.Type, opts marshalOptions) ([]byte, error) {
- s := p.PointerSlice()
- var err error
- for _, v := range s {
- m := asMessage(v.AsValueOf(goType.Elem()))
- b = wire.AppendVarint(b, wiretag)
- siz := proto.Size(m)
- b = wire.AppendVarint(b, uint64(siz))
- b, err = opts.Options().MarshalAppend(b, m)
- if err != nil {
- return b, err
- }
- }
- return b, nil
- }
- func consumeMessageSlice(b []byte, p pointer, goType reflect.Type, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- if wtyp != wire.BytesType {
- return 0, errUnknown
- }
- v, n := wire.ConsumeBytes(b)
- if n < 0 {
- return 0, wire.ParseError(n)
- }
- mp := reflect.New(goType.Elem())
- if err := opts.Options().Unmarshal(v, asMessage(mp)); err != nil {
- return 0, err
- }
- p.AppendPointerSlice(pointerOfValue(mp))
- return n, nil
- }
- func isInitMessageSlice(p pointer, goType reflect.Type) error {
- s := p.PointerSlice()
- for _, v := range s {
- m := asMessage(v.AsValueOf(goType.Elem()))
- if err := proto.IsInitialized(m); err != nil {
- return err
- }
- }
- return nil
- }
- // Slices of messages
- func sizeMessageSliceValue(listv pref.Value, tagsize int, opts marshalOptions) int {
- list := listv.List()
- n := 0
- for i, llen := 0, list.Len(); i < llen; i++ {
- m := list.Get(i).Message().Interface()
- n += wire.SizeBytes(proto.Size(m)) + tagsize
- }
- return n
- }
- func appendMessageSliceValue(b []byte, listv pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
- list := listv.List()
- mopts := opts.Options()
- for i, llen := 0, list.Len(); i < llen; i++ {
- m := list.Get(i).Message().Interface()
- b = wire.AppendVarint(b, wiretag)
- siz := proto.Size(m)
- b = wire.AppendVarint(b, uint64(siz))
- var err error
- b, err = mopts.MarshalAppend(b, m)
- if err != nil {
- return b, err
- }
- }
- return b, nil
- }
- func consumeMessageSliceValue(b []byte, listv pref.Value, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) {
- list := listv.List()
- if wtyp != wire.BytesType {
- return pref.Value{}, 0, errUnknown
- }
- v, n := wire.ConsumeBytes(b)
- if n < 0 {
- return pref.Value{}, 0, wire.ParseError(n)
- }
- m := list.NewElement()
- if err := opts.Options().Unmarshal(v, m.Message().Interface()); err != nil {
- return pref.Value{}, 0, err
- }
- list.Append(m)
- return listv, n, nil
- }
- func isInitMessageSliceValue(listv pref.Value) error {
- list := listv.List()
- for i, llen := 0, list.Len(); i < llen; i++ {
- m := list.Get(i).Message().Interface()
- if err := proto.IsInitialized(m); err != nil {
- return err
- }
- }
- return nil
- }
- var coderMessageSliceValue = valueCoderFuncs{
- size: sizeMessageSliceValue,
- marshal: appendMessageSliceValue,
- unmarshal: consumeMessageSliceValue,
- isInit: isInitMessageSliceValue,
- }
- func sizeGroupSliceValue(listv pref.Value, tagsize int, opts marshalOptions) int {
- list := listv.List()
- n := 0
- for i, llen := 0, list.Len(); i < llen; i++ {
- m := list.Get(i).Message().Interface()
- n += 2*tagsize + proto.Size(m)
- }
- return n
- }
- func appendGroupSliceValue(b []byte, listv pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
- list := listv.List()
- mopts := opts.Options()
- for i, llen := 0, list.Len(); i < llen; i++ {
- m := list.Get(i).Message().Interface()
- b = wire.AppendVarint(b, wiretag) // start group
- var err error
- b, err = mopts.MarshalAppend(b, m)
- if err != nil {
- return b, err
- }
- b = wire.AppendVarint(b, wiretag+1) // end group
- }
- return b, nil
- }
- func consumeGroupSliceValue(b []byte, listv pref.Value, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) {
- list := listv.List()
- if wtyp != wire.StartGroupType {
- return pref.Value{}, 0, errUnknown
- }
- b, n := wire.ConsumeGroup(num, b)
- if n < 0 {
- return pref.Value{}, 0, wire.ParseError(n)
- }
- m := list.NewElement()
- if err := opts.Options().Unmarshal(b, m.Message().Interface()); err != nil {
- return pref.Value{}, 0, err
- }
- list.Append(m)
- return listv, n, nil
- }
- var coderGroupSliceValue = valueCoderFuncs{
- size: sizeGroupSliceValue,
- marshal: appendGroupSliceValue,
- unmarshal: consumeGroupSliceValue,
- isInit: isInitMessageSliceValue,
- }
- func makeGroupSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
- num := fd.Number()
- if mi := getMessageInfo(ft); mi != nil {
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- return sizeGroupSliceInfo(p, mi, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- return appendGroupSliceInfo(b, p, wiretag, mi, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- return consumeGroupSliceInfo(b, p, num, wtyp, mi, opts)
- },
- isInit: func(p pointer) error {
- return isInitMessageSliceInfo(p, mi)
- },
- }
- }
- return pointerCoderFuncs{
- size: func(p pointer, tagsize int, opts marshalOptions) int {
- return sizeGroupSlice(p, ft, tagsize, opts)
- },
- marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
- return appendGroupSlice(b, p, wiretag, ft, opts)
- },
- unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
- return consumeGroupSlice(b, p, num, wtyp, ft, opts)
- },
- isInit: func(p pointer) error {
- return isInitMessageSlice(p, ft)
- },
- }
- }
- func sizeGroupSlice(p pointer, messageType reflect.Type, tagsize int, _ marshalOptions) int {
- s := p.PointerSlice()
- n := 0
- for _, v := range s {
- m := asMessage(v.AsValueOf(messageType.Elem()))
- n += 2*tagsize + proto.Size(m)
- }
- return n
- }
- func appendGroupSlice(b []byte, p pointer, wiretag uint64, messageType reflect.Type, opts marshalOptions) ([]byte, error) {
- s := p.PointerSlice()
- var err error
- for _, v := range s {
- m := asMessage(v.AsValueOf(messageType.Elem()))
- b = wire.AppendVarint(b, wiretag) // start group
- b, err = opts.Options().MarshalAppend(b, m)
- if err != nil {
- return b, err
- }
- b = wire.AppendVarint(b, wiretag+1) // end group
- }
- return b, nil
- }
- func consumeGroupSlice(b []byte, p pointer, num wire.Number, wtyp wire.Type, goType reflect.Type, opts unmarshalOptions) (int, error) {
- if wtyp != wire.StartGroupType {
- return 0, errUnknown
- }
- b, n := wire.ConsumeGroup(num, b)
- if n < 0 {
- return 0, wire.ParseError(n)
- }
- mp := reflect.New(goType.Elem())
- if err := opts.Options().Unmarshal(b, asMessage(mp)); err != nil {
- return 0, err
- }
- p.AppendPointerSlice(pointerOfValue(mp))
- return n, nil
- }
- func sizeGroupSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int {
- s := p.PointerSlice()
- n := 0
- for _, v := range s {
- n += 2*tagsize + mi.sizePointer(v, opts)
- }
- return n
- }
- func appendGroupSliceInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) {
- s := p.PointerSlice()
- var err error
- for _, v := range s {
- b = wire.AppendVarint(b, wiretag) // start group
- b, err = mi.marshalAppendPointer(b, v, opts)
- if err != nil {
- return b, err
- }
- b = wire.AppendVarint(b, wiretag+1) // end group
- }
- return b, nil
- }
- func consumeGroupSliceInfo(b []byte, p pointer, num wire.Number, wtyp wire.Type, mi *MessageInfo, opts unmarshalOptions) (int, error) {
- if wtyp != wire.StartGroupType {
- return 0, errUnknown
- }
- m := reflect.New(mi.GoReflectType.Elem()).Interface()
- mp := pointerOfIface(m)
- n, err := mi.unmarshalPointer(b, mp, num, opts)
- if err != nil {
- return 0, err
- }
- p.AppendPointerSlice(mp)
- return n, nil
- }
- func asMessage(v reflect.Value) pref.ProtoMessage {
- if m, ok := v.Interface().(pref.ProtoMessage); ok {
- return m
- }
- return legacyWrapMessage(v)
- }
|