messageset.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. calculatedSize := o.Size(value.Message().Interface())
  46. b = protowire.AppendVarint(b, uint64(calculatedSize))
  47. before := len(b)
  48. b, err := o.marshalMessage(b, value.Message())
  49. if err != nil {
  50. return b, err
  51. }
  52. if measuredSize := len(b) - before; calculatedSize != measuredSize {
  53. return nil, errors.MismatchedSizeCalculation(calculatedSize, measuredSize)
  54. }
  55. b = messageset.AppendFieldEnd(b)
  56. return b, nil
  57. }
  58. func (o UnmarshalOptions) unmarshalMessageSet(b []byte, m protoreflect.Message) error {
  59. if !flags.ProtoLegacy {
  60. return errors.New("no support for message_set_wire_format")
  61. }
  62. return messageset.Unmarshal(b, false, func(num protowire.Number, v []byte) error {
  63. err := o.unmarshalMessageSetField(m, num, v)
  64. if err == errUnknown {
  65. unknown := m.GetUnknown()
  66. unknown = protowire.AppendTag(unknown, num, protowire.BytesType)
  67. unknown = protowire.AppendBytes(unknown, v)
  68. m.SetUnknown(unknown)
  69. return nil
  70. }
  71. return err
  72. })
  73. }
  74. func (o UnmarshalOptions) unmarshalMessageSetField(m protoreflect.Message, num protowire.Number, v []byte) error {
  75. md := m.Descriptor()
  76. if !md.ExtensionRanges().Has(num) {
  77. return errUnknown
  78. }
  79. xt, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
  80. if err == protoregistry.NotFound {
  81. return errUnknown
  82. }
  83. if err != nil {
  84. return errors.New("%v: unable to resolve extension %v: %v", md.FullName(), num, err)
  85. }
  86. xd := xt.TypeDescriptor()
  87. if err := o.unmarshalMessage(v, m.Mutable(xd).Message()); err != nil {
  88. return err
  89. }
  90. return nil
  91. }