5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-14 06:10:27 +00:00
matterbridge/vendor/google.golang.org/protobuf/proto/messageset.go

94 lines
3.0 KiB
Go
Raw Normal View History

2020-08-09 22:29:54 +00:00
// 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 proto
import (
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/flags"
2021-06-16 19:00:49 +00:00
"google.golang.org/protobuf/internal/order"
2020-08-09 22:29:54 +00:00
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
2020-09-04 21:29:13 +00:00
func (o MarshalOptions) sizeMessageSet(m protoreflect.Message) (size int) {
2020-08-09 22:29:54 +00:00
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
size += messageset.SizeField(fd.Number())
size += protowire.SizeTag(messageset.FieldMessage)
2020-09-04 21:29:13 +00:00
size += protowire.SizeBytes(o.size(v.Message()))
2020-08-09 22:29:54 +00:00
return true
})
size += messageset.SizeUnknown(m.GetUnknown())
return size
}
2020-09-04 21:29:13 +00:00
func (o MarshalOptions) marshalMessageSet(b []byte, m protoreflect.Message) ([]byte, error) {
2020-08-09 22:29:54 +00:00
if !flags.ProtoLegacy {
return b, errors.New("no support for message_set_wire_format")
}
2021-06-16 19:00:49 +00:00
fieldOrder := order.AnyFieldOrder
if o.Deterministic {
fieldOrder = order.NumberFieldOrder
}
2020-08-09 22:29:54 +00:00
var err error
2021-06-16 19:00:49 +00:00
order.RangeFields(m, fieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
2020-09-04 21:29:13 +00:00
b, err = o.marshalMessageSetField(b, fd, v)
2020-08-09 22:29:54 +00:00
return err == nil
})
if err != nil {
return b, err
}
return messageset.AppendUnknown(b, m.GetUnknown())
}
2020-09-04 21:29:13 +00:00
func (o MarshalOptions) marshalMessageSetField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
2020-08-09 22:29:54 +00:00
b = messageset.AppendFieldStart(b, fd.Number())
b = protowire.AppendTag(b, messageset.FieldMessage, protowire.BytesType)
b = protowire.AppendVarint(b, uint64(o.Size(value.Message().Interface())))
b, err := o.marshalMessage(b, value.Message())
if err != nil {
return b, err
}
b = messageset.AppendFieldEnd(b)
return b, nil
}
2020-09-04 21:29:13 +00:00
func (o UnmarshalOptions) unmarshalMessageSet(b []byte, m protoreflect.Message) error {
2020-08-09 22:29:54 +00:00
if !flags.ProtoLegacy {
return errors.New("no support for message_set_wire_format")
}
return messageset.Unmarshal(b, false, func(num protowire.Number, v []byte) error {
2020-09-04 21:29:13 +00:00
err := o.unmarshalMessageSetField(m, num, v)
2020-08-09 22:29:54 +00:00
if err == errUnknown {
unknown := m.GetUnknown()
unknown = protowire.AppendTag(unknown, num, protowire.BytesType)
unknown = protowire.AppendBytes(unknown, v)
m.SetUnknown(unknown)
return nil
}
return err
})
}
2020-09-04 21:29:13 +00:00
func (o UnmarshalOptions) unmarshalMessageSetField(m protoreflect.Message, num protowire.Number, v []byte) error {
2020-08-09 22:29:54 +00:00
md := m.Descriptor()
if !md.ExtensionRanges().Has(num) {
return errUnknown
}
xt, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
if err == protoregistry.NotFound {
return errUnknown
}
if err != nil {
return errors.New("%v: unable to resolve extension %v: %v", md.FullName(), num, err)
}
xd := xt.TypeDescriptor()
if err := o.unmarshalMessage(v, m.Mutable(xd).Message()); err != nil {
return err
}
return nil
}