message_set.go 9.1 KB

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