message_set.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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(m map[int32]Extension) ([]byte, error) {
  134. if err := encodeExtensionMap(m); err != nil {
  135. return nil, err
  136. }
  137. // Sort extension IDs to provide a deterministic encoding.
  138. // See also enc_map in encode.go.
  139. ids := make([]int, 0, len(m))
  140. for id := range m {
  141. ids = append(ids, int(id))
  142. }
  143. sort.Ints(ids)
  144. ms := &messageSet{Item: make([]*_MessageSet_Item, 0, len(m))}
  145. for _, id := range ids {
  146. e := m[int32(id)]
  147. // Remove the wire type and field number varint, as well as the length varint.
  148. msg := skipVarint(skipVarint(e.enc))
  149. ms.Item = append(ms.Item, &_MessageSet_Item{
  150. TypeId: Int32(int32(id)),
  151. Message: msg,
  152. })
  153. }
  154. return Marshal(ms)
  155. }
  156. // UnmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.
  157. // It is called by generated Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
  158. func UnmarshalMessageSet(buf []byte, m map[int32]Extension) error {
  159. ms := new(messageSet)
  160. if err := Unmarshal(buf, ms); err != nil {
  161. return err
  162. }
  163. for _, item := range ms.Item {
  164. id := *item.TypeId
  165. msg := item.Message
  166. // Restore wire type and field number varint, plus length varint.
  167. // Be careful to preserve duplicate items.
  168. b := EncodeVarint(uint64(id)<<3 | WireBytes)
  169. if ext, ok := m[id]; ok {
  170. // Existing data; rip off the tag and length varint
  171. // so we join the new data correctly.
  172. // We can assume that ext.enc is set because we are unmarshaling.
  173. o := ext.enc[len(b):] // skip wire type and field number
  174. _, n := DecodeVarint(o) // calculate length of length varint
  175. o = o[n:] // skip length varint
  176. msg = append(o, msg...) // join old data and new data
  177. }
  178. b = append(b, EncodeVarint(uint64(len(msg)))...)
  179. b = append(b, msg...)
  180. m[id] = Extension{enc: b}
  181. }
  182. return nil
  183. }
  184. // MarshalMessageSetJSON encodes the extension map represented by m in JSON format.
  185. // It is called by generated MarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
  186. func MarshalMessageSetJSON(m map[int32]Extension) ([]byte, error) {
  187. var b bytes.Buffer
  188. b.WriteByte('{')
  189. // Process the map in key order for deterministic output.
  190. ids := make([]int32, 0, len(m))
  191. for id := range m {
  192. ids = append(ids, id)
  193. }
  194. sort.Sort(int32Slice(ids)) // int32Slice defined in text.go
  195. for i, id := range ids {
  196. ext := m[id]
  197. if i > 0 {
  198. b.WriteByte(',')
  199. }
  200. msd, ok := messageSetMap[id]
  201. if !ok {
  202. // Unknown type; we can't render it, so skip it.
  203. continue
  204. }
  205. fmt.Fprintf(&b, `"[%s]":`, msd.name)
  206. x := ext.value
  207. if x == nil {
  208. x = reflect.New(msd.t.Elem()).Interface()
  209. if err := Unmarshal(ext.enc, x.(Message)); err != nil {
  210. return nil, err
  211. }
  212. }
  213. d, err := json.Marshal(x)
  214. if err != nil {
  215. return nil, err
  216. }
  217. b.Write(d)
  218. }
  219. b.WriteByte('}')
  220. return b.Bytes(), nil
  221. }
  222. // UnmarshalMessageSetJSON decodes the extension map encoded in buf in JSON format.
  223. // It is called by generated UnmarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
  224. func UnmarshalMessageSetJSON(buf []byte, m map[int32]Extension) error {
  225. // Common-case fast path.
  226. if len(buf) == 0 || bytes.Equal(buf, []byte("{}")) {
  227. return nil
  228. }
  229. // This is fairly tricky, and it's not clear that it is needed.
  230. return errors.New("TODO: UnmarshalMessageSetJSON not yet implemented")
  231. }
  232. // A global registry of types that can be used in a MessageSet.
  233. var messageSetMap = make(map[int32]messageSetDesc)
  234. type messageSetDesc struct {
  235. t reflect.Type // pointer to struct
  236. name string
  237. }
  238. // RegisterMessageSetType is called from the generated code.
  239. func RegisterMessageSetType(m Message, fieldNum int32, name string) {
  240. messageSetMap[fieldNum] = messageSetDesc{
  241. t: reflect.TypeOf(m),
  242. name: name,
  243. }
  244. }