message_set.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto
  32. /*
  33. * Support for message sets.
  34. */
  35. import (
  36. "bytes"
  37. "encoding/json"
  38. "errors"
  39. "fmt"
  40. "reflect"
  41. "sort"
  42. )
  43. // ErrNoMessageTypeId occurs when a protocol buffer does not have a message type ID.
  44. // A message type ID is required for storing a protocol buffer in a message set.
  45. var ErrNoMessageTypeId = errors.New("proto does not have a message type ID")
  46. // The first two types (_MessageSet_Item and MessageSet)
  47. // model what the protocol compiler produces for the following protocol message:
  48. // message MessageSet {
  49. // repeated group Item = 1 {
  50. // required int32 type_id = 2;
  51. // required string message = 3;
  52. // };
  53. // }
  54. // That is the MessageSet wire format. We can't use a proto to generate these
  55. // because that would introduce a circular dependency between it and this package.
  56. //
  57. // When a proto1 proto has a field that looks like:
  58. // optional message<MessageSet> info = 3;
  59. // the protocol compiler produces a field in the generated struct that looks like:
  60. // Info *_proto_.MessageSet `protobuf:"bytes,3,opt,name=info"`
  61. // The package is automatically inserted so there is no need for that proto file to
  62. // import this package.
  63. type _MessageSet_Item struct {
  64. TypeId *int32 `protobuf:"varint,2,req,name=type_id"`
  65. Message []byte `protobuf:"bytes,3,req,name=message"`
  66. }
  67. type MessageSet struct {
  68. Item []*_MessageSet_Item `protobuf:"group,1,rep"`
  69. XXX_unrecognized []byte
  70. // TODO: caching?
  71. }
  72. // Make sure MessageSet is a Message.
  73. var _ Message = (*MessageSet)(nil)
  74. // messageTypeIder is an interface satisfied by a protocol buffer type
  75. // that may be stored in a MessageSet.
  76. type messageTypeIder interface {
  77. MessageTypeId() int32
  78. }
  79. func (ms *MessageSet) find(pb Message) *_MessageSet_Item {
  80. mti, ok := pb.(messageTypeIder)
  81. if !ok {
  82. return nil
  83. }
  84. id := mti.MessageTypeId()
  85. for _, item := range ms.Item {
  86. if *item.TypeId == id {
  87. return item
  88. }
  89. }
  90. return nil
  91. }
  92. func (ms *MessageSet) Has(pb Message) bool {
  93. if ms.find(pb) != nil {
  94. return true
  95. }
  96. return false
  97. }
  98. func (ms *MessageSet) Unmarshal(pb Message) error {
  99. if item := ms.find(pb); item != nil {
  100. return Unmarshal(item.Message, pb)
  101. }
  102. if _, ok := pb.(messageTypeIder); !ok {
  103. return ErrNoMessageTypeId
  104. }
  105. return nil // TODO: return error instead?
  106. }
  107. func (ms *MessageSet) Marshal(pb Message) error {
  108. msg, err := Marshal(pb)
  109. if err != nil {
  110. return err
  111. }
  112. if item := ms.find(pb); item != nil {
  113. // reuse existing item
  114. item.Message = msg
  115. return nil
  116. }
  117. mti, ok := pb.(messageTypeIder)
  118. if !ok {
  119. return ErrNoMessageTypeId
  120. }
  121. mtid := mti.MessageTypeId()
  122. ms.Item = append(ms.Item, &_MessageSet_Item{
  123. TypeId: &mtid,
  124. Message: msg,
  125. })
  126. return nil
  127. }
  128. func (ms *MessageSet) Reset() { *ms = MessageSet{} }
  129. func (ms *MessageSet) String() string { return CompactTextString(ms) }
  130. func (*MessageSet) ProtoMessage() {}
  131. // Support for the message_set_wire_format message option.
  132. func skipVarint(buf []byte) []byte {
  133. i := 0
  134. for ; buf[i]&0x80 != 0; i++ {
  135. }
  136. return buf[i+1:]
  137. }
  138. // MarshalMessageSet encodes the extension map represented by m in the message set wire format.
  139. // It is called by generated Marshal methods on protocol buffer messages with the message_set_wire_format option.
  140. func MarshalMessageSet(m map[int32]Extension) ([]byte, error) {
  141. if err := encodeExtensionMap(m); err != nil {
  142. return nil, err
  143. }
  144. // Sort extension IDs to provide a deterministic encoding.
  145. // See also enc_map in encode.go.
  146. ids := make([]int, 0, len(m))
  147. for id := range m {
  148. ids = append(ids, int(id))
  149. }
  150. sort.Ints(ids)
  151. ms := &MessageSet{Item: make([]*_MessageSet_Item, 0, len(m))}
  152. for _, id := range ids {
  153. e := m[int32(id)]
  154. // Remove the wire type and field number varint, as well as the length varint.
  155. msg := skipVarint(skipVarint(e.enc))
  156. ms.Item = append(ms.Item, &_MessageSet_Item{
  157. TypeId: Int32(int32(id)),
  158. Message: msg,
  159. })
  160. }
  161. return Marshal(ms)
  162. }
  163. // UnmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.
  164. // It is called by generated Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
  165. func UnmarshalMessageSet(buf []byte, m map[int32]Extension) error {
  166. ms := new(MessageSet)
  167. if err := Unmarshal(buf, ms); err != nil {
  168. return err
  169. }
  170. for _, item := range ms.Item {
  171. id := *item.TypeId
  172. msg := item.Message
  173. // Restore wire type and field number varint, plus length varint.
  174. // Be careful to preserve duplicate items.
  175. b := EncodeVarint(uint64(id)<<3 | WireBytes)
  176. if ext, ok := m[id]; ok {
  177. // Existing data; rip off the tag and length varint
  178. // so we join the new data correctly.
  179. // We can assume that ext.enc is set because we are unmarshaling.
  180. o := ext.enc[len(b):] // skip wire type and field number
  181. _, n := DecodeVarint(o) // calculate length of length varint
  182. o = o[n:] // skip length varint
  183. msg = append(o, msg...) // join old data and new data
  184. }
  185. b = append(b, EncodeVarint(uint64(len(msg)))...)
  186. b = append(b, msg...)
  187. m[id] = Extension{enc: b}
  188. }
  189. return nil
  190. }
  191. // MarshalMessageSetJSON encodes the extension map represented by m in JSON format.
  192. // It is called by generated MarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
  193. func MarshalMessageSetJSON(m map[int32]Extension) ([]byte, error) {
  194. var b bytes.Buffer
  195. b.WriteByte('{')
  196. // Process the map in key order for deterministic output.
  197. ids := make([]int32, 0, len(m))
  198. for id := range m {
  199. ids = append(ids, id)
  200. }
  201. sort.Sort(int32Slice(ids)) // int32Slice defined in text.go
  202. for i, id := range ids {
  203. ext := m[id]
  204. if i > 0 {
  205. b.WriteByte(',')
  206. }
  207. msd, ok := messageSetMap[id]
  208. if !ok {
  209. // Unknown type; we can't render it, so skip it.
  210. continue
  211. }
  212. fmt.Fprintf(&b, `"[%s]":`, msd.name)
  213. x := ext.value
  214. if x == nil {
  215. x = reflect.New(msd.t.Elem()).Interface()
  216. if err := Unmarshal(ext.enc, x.(Message)); err != nil {
  217. return nil, err
  218. }
  219. }
  220. d, err := json.Marshal(x)
  221. if err != nil {
  222. return nil, err
  223. }
  224. b.Write(d)
  225. }
  226. b.WriteByte('}')
  227. return b.Bytes(), nil
  228. }
  229. // UnmarshalMessageSetJSON decodes the extension map encoded in buf in JSON format.
  230. // It is called by generated UnmarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
  231. func UnmarshalMessageSetJSON(buf []byte, m map[int32]Extension) error {
  232. // Common-case fast path.
  233. if len(buf) == 0 || bytes.Equal(buf, []byte("{}")) {
  234. return nil
  235. }
  236. // This is fairly tricky, and it's not clear that it is needed.
  237. return errors.New("TODO: UnmarshalMessageSetJSON not yet implemented")
  238. }
  239. // A global registry of types that can be used in a MessageSet.
  240. var messageSetMap = make(map[int32]messageSetDesc)
  241. type messageSetDesc struct {
  242. t reflect.Type // pointer to struct
  243. name string
  244. }
  245. // RegisterMessageSetType is called from the generated code.
  246. func RegisterMessageSetType(m Message, fieldNum int32, name string) {
  247. messageSetMap[fieldNum] = messageSetDesc{
  248. t: reflect.TypeOf(m),
  249. name: name,
  250. }
  251. }