table_marshal_gogo.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2018, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. package proto
  29. import (
  30. "reflect"
  31. "time"
  32. )
  33. // makeMessageRefMarshaler differs a bit from makeMessageMarshaler
  34. // It marshal a message T instead of a *T
  35. func makeMessageRefMarshaler(u *marshalInfo) (sizer, marshaler) {
  36. return func(ptr pointer, tagsize int) int {
  37. siz := u.size(ptr)
  38. return siz + SizeVarint(uint64(siz)) + tagsize
  39. },
  40. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  41. b = appendVarint(b, wiretag)
  42. siz := u.cachedsize(ptr)
  43. b = appendVarint(b, uint64(siz))
  44. return u.marshal(b, ptr, deterministic)
  45. }
  46. }
  47. // makeMessageRefSliceMarshaler differs quite a lot from makeMessageSliceMarshaler
  48. // It marshals a slice of messages []T instead of []*T
  49. func makeMessageRefSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  50. return func(ptr pointer, tagsize int) int {
  51. s := ptr.getSlice(u.typ)
  52. n := 0
  53. for i := 0; i < s.Len(); i++ {
  54. elem := s.Index(i)
  55. e := elem.Interface()
  56. v := toAddrPointer(&e, false)
  57. siz := u.size(v)
  58. n += siz + SizeVarint(uint64(siz)) + tagsize
  59. }
  60. return n
  61. },
  62. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  63. s := ptr.getSlice(u.typ)
  64. var err, errreq error
  65. for i := 0; i < s.Len(); i++ {
  66. elem := s.Index(i)
  67. e := elem.Interface()
  68. v := toAddrPointer(&e, false)
  69. b = appendVarint(b, wiretag)
  70. siz := u.size(v)
  71. b = appendVarint(b, uint64(siz))
  72. b, err = u.marshal(b, v, deterministic)
  73. if err != nil {
  74. if _, ok := err.(*RequiredNotSetError); ok {
  75. // Required field in submessage is not set.
  76. // We record the error but keep going, to give a complete marshaling.
  77. if errreq == nil {
  78. errreq = err
  79. }
  80. continue
  81. }
  82. if err == ErrNil {
  83. err = errRepeatedHasNil
  84. }
  85. return b, err
  86. }
  87. }
  88. return b, errreq
  89. }
  90. }
  91. func makeCustomPtrMarshaler(u *marshalInfo) (sizer, marshaler) {
  92. return func(ptr pointer, tagsize int) int {
  93. if ptr.isNil() {
  94. return 0
  95. }
  96. m := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(custom)
  97. siz := m.Size()
  98. return tagsize + SizeVarint(uint64(siz)) + siz
  99. }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  100. if ptr.isNil() {
  101. return b, nil
  102. }
  103. m := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(custom)
  104. siz := m.Size()
  105. buf, err := m.Marshal()
  106. if err != nil {
  107. return nil, err
  108. }
  109. b = appendVarint(b, wiretag)
  110. b = appendVarint(b, uint64(siz))
  111. b = append(b, buf...)
  112. return b, nil
  113. }
  114. }
  115. func makeCustomMarshaler(u *marshalInfo) (sizer, marshaler) {
  116. return func(ptr pointer, tagsize int) int {
  117. m := ptr.asPointerTo(u.typ).Interface().(custom)
  118. siz := m.Size()
  119. return tagsize + SizeVarint(uint64(siz)) + siz
  120. }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  121. m := ptr.asPointerTo(u.typ).Interface().(custom)
  122. siz := m.Size()
  123. buf, err := m.Marshal()
  124. if err != nil {
  125. return nil, err
  126. }
  127. b = appendVarint(b, wiretag)
  128. b = appendVarint(b, uint64(siz))
  129. b = append(b, buf...)
  130. return b, nil
  131. }
  132. }
  133. func makeTimeMarshaler(u *marshalInfo) (sizer, marshaler) {
  134. return func(ptr pointer, tagsize int) int {
  135. t := ptr.asPointerTo(u.typ).Interface().(*time.Time)
  136. ts, err := timestampProto(*t)
  137. if err != nil {
  138. return 0
  139. }
  140. siz := Size(ts)
  141. return tagsize + SizeVarint(uint64(siz)) + siz
  142. }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  143. t := ptr.asPointerTo(u.typ).Interface().(*time.Time)
  144. ts, err := timestampProto(*t)
  145. if err != nil {
  146. return nil, err
  147. }
  148. buf, err := Marshal(ts)
  149. if err != nil {
  150. return nil, err
  151. }
  152. b = appendVarint(b, wiretag)
  153. b = appendVarint(b, uint64(len(buf)))
  154. b = append(b, buf...)
  155. return b, nil
  156. }
  157. }
  158. func makeTimePtrMarshaler(u *marshalInfo) (sizer, marshaler) {
  159. return func(ptr pointer, tagsize int) int {
  160. if ptr.isNil() {
  161. return 0
  162. }
  163. t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*time.Time)
  164. ts, err := timestampProto(*t)
  165. if err != nil {
  166. return 0
  167. }
  168. siz := Size(ts)
  169. return tagsize + SizeVarint(uint64(siz)) + siz
  170. }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  171. if ptr.isNil() {
  172. return b, nil
  173. }
  174. t := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*time.Time)
  175. ts, err := timestampProto(*t)
  176. if err != nil {
  177. return nil, err
  178. }
  179. buf, err := Marshal(ts)
  180. if err != nil {
  181. return nil, err
  182. }
  183. b = appendVarint(b, wiretag)
  184. b = appendVarint(b, uint64(len(buf)))
  185. b = append(b, buf...)
  186. return b, nil
  187. }
  188. }
  189. func makeTimeSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  190. return func(ptr pointer, tagsize int) int {
  191. s := ptr.getSlice(u.typ)
  192. n := 0
  193. for i := 0; i < s.Len(); i++ {
  194. elem := s.Index(i)
  195. t := elem.Interface().(time.Time)
  196. ts, err := timestampProto(t)
  197. if err != nil {
  198. return 0
  199. }
  200. siz := Size(ts)
  201. n += siz + SizeVarint(uint64(siz)) + tagsize
  202. }
  203. return n
  204. },
  205. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  206. s := ptr.getSlice(u.typ)
  207. for i := 0; i < s.Len(); i++ {
  208. elem := s.Index(i)
  209. t := elem.Interface().(time.Time)
  210. ts, err := timestampProto(t)
  211. if err != nil {
  212. return nil, err
  213. }
  214. siz := Size(ts)
  215. buf, err := Marshal(ts)
  216. if err != nil {
  217. return nil, err
  218. }
  219. b = appendVarint(b, wiretag)
  220. b = appendVarint(b, uint64(siz))
  221. b = append(b, buf...)
  222. }
  223. return b, nil
  224. }
  225. }
  226. func makeTimePtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  227. return func(ptr pointer, tagsize int) int {
  228. s := ptr.getSlice(reflect.PtrTo(u.typ))
  229. n := 0
  230. for i := 0; i < s.Len(); i++ {
  231. elem := s.Index(i)
  232. t := elem.Interface().(*time.Time)
  233. ts, err := timestampProto(*t)
  234. if err != nil {
  235. return 0
  236. }
  237. siz := Size(ts)
  238. n += siz + SizeVarint(uint64(siz)) + tagsize
  239. }
  240. return n
  241. },
  242. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  243. s := ptr.getSlice(reflect.PtrTo(u.typ))
  244. for i := 0; i < s.Len(); i++ {
  245. elem := s.Index(i)
  246. t := elem.Interface().(*time.Time)
  247. ts, err := timestampProto(*t)
  248. if err != nil {
  249. return nil, err
  250. }
  251. siz := Size(ts)
  252. buf, err := Marshal(ts)
  253. if err != nil {
  254. return nil, err
  255. }
  256. b = appendVarint(b, wiretag)
  257. b = appendVarint(b, uint64(siz))
  258. b = append(b, buf...)
  259. }
  260. return b, nil
  261. }
  262. }
  263. func makeDurationMarshaler(u *marshalInfo) (sizer, marshaler) {
  264. return func(ptr pointer, tagsize int) int {
  265. d := ptr.asPointerTo(u.typ).Interface().(*time.Duration)
  266. dur := durationProto(*d)
  267. siz := Size(dur)
  268. return tagsize + SizeVarint(uint64(siz)) + siz
  269. }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  270. d := ptr.asPointerTo(u.typ).Interface().(*time.Duration)
  271. dur := durationProto(*d)
  272. buf, err := Marshal(dur)
  273. if err != nil {
  274. return nil, err
  275. }
  276. b = appendVarint(b, wiretag)
  277. b = appendVarint(b, uint64(len(buf)))
  278. b = append(b, buf...)
  279. return b, nil
  280. }
  281. }
  282. func makeDurationPtrMarshaler(u *marshalInfo) (sizer, marshaler) {
  283. return func(ptr pointer, tagsize int) int {
  284. if ptr.isNil() {
  285. return 0
  286. }
  287. d := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*time.Duration)
  288. dur := durationProto(*d)
  289. siz := Size(dur)
  290. return tagsize + SizeVarint(uint64(siz)) + siz
  291. }, func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  292. if ptr.isNil() {
  293. return b, nil
  294. }
  295. d := ptr.asPointerTo(reflect.PtrTo(u.typ)).Elem().Interface().(*time.Duration)
  296. dur := durationProto(*d)
  297. buf, err := Marshal(dur)
  298. if err != nil {
  299. return nil, err
  300. }
  301. b = appendVarint(b, wiretag)
  302. b = appendVarint(b, uint64(len(buf)))
  303. b = append(b, buf...)
  304. return b, nil
  305. }
  306. }
  307. func makeDurationSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  308. return func(ptr pointer, tagsize int) int {
  309. s := ptr.getSlice(u.typ)
  310. n := 0
  311. for i := 0; i < s.Len(); i++ {
  312. elem := s.Index(i)
  313. d := elem.Interface().(time.Duration)
  314. dur := durationProto(d)
  315. siz := Size(dur)
  316. n += siz + SizeVarint(uint64(siz)) + tagsize
  317. }
  318. return n
  319. },
  320. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  321. s := ptr.getSlice(u.typ)
  322. for i := 0; i < s.Len(); i++ {
  323. elem := s.Index(i)
  324. d := elem.Interface().(time.Duration)
  325. dur := durationProto(d)
  326. siz := Size(dur)
  327. buf, err := Marshal(dur)
  328. if err != nil {
  329. return nil, err
  330. }
  331. b = appendVarint(b, wiretag)
  332. b = appendVarint(b, uint64(siz))
  333. b = append(b, buf...)
  334. }
  335. return b, nil
  336. }
  337. }
  338. func makeDurationPtrSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  339. return func(ptr pointer, tagsize int) int {
  340. s := ptr.getSlice(reflect.PtrTo(u.typ))
  341. n := 0
  342. for i := 0; i < s.Len(); i++ {
  343. elem := s.Index(i)
  344. d := elem.Interface().(*time.Duration)
  345. dur := durationProto(*d)
  346. siz := Size(dur)
  347. n += siz + SizeVarint(uint64(siz)) + tagsize
  348. }
  349. return n
  350. },
  351. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  352. s := ptr.getSlice(reflect.PtrTo(u.typ))
  353. for i := 0; i < s.Len(); i++ {
  354. elem := s.Index(i)
  355. d := elem.Interface().(*time.Duration)
  356. dur := durationProto(*d)
  357. siz := Size(dur)
  358. buf, err := Marshal(dur)
  359. if err != nil {
  360. return nil, err
  361. }
  362. b = appendVarint(b, wiretag)
  363. b = appendVarint(b, uint64(siz))
  364. b = append(b, buf...)
  365. }
  366. return b, nil
  367. }
  368. }