encode_gogo.go 9.5 KB

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