PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/gogo/protobuf/proto/encode_gogo.go

https://gitlab.com/JamesClonk/firehose-to-syslog
Go | 383 lines | 302 code | 34 blank | 47 comment | 62 complexity | 4b8f4b0f7330190aac54483d6ee3b909 MD5 | raw file
  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. 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 := int32(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 := int32(refWord32_Get(v))
  97. n += len(p.tagcode)
  98. n += p.valSize(uint64(x))
  99. return
  100. }
  101. func (o *Buffer) enc_ref_uint32(p *Properties, base structPointer) error {
  102. v := structPointer_RefWord32(base, p.field)
  103. if refWord32_IsNil(v) {
  104. return ErrNil
  105. }
  106. x := refWord32_Get(v)
  107. o.buf = append(o.buf, p.tagcode...)
  108. p.valEnc(o, uint64(x))
  109. return nil
  110. }
  111. func size_ref_uint32(p *Properties, base structPointer) (n int) {
  112. v := structPointer_RefWord32(base, p.field)
  113. if refWord32_IsNil(v) {
  114. return 0
  115. }
  116. x := refWord32_Get(v)
  117. n += len(p.tagcode)
  118. n += p.valSize(uint64(x))
  119. return
  120. }
  121. // Encode a reference to an int64 pointer.
  122. func (o *Buffer) enc_ref_int64(p *Properties, base structPointer) error {
  123. v := structPointer_RefWord64(base, p.field)
  124. if refWord64_IsNil(v) {
  125. return ErrNil
  126. }
  127. x := refWord64_Get(v)
  128. o.buf = append(o.buf, p.tagcode...)
  129. p.valEnc(o, x)
  130. return nil
  131. }
  132. func size_ref_int64(p *Properties, base structPointer) (n int) {
  133. v := structPointer_RefWord64(base, p.field)
  134. if refWord64_IsNil(v) {
  135. return 0
  136. }
  137. x := refWord64_Get(v)
  138. n += len(p.tagcode)
  139. n += p.valSize(x)
  140. return
  141. }
  142. // Encode a reference to a string pointer.
  143. func (o *Buffer) enc_ref_string(p *Properties, base structPointer) error {
  144. v := structPointer_RefString(base, p.field)
  145. if v == nil {
  146. return ErrNil
  147. }
  148. x := *v
  149. o.buf = append(o.buf, p.tagcode...)
  150. o.EncodeStringBytes(x)
  151. return nil
  152. }
  153. func size_ref_string(p *Properties, base structPointer) (n int) {
  154. v := structPointer_RefString(base, p.field)
  155. if v == nil {
  156. return 0
  157. }
  158. x := *v
  159. n += len(p.tagcode)
  160. n += sizeStringBytes(x)
  161. return
  162. }
  163. // Encode a reference to a message struct.
  164. func (o *Buffer) enc_ref_struct_message(p *Properties, base structPointer) error {
  165. var state errorState
  166. structp := structPointer_GetRefStructPointer(base, p.field)
  167. if structPointer_IsNil(structp) {
  168. return ErrNil
  169. }
  170. // Can the object marshal itself?
  171. if p.isMarshaler {
  172. m := structPointer_Interface(structp, p.stype).(Marshaler)
  173. data, err := m.Marshal()
  174. if err != nil && !state.shouldContinue(err, nil) {
  175. return err
  176. }
  177. o.buf = append(o.buf, p.tagcode...)
  178. o.EncodeRawBytes(data)
  179. return nil
  180. }
  181. o.buf = append(o.buf, p.tagcode...)
  182. return o.enc_len_struct(p.sprop, structp, &state)
  183. }
  184. //TODO this is only copied, please fix this
  185. func size_ref_struct_message(p *Properties, base structPointer) int {
  186. structp := structPointer_GetRefStructPointer(base, p.field)
  187. if structPointer_IsNil(structp) {
  188. return 0
  189. }
  190. // Can the object marshal itself?
  191. if p.isMarshaler {
  192. m := structPointer_Interface(structp, p.stype).(Marshaler)
  193. data, _ := m.Marshal()
  194. n0 := len(p.tagcode)
  195. n1 := sizeRawBytes(data)
  196. return n0 + n1
  197. }
  198. n0 := len(p.tagcode)
  199. n1 := size_struct(p.sprop, structp)
  200. n2 := sizeVarint(uint64(n1)) // size of encoded length
  201. return n0 + n1 + n2
  202. }
  203. // Encode a slice of references to message struct pointers ([]struct).
  204. func (o *Buffer) enc_slice_ref_struct_message(p *Properties, base structPointer) error {
  205. var state errorState
  206. ss := structPointer_GetStructPointer(base, p.field)
  207. ss1 := structPointer_GetRefStructPointer(ss, field(0))
  208. size := p.stype.Size()
  209. l := structPointer_Len(base, p.field)
  210. for i := 0; i < l; i++ {
  211. structp := structPointer_Add(ss1, field(uintptr(i)*size))
  212. if structPointer_IsNil(structp) {
  213. return ErrRepeatedHasNil
  214. }
  215. // Can the object marshal itself?
  216. if p.isMarshaler {
  217. m := structPointer_Interface(structp, p.stype).(Marshaler)
  218. data, err := m.Marshal()
  219. if err != nil && !state.shouldContinue(err, nil) {
  220. return err
  221. }
  222. o.buf = append(o.buf, p.tagcode...)
  223. o.EncodeRawBytes(data)
  224. continue
  225. }
  226. o.buf = append(o.buf, p.tagcode...)
  227. err := o.enc_len_struct(p.sprop, structp, &state)
  228. if err != nil && !state.shouldContinue(err, nil) {
  229. if err == ErrNil {
  230. return ErrRepeatedHasNil
  231. }
  232. return err
  233. }
  234. }
  235. return state.err
  236. }
  237. //TODO this is only copied, please fix this
  238. func size_slice_ref_struct_message(p *Properties, base structPointer) (n int) {
  239. ss := structPointer_GetStructPointer(base, p.field)
  240. ss1 := structPointer_GetRefStructPointer(ss, field(0))
  241. size := p.stype.Size()
  242. l := structPointer_Len(base, p.field)
  243. n += l * len(p.tagcode)
  244. for i := 0; i < l; i++ {
  245. structp := structPointer_Add(ss1, field(uintptr(i)*size))
  246. if structPointer_IsNil(structp) {
  247. return // return the size up to this point
  248. }
  249. // Can the object marshal itself?
  250. if p.isMarshaler {
  251. m := structPointer_Interface(structp, p.stype).(Marshaler)
  252. data, _ := m.Marshal()
  253. n += len(p.tagcode)
  254. n += sizeRawBytes(data)
  255. continue
  256. }
  257. n0 := size_struct(p.sprop, structp)
  258. n1 := sizeVarint(uint64(n0)) // size of encoded length
  259. n += n0 + n1
  260. }
  261. return
  262. }
  263. func (o *Buffer) enc_custom_bytes(p *Properties, base structPointer) error {
  264. i := structPointer_InterfaceRef(base, p.field, p.ctype)
  265. if i == nil {
  266. return ErrNil
  267. }
  268. custom := i.(Marshaler)
  269. data, err := custom.Marshal()
  270. if err != nil {
  271. return err
  272. }
  273. if data == nil {
  274. return ErrNil
  275. }
  276. o.buf = append(o.buf, p.tagcode...)
  277. o.EncodeRawBytes(data)
  278. return nil
  279. }
  280. func size_custom_bytes(p *Properties, base structPointer) (n int) {
  281. n += len(p.tagcode)
  282. i := structPointer_InterfaceRef(base, p.field, p.ctype)
  283. if i == nil {
  284. return 0
  285. }
  286. custom := i.(Marshaler)
  287. data, _ := custom.Marshal()
  288. n += sizeRawBytes(data)
  289. return
  290. }
  291. func (o *Buffer) enc_custom_ref_bytes(p *Properties, base structPointer) error {
  292. custom := structPointer_InterfaceAt(base, p.field, p.ctype).(Marshaler)
  293. data, err := custom.Marshal()
  294. if err != nil {
  295. return err
  296. }
  297. if data == nil {
  298. return ErrNil
  299. }
  300. o.buf = append(o.buf, p.tagcode...)
  301. o.EncodeRawBytes(data)
  302. return nil
  303. }
  304. func size_custom_ref_bytes(p *Properties, base structPointer) (n int) {
  305. n += len(p.tagcode)
  306. i := structPointer_InterfaceAt(base, p.field, p.ctype)
  307. if i == nil {
  308. return 0
  309. }
  310. custom := i.(Marshaler)
  311. data, _ := custom.Marshal()
  312. n += sizeRawBytes(data)
  313. return
  314. }
  315. func (o *Buffer) enc_custom_slice_bytes(p *Properties, base structPointer) error {
  316. inter := structPointer_InterfaceRef(base, p.field, p.ctype)
  317. if inter == nil {
  318. return ErrNil
  319. }
  320. slice := reflect.ValueOf(inter)
  321. l := slice.Len()
  322. for i := 0; i < l; i++ {
  323. v := slice.Index(i)
  324. custom := v.Interface().(Marshaler)
  325. data, err := custom.Marshal()
  326. if err != nil {
  327. return err
  328. }
  329. o.buf = append(o.buf, p.tagcode...)
  330. o.EncodeRawBytes(data)
  331. }
  332. return nil
  333. }
  334. func size_custom_slice_bytes(p *Properties, base structPointer) (n int) {
  335. inter := structPointer_InterfaceRef(base, p.field, p.ctype)
  336. if inter == nil {
  337. return 0
  338. }
  339. slice := reflect.ValueOf(inter)
  340. l := slice.Len()
  341. n += l * len(p.tagcode)
  342. for i := 0; i < l; i++ {
  343. v := slice.Index(i)
  344. custom := v.Interface().(Marshaler)
  345. data, _ := custom.Marshal()
  346. n += sizeRawBytes(data)
  347. }
  348. return
  349. }