message_set.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2010 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. /*
  6. * Support for message sets.
  7. */
  8. import (
  9. "errors"
  10. "reflect"
  11. "google.golang.org/protobuf/reflect/protoreflect"
  12. )
  13. // errNoMessageTypeID occurs when a protocol buffer does not have a message type ID.
  14. // A message type ID is required for storing a protocol buffer in a message set.
  15. var errNoMessageTypeID = errors.New("proto does not have a message type ID")
  16. // The first two types (_MessageSet_Item and messageSet)
  17. // model what the protocol compiler produces for the following protocol message:
  18. // message MessageSet {
  19. // repeated group Item = 1 {
  20. // required int32 type_id = 2;
  21. // required string message = 3;
  22. // };
  23. // }
  24. // That is the MessageSet wire format. We can't use a proto to generate these
  25. // because that would introduce a circular dependency between it and this package.
  26. type _MessageSet_Item struct {
  27. TypeId *int32 `protobuf:"varint,2,req,name=type_id"`
  28. Message []byte `protobuf:"bytes,3,req,name=message"`
  29. }
  30. type messageSet struct {
  31. Item []*_MessageSet_Item `protobuf:"group,1,rep"`
  32. XXX_unrecognized []byte
  33. // TODO: caching?
  34. }
  35. // Make sure messageSet is a Message.
  36. var _ Message = (*messageSet)(nil)
  37. // messageTypeIder is an interface satisfied by a protocol buffer type
  38. // that may be stored in a MessageSet.
  39. type messageTypeIder interface {
  40. MessageTypeId() int32
  41. }
  42. func (ms *messageSet) find(pb Message) *_MessageSet_Item {
  43. mti, ok := pb.(messageTypeIder)
  44. if !ok {
  45. return nil
  46. }
  47. id := mti.MessageTypeId()
  48. for _, item := range ms.Item {
  49. if *item.TypeId == id {
  50. return item
  51. }
  52. }
  53. return nil
  54. }
  55. func (ms *messageSet) Has(pb Message) bool {
  56. return ms.find(pb) != nil
  57. }
  58. func (ms *messageSet) Unmarshal(pb Message) error {
  59. if item := ms.find(pb); item != nil {
  60. return Unmarshal(item.Message, pb)
  61. }
  62. if _, ok := pb.(messageTypeIder); !ok {
  63. return errNoMessageTypeID
  64. }
  65. return nil // TODO: return error instead?
  66. }
  67. func (ms *messageSet) Marshal(pb Message) error {
  68. msg, err := Marshal(pb)
  69. if err != nil {
  70. return err
  71. }
  72. if item := ms.find(pb); item != nil {
  73. // reuse existing item
  74. item.Message = msg
  75. return nil
  76. }
  77. mti, ok := pb.(messageTypeIder)
  78. if !ok {
  79. return errNoMessageTypeID
  80. }
  81. mtid := mti.MessageTypeId()
  82. ms.Item = append(ms.Item, &_MessageSet_Item{
  83. TypeId: &mtid,
  84. Message: msg,
  85. })
  86. return nil
  87. }
  88. func (ms *messageSet) Reset() { *ms = messageSet{} }
  89. func (ms *messageSet) String() string { return CompactTextString(ms) }
  90. func (*messageSet) ProtoMessage() {}
  91. // Support for the message_set_wire_format message option.
  92. func skipVarint(buf []byte) []byte {
  93. i := 0
  94. for ; buf[i]&0x80 != 0; i++ {
  95. }
  96. return buf[i+1:]
  97. }
  98. // unmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.
  99. // It is called by Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
  100. func unmarshalMessageSet(buf []byte, mi Message, exts interface{}) error {
  101. ms := new(messageSet)
  102. if err := Unmarshal(buf, ms); err != nil {
  103. return err
  104. }
  105. unrecognized := unknownFieldsValue(reflect.ValueOf(mi).Elem()).Addr().Interface().(*[]byte)
  106. for _, item := range ms.Item {
  107. id := protoreflect.FieldNumber(*item.TypeId)
  108. msg := item.Message
  109. // Restore wire type and field number varint, plus length varint.
  110. b := EncodeVarint(uint64(id)<<3 | WireBytes)
  111. b = append(b, EncodeVarint(uint64(len(msg)))...)
  112. b = append(b, msg...)
  113. *unrecognized = append(*unrecognized, b...)
  114. }
  115. return unmarshalExtensions(mi, unrecognized)
  116. }