encode_gogo.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Extensions for Protocol Buffers to create more go like structures.
  2. //
  3. // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
  4. // http://code.google.com/p/gogoprotobuf/gogoproto
  5. //
  6. // Go support for Protocol Buffers - Google's data interchange format
  7. //
  8. // Copyright 2010 The Go Authors. All rights reserved.
  9. // http://code.google.com/p/goprotobuf/
  10. //
  11. // Redistribution and use in source and binary forms, with or without
  12. // modification, are permitted provided that the following conditions are
  13. // met:
  14. //
  15. // * Redistributions of source code must retain the above copyright
  16. // notice, this list of conditions and the following disclaimer.
  17. // * Redistributions in binary form must reproduce the above
  18. // copyright notice, this list of conditions and the following disclaimer
  19. // in the documentation and/or other materials provided with the
  20. // distribution.
  21. // * Neither the name of Google Inc. nor the names of its
  22. // contributors may be used to endorse or promote products derived from
  23. // this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. package proto
  37. import (
  38. "reflect"
  39. )
  40. type Sizer interface {
  41. Size() int
  42. }
  43. // Encode a reference to bool pointer.
  44. func (o *Buffer) enc_ref_bool(p *Properties, base structPointer) error {
  45. v := structPointer_RefBool(base, p.field)
  46. if v == nil {
  47. return ErrNil
  48. }
  49. x := 0
  50. if *v {
  51. x = 1
  52. }
  53. o.buf = append(o.buf, p.tagcode...)
  54. p.valEnc(o, uint64(x))
  55. return nil
  56. }
  57. func size_ref_bool(p *Properties, base structPointer) int {
  58. v := structPointer_RefBool(base, p.field)
  59. if v == nil {
  60. return 0
  61. }
  62. return len(p.tagcode) + 1 // each bool takes exactly one byte
  63. }
  64. // Encode a reference to int32 pointer.
  65. func (o *Buffer) enc_ref_int32(p *Properties, base structPointer) error {
  66. v := structPointer_RefWord32(base, p.field)
  67. if refWord32_IsNil(v) {
  68. return ErrNil
  69. }
  70. x := refWord32_Get(v)
  71. o.buf = append(o.buf, p.tagcode...)
  72. p.valEnc(o, uint64(x))
  73. return nil
  74. }
  75. func size_ref_int32(p *Properties, base structPointer) (n int) {
  76. v := structPointer_RefWord32(base, p.field)
  77. if refWord32_IsNil(v) {
  78. return 0
  79. }
  80. x := refWord32_Get(v)
  81. n += len(p.tagcode)
  82. n += p.valSize(uint64(x))
  83. return
  84. }
  85. // Encode a reference to an int64 pointer.
  86. func (o *Buffer) enc_ref_int64(p *Properties, base structPointer) error {
  87. v := structPointer_RefWord64(base, p.field)
  88. if refWord64_IsNil(v) {
  89. return ErrNil
  90. }
  91. x := refWord64_Get(v)
  92. o.buf = append(o.buf, p.tagcode...)
  93. p.valEnc(o, x)
  94. return nil
  95. }
  96. func size_ref_int64(p *Properties, base structPointer) (n int) {
  97. v := structPointer_RefWord64(base, p.field)
  98. if refWord64_IsNil(v) {
  99. return 0
  100. }
  101. x := refWord64_Get(v)
  102. n += len(p.tagcode)
  103. n += p.valSize(x)
  104. return
  105. }
  106. // Encode a reference to a string pointer.
  107. func (o *Buffer) enc_ref_string(p *Properties, base structPointer) error {
  108. v := structPointer_RefString(base, p.field)
  109. if v == nil {
  110. return ErrNil
  111. }
  112. x := *v
  113. o.buf = append(o.buf, p.tagcode...)
  114. o.EncodeStringBytes(x)
  115. return nil
  116. }
  117. func size_ref_string(p *Properties, base structPointer) (n int) {
  118. v := structPointer_RefString(base, p.field)
  119. if v == nil {
  120. return 0
  121. }
  122. x := *v
  123. n += len(p.tagcode)
  124. n += sizeStringBytes(x)
  125. return
  126. }
  127. // Encode a reference to a message struct.
  128. func (o *Buffer) enc_ref_struct_message(p *Properties, base structPointer) error {
  129. var state errorState
  130. structp := structPointer_GetRefStructPointer(base, p.field)
  131. if structPointer_IsNil(structp) {
  132. return ErrNil
  133. }
  134. // Can the object marshal itself?
  135. if p.isMarshaler {
  136. m := structPointer_Interface(structp, p.stype).(Marshaler)
  137. data, err := m.Marshal()
  138. if err != nil && !state.shouldContinue(err, nil) {
  139. return err
  140. }
  141. o.buf = append(o.buf, p.tagcode...)
  142. o.EncodeRawBytes(data)
  143. return nil
  144. }
  145. // need the length before we can write out the message itself,
  146. // so marshal into a separate byte buffer first.
  147. obuf := o.buf
  148. o.buf = o.bufalloc()
  149. err := o.enc_struct(p.stype, p.sprop, structp)
  150. nbuf := o.buf
  151. o.buf = obuf
  152. if err != nil && !state.shouldContinue(err, nil) {
  153. o.buffree(nbuf)
  154. return err
  155. }
  156. o.buf = append(o.buf, p.tagcode...)
  157. o.EncodeRawBytes(nbuf)
  158. o.buffree(nbuf)
  159. return nil
  160. }
  161. //TODO this is only copied, please fix this
  162. func size_ref_struct_message(p *Properties, base structPointer) int {
  163. structp := structPointer_GetRefStructPointer(base, p.field)
  164. if structPointer_IsNil(structp) {
  165. return 0
  166. }
  167. // Can the object marshal itself?
  168. if p.isMarshaler {
  169. m := structPointer_Interface(structp, p.stype).(Marshaler)
  170. data, _ := m.Marshal()
  171. n0 := len(p.tagcode)
  172. n1 := sizeRawBytes(data)
  173. return n0 + n1
  174. }
  175. n0 := len(p.tagcode)
  176. n1 := size_struct(p.stype, p.sprop, structp)
  177. n2 := sizeVarint(uint64(n1)) // size of encoded length
  178. return n0 + n1 + n2
  179. }
  180. // Encode a slice of references to message struct pointers ([]struct).
  181. func (o *Buffer) enc_slice_ref_struct_message(p *Properties, base structPointer) error {
  182. var state errorState
  183. ss := structPointer_GetStructPointer(base, p.field)
  184. ss1 := structPointer_GetRefStructPointer(ss, field(0))
  185. size := p.stype.Size()
  186. l := structPointer_Len(base, p.field)
  187. for i := 0; i < l; i++ {
  188. structp := structPointer_Add(ss1, field(uintptr(i)*size))
  189. if structPointer_IsNil(structp) {
  190. return ErrRepeatedHasNil
  191. }
  192. // Can the object marshal itself?
  193. if p.isMarshaler {
  194. m := structPointer_Interface(structp, p.stype).(Marshaler)
  195. data, err := m.Marshal()
  196. if err != nil && !state.shouldContinue(err, nil) {
  197. return err
  198. }
  199. o.buf = append(o.buf, p.tagcode...)
  200. o.EncodeRawBytes(data)
  201. continue
  202. }
  203. obuf := o.buf
  204. o.buf = o.bufalloc()
  205. err := o.enc_struct(p.stype, p.sprop, structp)
  206. nbuf := o.buf
  207. o.buf = obuf
  208. if err != nil && !state.shouldContinue(err, nil) {
  209. o.buffree(nbuf)
  210. if err == ErrNil {
  211. return ErrRepeatedHasNil
  212. }
  213. return err
  214. }
  215. o.buf = append(o.buf, p.tagcode...)
  216. o.EncodeRawBytes(nbuf)
  217. o.buffree(nbuf)
  218. }
  219. return nil
  220. }
  221. //TODO this is only copied, please fix this
  222. func size_slice_ref_struct_message(p *Properties, base structPointer) (n int) {
  223. ss := structPointer_GetStructPointer(base, p.field)
  224. ss1 := structPointer_GetRefStructPointer(ss, field(0))
  225. size := p.stype.Size()
  226. l := structPointer_Len(base, p.field)
  227. n += l * len(p.tagcode)
  228. for i := 0; i < l; i++ {
  229. structp := structPointer_Add(ss1, field(uintptr(i)*size))
  230. if structPointer_IsNil(structp) {
  231. return // return the size up to this point
  232. }
  233. // Can the object marshal itself?
  234. if p.isMarshaler {
  235. m := structPointer_Interface(structp, p.stype).(Marshaler)
  236. data, _ := m.Marshal()
  237. n += len(p.tagcode)
  238. n += sizeRawBytes(data)
  239. continue
  240. }
  241. n0 := size_struct(p.stype, p.sprop, structp)
  242. n1 := sizeVarint(uint64(n0)) // size of encoded length
  243. n += n0 + n1
  244. }
  245. return
  246. }
  247. func (o *Buffer) enc_custom_bytes(p *Properties, base structPointer) error {
  248. i := structPointer_InterfaceRef(base, p.field, p.ctype)
  249. if i == nil {
  250. return ErrNil
  251. }
  252. custom := i.(Marshaler)
  253. data, err := custom.Marshal()
  254. if err != nil {
  255. return err
  256. }
  257. if data == nil {
  258. return ErrNil
  259. }
  260. o.buf = append(o.buf, p.tagcode...)
  261. o.EncodeRawBytes(data)
  262. return nil
  263. }
  264. func size_custom_bytes(p *Properties, base structPointer) (n int) {
  265. n += len(p.tagcode)
  266. i := structPointer_InterfaceRef(base, p.field, p.ctype)
  267. if i == nil {
  268. return 0
  269. }
  270. custom := i.(Marshaler)
  271. data, _ := custom.Marshal()
  272. n += sizeRawBytes(data)
  273. return
  274. }
  275. func (o *Buffer) enc_custom_ref_bytes(p *Properties, base structPointer) error {
  276. custom := structPointer_InterfaceAt(base, p.field, p.ctype).(Marshaler)
  277. data, err := custom.Marshal()
  278. if err != nil {
  279. return err
  280. }
  281. if data == nil {
  282. return ErrNil
  283. }
  284. o.buf = append(o.buf, p.tagcode...)
  285. o.EncodeRawBytes(data)
  286. return nil
  287. }
  288. func size_custom_ref_bytes(p *Properties, base structPointer) (n int) {
  289. n += len(p.tagcode)
  290. i := structPointer_InterfaceAt(base, p.field, p.ctype)
  291. if i == nil {
  292. return 0
  293. }
  294. custom := i.(Marshaler)
  295. data, _ := custom.Marshal()
  296. n += sizeRawBytes(data)
  297. return
  298. }
  299. func (o *Buffer) enc_custom_slice_bytes(p *Properties, base structPointer) error {
  300. inter := structPointer_InterfaceRef(base, p.field, p.ctype)
  301. if inter == nil {
  302. return ErrNil
  303. }
  304. slice := reflect.ValueOf(inter)
  305. l := slice.Len()
  306. for i := 0; i < l; i++ {
  307. v := slice.Index(i)
  308. custom := v.Interface().(Marshaler)
  309. data, err := custom.Marshal()
  310. if err != nil {
  311. return err
  312. }
  313. o.buf = append(o.buf, p.tagcode...)
  314. o.EncodeRawBytes(data)
  315. }
  316. return nil
  317. }
  318. func size_custom_slice_bytes(p *Properties, base structPointer) (n int) {
  319. inter := structPointer_InterfaceRef(base, p.field, p.ctype)
  320. if inter == nil {
  321. return 0
  322. }
  323. slice := reflect.ValueOf(inter)
  324. l := slice.Len()
  325. n += l * len(p.tagcode)
  326. for i := 0; i < l; i++ {
  327. v := slice.Index(i)
  328. custom := v.Interface().(Marshaler)
  329. data, _ := custom.Marshal()
  330. n += sizeRawBytes(data)
  331. }
  332. return
  333. }