messageset.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto
  5. import (
  6. "google.golang.org/protobuf/encoding/protowire"
  7. "google.golang.org/protobuf/internal/encoding/messageset"
  8. "google.golang.org/protobuf/internal/errors"
  9. "google.golang.org/protobuf/internal/flags"
  10. "google.golang.org/protobuf/internal/order"
  11. "google.golang.org/protobuf/reflect/protoreflect"
  12. "google.golang.org/protobuf/reflect/protoregistry"
  13. )
  14. func (o MarshalOptions) sizeMessageSet(m protoreflect.Message) (size int) {
  15. m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  16. size += messageset.SizeField(fd.Number())
  17. size += protowire.SizeTag(messageset.FieldMessage)
  18. size += protowire.SizeBytes(o.size(v.Message()))
  19. return true
  20. })
  21. size += messageset.SizeUnknown(m.GetUnknown())
  22. return size
  23. }
  24. func (o MarshalOptions) marshalMessageSet(b []byte, m protoreflect.Message) ([]byte, error) {
  25. if !flags.ProtoLegacy {
  26. return b, errors.New("no support for message_set_wire_format")
  27. }
  28. fieldOrder := order.AnyFieldOrder
  29. if o.Deterministic {
  30. fieldOrder = order.NumberFieldOrder
  31. }
  32. var err error
  33. order.RangeFields(m, fieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  34. b, err = o.marshalMessageSetField(b, fd, v)
  35. return err == nil
  36. })
  37. if err != nil {
  38. return b, err
  39. }
  40. return messageset.AppendUnknown(b, m.GetUnknown())
  41. }
  42. func (o MarshalOptions) marshalMessageSetField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
  43. b = messageset.AppendFieldStart(b, fd.Number())
  44. b = protowire.AppendTag(b, messageset.FieldMessage, protowire.BytesType)
  45. b = protowire.AppendVarint(b, uint64(o.Size(value.Message().Interface())))
  46. b, err := o.marshalMessage(b, value.Message())
  47. if err != nil {
  48. return b, err
  49. }
  50. b = messageset.AppendFieldEnd(b)
  51. return b, nil
  52. }
  53. func (o UnmarshalOptions) unmarshalMessageSet(b []byte, m protoreflect.Message) error {
  54. if !flags.ProtoLegacy {
  55. return errors.New("no support for message_set_wire_format")
  56. }
  57. return messageset.Unmarshal(b, false, func(num protowire.Number, v []byte) error {
  58. err := o.unmarshalMessageSetField(m, num, v)
  59. if err == errUnknown {
  60. unknown := m.GetUnknown()
  61. unknown = protowire.AppendTag(unknown, num, protowire.BytesType)
  62. unknown = protowire.AppendBytes(unknown, v)
  63. m.SetUnknown(unknown)
  64. return nil
  65. }
  66. return err
  67. })
  68. }
  69. func (o UnmarshalOptions) unmarshalMessageSetField(m protoreflect.Message, num protowire.Number, v []byte) error {
  70. md := m.Descriptor()
  71. if !md.ExtensionRanges().Has(num) {
  72. return errUnknown
  73. }
  74. xt, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
  75. if err == protoregistry.NotFound {
  76. return errUnknown
  77. }
  78. if err != nil {
  79. return errors.New("%v: unable to resolve extension %v: %v", md.FullName(), num, err)
  80. }
  81. xd := xt.TypeDescriptor()
  82. if err := o.unmarshalMessage(v, m.Mutable(xd).Message()); err != nil {
  83. return err
  84. }
  85. return nil
  86. }