message_set.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. type _MessageSet_Item struct {
  57. TypeId *int32 `protobuf:"varint,2,req,name=type_id"`
  58. Message []byte `protobuf:"bytes,3,req,name=message"`
  59. }
  60. type messageSet struct {
  61. Item []*_MessageSet_Item `protobuf:"group,1,rep"`
  62. XXX_unrecognized []byte
  63. // TODO: caching?
  64. }
  65. // Make sure messageSet is a Message.
  66. var _ Message = (*messageSet)(nil)
  67. // messageTypeIder is an interface satisfied by a protocol buffer type
  68. // that may be stored in a MessageSet.
  69. type messageTypeIder interface {
  70. MessageTypeId() int32
  71. }
  72. func (ms *messageSet) find(pb Message) *_MessageSet_Item {
  73. mti, ok := pb.(messageTypeIder)
  74. if !ok {
  75. return nil
  76. }
  77. id := mti.MessageTypeId()
  78. for _, item := range ms.Item {
  79. if *item.TypeId == id {
  80. return item
  81. }
  82. }
  83. return nil
  84. }
  85. func (ms *messageSet) Has(pb Message) bool {
  86. if ms.find(pb) != nil {
  87. return true
  88. }
  89. return false
  90. }
  91. func (ms *messageSet) Unmarshal(pb Message) error {
  92. if item := ms.find(pb); item != nil {
  93. return Unmarshal(item.Message, pb)
  94. }
  95. if _, ok := pb.(messageTypeIder); !ok {
  96. return errNoMessageTypeID
  97. }
  98. return nil // TODO: return error instead?
  99. }
  100. func (ms *messageSet) Marshal(pb Message) error {
  101. msg, err := Marshal(pb)
  102. if err != nil {
  103. return err
  104. }
  105. if item := ms.find(pb); item != nil {
  106. // reuse existing item
  107. item.Message = msg
  108. return nil
  109. }
  110. mti, ok := pb.(messageTypeIder)
  111. if !ok {
  112. return errNoMessageTypeID
  113. }
  114. mtid := mti.MessageTypeId()
  115. ms.Item = append(ms.Item, &_MessageSet_Item{
  116. TypeId: &mtid,
  117. Message: msg,
  118. })
  119. return nil
  120. }
  121. func (ms *messageSet) Reset() { *ms = messageSet{} }
  122. func (ms *messageSet) String() string { return CompactTextString(ms) }
  123. func (*messageSet) ProtoMessage() {}
  124. // Support for the message_set_wire_format message option.
  125. func skipVarint(buf []byte) []byte {
  126. i := 0
  127. for ; buf[i]&0x80 != 0; i++ {
  128. }
  129. return buf[i+1:]
  130. }
  131. // MarshalMessageSet encodes the extension map represented by m in the message set wire format.
  132. // It is called by generated Marshal methods on protocol buffer messages with the message_set_wire_format option.
  133. func MarshalMessageSet(exts interface{}) ([]byte, error) {
  134. var m map[int32]Extension
  135. switch exts := exts.(type) {
  136. case *XXX_InternalExtensions:
  137. if err := encodeExtensions(exts); err != nil {
  138. return nil, err
  139. }
  140. m, _ = exts.extensionsRead()
  141. case map[int32]Extension:
  142. if err := encodeExtensionsMap(exts); err != nil {
  143. return nil, err
  144. }
  145. m = exts
  146. default:
  147. return nil, errors.New("proto: not an extension map")
  148. }
  149. // Sort extension IDs to provide a deterministic encoding.
  150. // See also enc_map in encode.go.
  151. ids := make([]int, 0, len(m))
  152. for id := range m {
  153. ids = append(ids, int(id))
  154. }
  155. sort.Ints(ids)
  156. ms := &messageSet{Item: make([]*_MessageSet_Item, 0, len(m))}
  157. for _, id := range ids {
  158. e := m[int32(id)]
  159. // Remove the wire type and field number varint, as well as the length varint.
  160. msg := skipVarint(skipVarint(e.enc))
  161. ms.Item = append(ms.Item, &_MessageSet_Item{
  162. TypeId: Int32(int32(id)),
  163. Message: msg,
  164. })
  165. }
  166. return Marshal(ms)
  167. }
  168. // UnmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.
  169. // It is called by generated Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
  170. func UnmarshalMessageSet(buf []byte, exts interface{}) error {
  171. var m map[int32]Extension
  172. switch exts := exts.(type) {
  173. case *XXX_InternalExtensions:
  174. m = exts.extensionsWrite()
  175. case map[int32]Extension:
  176. m = exts
  177. default:
  178. return errors.New("proto: not an extension map")
  179. }
  180. ms := new(messageSet)
  181. if err := Unmarshal(buf, ms); err != nil {
  182. return err
  183. }
  184. for _, item := range ms.Item {
  185. id := *item.TypeId
  186. msg := item.Message
  187. // Restore wire type and field number varint, plus length varint.
  188. // Be careful to preserve duplicate items.
  189. b := EncodeVarint(uint64(id)<<3 | WireBytes)
  190. if ext, ok := m[id]; ok {
  191. // Existing data; rip off the tag and length varint
  192. // so we join the new data correctly.
  193. // We can assume that ext.enc is set because we are unmarshaling.
  194. o := ext.enc[len(b):] // skip wire type and field number
  195. _, n := DecodeVarint(o) // calculate length of length varint
  196. o = o[n:] // skip length varint
  197. msg = append(o, msg...) // join old data and new data
  198. }
  199. b = append(b, EncodeVarint(uint64(len(msg)))...)
  200. b = append(b, msg...)
  201. m[id] = Extension{enc: b}
  202. }
  203. return nil
  204. }
  205. // MarshalMessageSetJSON encodes the extension map represented by m in JSON format.
  206. // It is called by generated MarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
  207. func MarshalMessageSetJSON(exts interface{}) ([]byte, error) {
  208. var m map[int32]Extension
  209. switch exts := exts.(type) {
  210. case *XXX_InternalExtensions:
  211. m, _ = exts.extensionsRead()
  212. case map[int32]Extension:
  213. m = exts
  214. default:
  215. return nil, errors.New("proto: not an extension map")
  216. }
  217. var b bytes.Buffer
  218. b.WriteByte('{')
  219. // Process the map in key order for deterministic output.
  220. ids := make([]int32, 0, len(m))
  221. for id := range m {
  222. ids = append(ids, id)
  223. }
  224. sort.Sort(int32Slice(ids)) // int32Slice defined in text.go
  225. for i, id := range ids {
  226. ext := m[id]
  227. if i > 0 {
  228. b.WriteByte(',')
  229. }
  230. msd, ok := messageSetMap[id]
  231. if !ok {
  232. // Unknown type; we can't render it, so skip it.
  233. continue
  234. }
  235. fmt.Fprintf(&b, `"[%s]":`, msd.name)
  236. x := ext.value
  237. if x == nil {
  238. x = reflect.New(msd.t.Elem()).Interface()
  239. if err := Unmarshal(ext.enc, x.(Message)); err != nil {
  240. return nil, err
  241. }
  242. }
  243. d, err := json.Marshal(x)
  244. if err != nil {
  245. return nil, err
  246. }
  247. b.Write(d)
  248. }
  249. b.WriteByte('}')
  250. return b.Bytes(), nil
  251. }
  252. // UnmarshalMessageSetJSON decodes the extension map encoded in buf in JSON format.
  253. // It is called by generated UnmarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
  254. func UnmarshalMessageSetJSON(buf []byte, exts interface{}) error {
  255. // Common-case fast path.
  256. if len(buf) == 0 || bytes.Equal(buf, []byte("{}")) {
  257. return nil
  258. }
  259. // This is fairly tricky, and it's not clear that it is needed.
  260. return errors.New("TODO: UnmarshalMessageSetJSON not yet implemented")
  261. }
  262. // A global registry of types that can be used in a MessageSet.
  263. var messageSetMap = make(map[int32]messageSetDesc)
  264. type messageSetDesc struct {
  265. t reflect.Type // pointer to struct
  266. name string
  267. }
  268. // RegisterMessageSetType is called from the generated code.
  269. func RegisterMessageSetType(m Message, fieldNum int32, name string) {
  270. messageSetMap[fieldNum] = messageSetDesc{
  271. t: reflect.TypeOf(m),
  272. name: name,
  273. }
  274. }