encode_gogo.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. func (o *Buffer) enc_ext_slice_byte(p *Properties, base structPointer) error {
  44. s := *structPointer_Bytes(base, p.field)
  45. if s == nil {
  46. return ErrNil
  47. }
  48. o.buf = append(o.buf, s...)
  49. return nil
  50. }
  51. func size_ext_slice_byte(p *Properties, base structPointer) (n int) {
  52. s := *structPointer_Bytes(base, p.field)
  53. if s == nil {
  54. return 0
  55. }
  56. n += len(s)
  57. return
  58. }
  59. // Encode a reference to bool pointer.
  60. func (o *Buffer) enc_ref_bool(p *Properties, base structPointer) error {
  61. v := structPointer_RefBool(base, p.field)
  62. if v == nil {
  63. return ErrNil
  64. }
  65. x := 0
  66. if *v {
  67. x = 1
  68. }
  69. o.buf = append(o.buf, p.tagcode...)
  70. p.valEnc(o, uint64(x))
  71. return nil
  72. }
  73. func size_ref_bool(p *Properties, base structPointer) int {
  74. v := structPointer_RefBool(base, p.field)
  75. if v == nil {
  76. return 0
  77. }
  78. return len(p.tagcode) + 1 // each bool takes exactly one byte
  79. }
  80. // Encode a reference to int32 pointer.
  81. func (o *Buffer) enc_ref_int32(p *Properties, base structPointer) error {
  82. v := structPointer_RefWord32(base, p.field)
  83. if refWord32_IsNil(v) {
  84. return ErrNil
  85. }
  86. x := refWord32_Get(v)
  87. o.buf = append(o.buf, p.tagcode...)
  88. p.valEnc(o, uint64(x))
  89. return nil
  90. }
  91. func size_ref_int32(p *Properties, base structPointer) (n int) {
  92. v := structPointer_RefWord32(base, p.field)
  93. if refWord32_IsNil(v) {
  94. return 0
  95. }
  96. x := refWord32_Get(v)
  97. n += len(p.tagcode)
  98. n += p.valSize(uint64(x))
  99. return
  100. }
  101. // Encode a reference to an int64 pointer.
  102. func (o *Buffer) enc_ref_int64(p *Properties, base structPointer) error {
  103. v := structPointer_RefWord64(base, p.field)
  104. if refWord64_IsNil(v) {
  105. return ErrNil
  106. }
  107. x := refWord64_Get(v)
  108. o.buf = append(o.buf, p.tagcode...)
  109. p.valEnc(o, x)
  110. return nil
  111. }
  112. func size_ref_int64(p *Properties, base structPointer) (n int) {
  113. v := structPointer_RefWord64(base, p.field)
  114. if refWord64_IsNil(v) {
  115. return 0
  116. }
  117. x := refWord64_Get(v)
  118. n += len(p.tagcode)
  119. n += p.valSize(x)
  120. return
  121. }
  122. // Encode a reference to a string pointer.
  123. func (o *Buffer) enc_ref_string(p *Properties, base structPointer) error {
  124. v := structPointer_RefString(base, p.field)
  125. if v == nil {
  126. return ErrNil
  127. }
  128. x := *v
  129. o.buf = append(o.buf, p.tagcode...)
  130. o.EncodeStringBytes(x)
  131. return nil
  132. }
  133. func size_ref_string(p *Properties, base structPointer) (n int) {
  134. v := structPointer_RefString(base, p.field)
  135. if v == nil {
  136. return 0
  137. }
  138. x := *v
  139. n += len(p.tagcode)
  140. n += sizeStringBytes(x)
  141. return
  142. }
  143. // Encode a reference to a message struct.
  144. func (o *Buffer) enc_ref_struct_message(p *Properties, base structPointer) error {
  145. var state errorState
  146. structp := structPointer_GetRefStructPointer(base, p.field)
  147. if structPointer_IsNil(structp) {
  148. return ErrNil
  149. }
  150. // Can the object marshal itself?
  151. if p.isMarshaler {
  152. m := structPointer_Interface(structp, p.stype).(Marshaler)
  153. data, err := m.Marshal()
  154. if err != nil && !state.shouldContinue(err, nil) {
  155. return err
  156. }
  157. o.buf = append(o.buf, p.tagcode...)
  158. o.EncodeRawBytes(data)
  159. return nil
  160. }
  161. o.buf = append(o.buf, p.tagcode...)
  162. return o.enc_len_struct(p.stype, p.sprop, structp, &state)
  163. }
  164. //TODO this is only copied, please fix this
  165. func size_ref_struct_message(p *Properties, base structPointer) int {
  166. structp := structPointer_GetRefStructPointer(base, p.field)
  167. if structPointer_IsNil(structp) {
  168. return 0
  169. }
  170. // Can the object marshal itself?
  171. if p.isMarshaler {
  172. m := structPointer_Interface(structp, p.stype).(Marshaler)
  173. data, _ := m.Marshal()
  174. n0 := len(p.tagcode)
  175. n1 := sizeRawBytes(data)
  176. return n0 + n1
  177. }
  178. n0 := len(p.tagcode)
  179. n1 := size_struct(p.stype, p.sprop, structp)
  180. n2 := sizeVarint(uint64(n1)) // size of encoded length
  181. return n0 + n1 + n2
  182. }
  183. // Encode a slice of references to message struct pointers ([]struct).
  184. func (o *Buffer) enc_slice_ref_struct_message(p *Properties, base structPointer) error {
  185. var state errorState
  186. ss := structPointer_GetStructPointer(base, p.field)
  187. ss1 := structPointer_GetRefStructPointer(ss, field(0))
  188. size := p.stype.Size()
  189. l := structPointer_Len(base, p.field)
  190. for i := 0; i < l; i++ {
  191. structp := structPointer_Add(ss1, field(uintptr(i)*size))
  192. if structPointer_IsNil(structp) {
  193. return ErrRepeatedHasNil
  194. }
  195. // Can the object marshal itself?
  196. if p.isMarshaler {
  197. m := structPointer_Interface(structp, p.stype).(Marshaler)
  198. data, err := m.Marshal()
  199. if err != nil && !state.shouldContinue(err, nil) {
  200. return err
  201. }
  202. o.buf = append(o.buf, p.tagcode...)
  203. o.EncodeRawBytes(data)
  204. continue
  205. }
  206. o.buf = append(o.buf, p.tagcode...)
  207. err := o.enc_len_struct(p.stype, p.sprop, structp, &state)
  208. if err != nil && !state.shouldContinue(err, nil) {
  209. if err == ErrNil {
  210. return ErrRepeatedHasNil
  211. }
  212. return err
  213. }
  214. }
  215. return state.err
  216. }
  217. //TODO this is only copied, please fix this
  218. func size_slice_ref_struct_message(p *Properties, base structPointer) (n int) {
  219. ss := structPointer_GetStructPointer(base, p.field)
  220. ss1 := structPointer_GetRefStructPointer(ss, field(0))
  221. size := p.stype.Size()
  222. l := structPointer_Len(base, p.field)
  223. n += l * len(p.tagcode)
  224. for i := 0; i < l; i++ {
  225. structp := structPointer_Add(ss1, field(uintptr(i)*size))
  226. if structPointer_IsNil(structp) {
  227. return // return the size up to this point
  228. }
  229. // Can the object marshal itself?
  230. if p.isMarshaler {
  231. m := structPointer_Interface(structp, p.stype).(Marshaler)
  232. data, _ := m.Marshal()
  233. n += len(p.tagcode)
  234. n += sizeRawBytes(data)
  235. continue
  236. }
  237. n0 := size_struct(p.stype, p.sprop, structp)
  238. n1 := sizeVarint(uint64(n0)) // size of encoded length
  239. n += n0 + n1
  240. }
  241. return
  242. }
  243. func (o *Buffer) enc_custom_bytes(p *Properties, base structPointer) error {
  244. i := structPointer_InterfaceRef(base, p.field, p.ctype)
  245. if i == nil {
  246. return ErrNil
  247. }
  248. custom := i.(Marshaler)
  249. data, err := custom.Marshal()
  250. if err != nil {
  251. return err
  252. }
  253. if data == nil {
  254. return ErrNil
  255. }
  256. o.buf = append(o.buf, p.tagcode...)
  257. o.EncodeRawBytes(data)
  258. return nil
  259. }
  260. func size_custom_bytes(p *Properties, base structPointer) (n int) {
  261. n += len(p.tagcode)
  262. i := structPointer_InterfaceRef(base, p.field, p.ctype)
  263. if i == nil {
  264. return 0
  265. }
  266. custom := i.(Marshaler)
  267. data, _ := custom.Marshal()
  268. n += sizeRawBytes(data)
  269. return
  270. }
  271. func (o *Buffer) enc_custom_ref_bytes(p *Properties, base structPointer) error {
  272. custom := structPointer_InterfaceAt(base, p.field, p.ctype).(Marshaler)
  273. data, err := custom.Marshal()
  274. if err != nil {
  275. return err
  276. }
  277. if data == nil {
  278. return ErrNil
  279. }
  280. o.buf = append(o.buf, p.tagcode...)
  281. o.EncodeRawBytes(data)
  282. return nil
  283. }
  284. func size_custom_ref_bytes(p *Properties, base structPointer) (n int) {
  285. n += len(p.tagcode)
  286. i := structPointer_InterfaceAt(base, p.field, p.ctype)
  287. if i == nil {
  288. return 0
  289. }
  290. custom := i.(Marshaler)
  291. data, _ := custom.Marshal()
  292. n += sizeRawBytes(data)
  293. return
  294. }
  295. func (o *Buffer) enc_custom_slice_bytes(p *Properties, base structPointer) error {
  296. inter := structPointer_InterfaceRef(base, p.field, p.ctype)
  297. if inter == nil {
  298. return ErrNil
  299. }
  300. slice := reflect.ValueOf(inter)
  301. l := slice.Len()
  302. for i := 0; i < l; i++ {
  303. v := slice.Index(i)
  304. custom := v.Interface().(Marshaler)
  305. data, err := custom.Marshal()
  306. if err != nil {
  307. return err
  308. }
  309. o.buf = append(o.buf, p.tagcode...)
  310. o.EncodeRawBytes(data)
  311. }
  312. return nil
  313. }
  314. func size_custom_slice_bytes(p *Properties, base structPointer) (n int) {
  315. inter := structPointer_InterfaceRef(base, p.field, p.ctype)
  316. if inter == nil {
  317. return 0
  318. }
  319. slice := reflect.ValueOf(inter)
  320. l := slice.Len()
  321. n += l * len(p.tagcode)
  322. for i := 0; i < l; i++ {
  323. v := slice.Index(i)
  324. custom := v.Interface().(Marshaler)
  325. data, _ := custom.Marshal()
  326. n += sizeRawBytes(data)
  327. }
  328. return
  329. }