encode_gogo.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  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_StructRefSlice(base, p.field, p.stype.Size())
  177. l := ss.Len()
  178. for i := 0; i < l; i++ {
  179. structp := ss.Index(i)
  180. if structPointer_IsNil(structp) {
  181. return errRepeatedHasNil
  182. }
  183. // Can the object marshal itself?
  184. if p.isMarshaler {
  185. m := structPointer_Interface(structp, p.stype).(Marshaler)
  186. data, err := m.Marshal()
  187. if err != nil && !state.shouldContinue(err, nil) {
  188. return err
  189. }
  190. o.buf = append(o.buf, p.tagcode...)
  191. o.EncodeRawBytes(data)
  192. continue
  193. }
  194. o.buf = append(o.buf, p.tagcode...)
  195. err := o.enc_len_struct(p.sprop, structp, &state)
  196. if err != nil && !state.shouldContinue(err, nil) {
  197. if err == ErrNil {
  198. return errRepeatedHasNil
  199. }
  200. return err
  201. }
  202. }
  203. return state.err
  204. }
  205. //TODO this is only copied, please fix this
  206. func size_slice_ref_struct_message(p *Properties, base structPointer) (n int) {
  207. ss := structPointer_StructRefSlice(base, p.field, p.stype.Size())
  208. l := ss.Len()
  209. n += l * len(p.tagcode)
  210. for i := 0; i < l; i++ {
  211. structp := ss.Index(i)
  212. if structPointer_IsNil(structp) {
  213. return // return the size up to this point
  214. }
  215. // Can the object marshal itself?
  216. if p.isMarshaler {
  217. m := structPointer_Interface(structp, p.stype).(Marshaler)
  218. data, _ := m.Marshal()
  219. n += len(p.tagcode)
  220. n += sizeRawBytes(data)
  221. continue
  222. }
  223. n0 := size_struct(p.sprop, structp)
  224. n1 := sizeVarint(uint64(n0)) // size of encoded length
  225. n += n0 + n1
  226. }
  227. return
  228. }
  229. func (o *Buffer) enc_custom_bytes(p *Properties, base structPointer) error {
  230. i := structPointer_InterfaceRef(base, p.field, p.ctype)
  231. if i == nil {
  232. return ErrNil
  233. }
  234. custom := i.(Marshaler)
  235. data, err := custom.Marshal()
  236. if err != nil {
  237. return err
  238. }
  239. if data == nil {
  240. return ErrNil
  241. }
  242. o.buf = append(o.buf, p.tagcode...)
  243. o.EncodeRawBytes(data)
  244. return nil
  245. }
  246. func size_custom_bytes(p *Properties, base structPointer) (n int) {
  247. n += len(p.tagcode)
  248. i := structPointer_InterfaceRef(base, p.field, p.ctype)
  249. if i == nil {
  250. return 0
  251. }
  252. custom := i.(Marshaler)
  253. data, _ := custom.Marshal()
  254. n += sizeRawBytes(data)
  255. return
  256. }
  257. func (o *Buffer) enc_custom_ref_bytes(p *Properties, base structPointer) error {
  258. custom := structPointer_InterfaceAt(base, p.field, p.ctype).(Marshaler)
  259. data, err := custom.Marshal()
  260. if err != nil {
  261. return err
  262. }
  263. if data == nil {
  264. return ErrNil
  265. }
  266. o.buf = append(o.buf, p.tagcode...)
  267. o.EncodeRawBytes(data)
  268. return nil
  269. }
  270. func size_custom_ref_bytes(p *Properties, base structPointer) (n int) {
  271. n += len(p.tagcode)
  272. i := structPointer_InterfaceAt(base, p.field, p.ctype)
  273. if i == nil {
  274. return 0
  275. }
  276. custom := i.(Marshaler)
  277. data, _ := custom.Marshal()
  278. n += sizeRawBytes(data)
  279. return
  280. }
  281. func (o *Buffer) enc_custom_slice_bytes(p *Properties, base structPointer) error {
  282. inter := structPointer_InterfaceRef(base, p.field, p.ctype)
  283. if inter == nil {
  284. return ErrNil
  285. }
  286. slice := reflect.ValueOf(inter)
  287. l := slice.Len()
  288. for i := 0; i < l; i++ {
  289. v := slice.Index(i)
  290. custom := v.Interface().(Marshaler)
  291. data, err := custom.Marshal()
  292. if err != nil {
  293. return err
  294. }
  295. o.buf = append(o.buf, p.tagcode...)
  296. o.EncodeRawBytes(data)
  297. }
  298. return nil
  299. }
  300. func size_custom_slice_bytes(p *Properties, base structPointer) (n int) {
  301. inter := structPointer_InterfaceRef(base, p.field, p.ctype)
  302. if inter == nil {
  303. return 0
  304. }
  305. slice := reflect.ValueOf(inter)
  306. l := slice.Len()
  307. n += l * len(p.tagcode)
  308. for i := 0; i < l; i++ {
  309. v := slice.Index(i)
  310. custom := v.Interface().(Marshaler)
  311. data, _ := custom.Marshal()
  312. n += sizeRawBytes(data)
  313. }
  314. return
  315. }