table_unmarshal_gogo.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. "io"
  31. "reflect"
  32. )
  33. func makeUnmarshalMessage(sub *unmarshalInfo, name string) unmarshaler {
  34. return func(b []byte, f pointer, w int) ([]byte, error) {
  35. if w != WireBytes {
  36. return nil, errInternalBadWireType
  37. }
  38. x, n := decodeVarint(b)
  39. if n == 0 {
  40. return nil, io.ErrUnexpectedEOF
  41. }
  42. b = b[n:]
  43. if x > uint64(len(b)) {
  44. return nil, io.ErrUnexpectedEOF
  45. }
  46. // First read the message field to see if something is there.
  47. // The semantics of multiple submessages are weird. Instead of
  48. // the last one winning (as it is for all other fields), multiple
  49. // submessages are merged.
  50. v := f // gogo: changed from v := f.getPointer()
  51. if v.isNil() {
  52. v = valToPointer(reflect.New(sub.typ))
  53. f.setPointer(v)
  54. }
  55. err := sub.unmarshal(v, b[:x])
  56. if err != nil {
  57. if r, ok := err.(*RequiredNotSetError); ok {
  58. r.field = name + "." + r.field
  59. } else {
  60. return nil, err
  61. }
  62. }
  63. return b[x:], err
  64. }
  65. }
  66. func makeUnmarshalMessageSlice(sub *unmarshalInfo, name string) unmarshaler {
  67. return func(b []byte, f pointer, w int) ([]byte, error) {
  68. if w != WireBytes {
  69. return nil, errInternalBadWireType
  70. }
  71. x, n := decodeVarint(b)
  72. if n == 0 {
  73. return nil, io.ErrUnexpectedEOF
  74. }
  75. b = b[n:]
  76. if x > uint64(len(b)) {
  77. return nil, io.ErrUnexpectedEOF
  78. }
  79. v := valToPointer(reflect.New(sub.typ))
  80. err := sub.unmarshal(v, b[:x])
  81. if err != nil {
  82. if r, ok := err.(*RequiredNotSetError); ok {
  83. r.field = name + "." + r.field
  84. } else {
  85. return nil, err
  86. }
  87. }
  88. f.appendRef(v, sub.typ) // gogo: changed from f.appendPointer(v)
  89. return b[x:], err
  90. }
  91. }
  92. func makeUnmarshalCustomPtr(sub *unmarshalInfo, name string) unmarshaler {
  93. return func(b []byte, f pointer, w int) ([]byte, error) {
  94. if w != WireBytes {
  95. return nil, errInternalBadWireType
  96. }
  97. x, n := decodeVarint(b)
  98. if n == 0 {
  99. return nil, io.ErrUnexpectedEOF
  100. }
  101. b = b[n:]
  102. if x > uint64(len(b)) {
  103. return nil, io.ErrUnexpectedEOF
  104. }
  105. s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem()
  106. s.Set(reflect.New(sub.typ))
  107. m := s.Interface().(custom)
  108. if err := m.Unmarshal(b[:x]); err != nil {
  109. return nil, err
  110. }
  111. return b[x:], nil
  112. }
  113. }
  114. func makeUnmarshalCustomSlice(sub *unmarshalInfo, name string) unmarshaler {
  115. return func(b []byte, f pointer, w int) ([]byte, error) {
  116. if w != WireBytes {
  117. return nil, errInternalBadWireType
  118. }
  119. x, n := decodeVarint(b)
  120. if n == 0 {
  121. return nil, io.ErrUnexpectedEOF
  122. }
  123. b = b[n:]
  124. if x > uint64(len(b)) {
  125. return nil, io.ErrUnexpectedEOF
  126. }
  127. m := reflect.New(sub.typ)
  128. c := m.Interface().(custom)
  129. if err := c.Unmarshal(b[:x]); err != nil {
  130. return nil, err
  131. }
  132. v := valToPointer(m)
  133. f.appendRef(v, sub.typ)
  134. return b[x:], nil
  135. }
  136. }
  137. func makeUnmarshalCustom(sub *unmarshalInfo, name string) unmarshaler {
  138. return func(b []byte, f pointer, w int) ([]byte, error) {
  139. if w != WireBytes {
  140. return nil, errInternalBadWireType
  141. }
  142. x, n := decodeVarint(b)
  143. if n == 0 {
  144. return nil, io.ErrUnexpectedEOF
  145. }
  146. b = b[n:]
  147. if x > uint64(len(b)) {
  148. return nil, io.ErrUnexpectedEOF
  149. }
  150. m := f.asPointerTo(sub.typ).Interface().(custom)
  151. if err := m.Unmarshal(b[:x]); err != nil {
  152. return nil, err
  153. }
  154. return b[x:], nil
  155. }
  156. }
  157. func makeUnmarshalTime(sub *unmarshalInfo, name string) unmarshaler {
  158. return func(b []byte, f pointer, w int) ([]byte, error) {
  159. if w != WireBytes {
  160. return nil, errInternalBadWireType
  161. }
  162. x, n := decodeVarint(b)
  163. if n == 0 {
  164. return nil, io.ErrUnexpectedEOF
  165. }
  166. b = b[n:]
  167. if x > uint64(len(b)) {
  168. return nil, io.ErrUnexpectedEOF
  169. }
  170. m := &timestamp{}
  171. if err := Unmarshal(b[:x], m); err != nil {
  172. return nil, err
  173. }
  174. t, err := timestampFromProto(m)
  175. if err != nil {
  176. return nil, err
  177. }
  178. s := f.asPointerTo(sub.typ).Elem()
  179. s.Set(reflect.ValueOf(t))
  180. return b[x:], nil
  181. }
  182. }
  183. func makeUnmarshalTimePtr(sub *unmarshalInfo, name string) unmarshaler {
  184. return func(b []byte, f pointer, w int) ([]byte, error) {
  185. if w != WireBytes {
  186. return nil, errInternalBadWireType
  187. }
  188. x, n := decodeVarint(b)
  189. if n == 0 {
  190. return nil, io.ErrUnexpectedEOF
  191. }
  192. b = b[n:]
  193. if x > uint64(len(b)) {
  194. return nil, io.ErrUnexpectedEOF
  195. }
  196. m := &timestamp{}
  197. if err := Unmarshal(b[:x], m); err != nil {
  198. return nil, err
  199. }
  200. t, err := timestampFromProto(m)
  201. if err != nil {
  202. return nil, err
  203. }
  204. s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem()
  205. s.Set(reflect.ValueOf(&t))
  206. return b[x:], nil
  207. }
  208. }
  209. func makeUnmarshalTimePtrSlice(sub *unmarshalInfo, name string) unmarshaler {
  210. return func(b []byte, f pointer, w int) ([]byte, error) {
  211. if w != WireBytes {
  212. return nil, errInternalBadWireType
  213. }
  214. x, n := decodeVarint(b)
  215. if n == 0 {
  216. return nil, io.ErrUnexpectedEOF
  217. }
  218. b = b[n:]
  219. if x > uint64(len(b)) {
  220. return nil, io.ErrUnexpectedEOF
  221. }
  222. m := &timestamp{}
  223. if err := Unmarshal(b[:x], m); err != nil {
  224. return nil, err
  225. }
  226. t, err := timestampFromProto(m)
  227. if err != nil {
  228. return nil, err
  229. }
  230. slice := f.getSlice(reflect.PtrTo(sub.typ))
  231. newSlice := reflect.Append(slice, reflect.ValueOf(&t))
  232. slice.Set(newSlice)
  233. return b[x:], nil
  234. }
  235. }
  236. func makeUnmarshalTimeSlice(sub *unmarshalInfo, name string) unmarshaler {
  237. return func(b []byte, f pointer, w int) ([]byte, error) {
  238. if w != WireBytes {
  239. return nil, errInternalBadWireType
  240. }
  241. x, n := decodeVarint(b)
  242. if n == 0 {
  243. return nil, io.ErrUnexpectedEOF
  244. }
  245. b = b[n:]
  246. if x > uint64(len(b)) {
  247. return nil, io.ErrUnexpectedEOF
  248. }
  249. m := &timestamp{}
  250. if err := Unmarshal(b[:x], m); err != nil {
  251. return nil, err
  252. }
  253. t, err := timestampFromProto(m)
  254. if err != nil {
  255. return nil, err
  256. }
  257. slice := f.getSlice(sub.typ)
  258. newSlice := reflect.Append(slice, reflect.ValueOf(t))
  259. slice.Set(newSlice)
  260. return b[x:], nil
  261. }
  262. }
  263. func makeUnmarshalDurationPtr(sub *unmarshalInfo, name string) unmarshaler {
  264. return func(b []byte, f pointer, w int) ([]byte, error) {
  265. if w != WireBytes {
  266. return nil, errInternalBadWireType
  267. }
  268. x, n := decodeVarint(b)
  269. if n == 0 {
  270. return nil, io.ErrUnexpectedEOF
  271. }
  272. b = b[n:]
  273. if x > uint64(len(b)) {
  274. return nil, io.ErrUnexpectedEOF
  275. }
  276. m := &duration{}
  277. if err := Unmarshal(b[:x], m); err != nil {
  278. return nil, err
  279. }
  280. d, err := durationFromProto(m)
  281. if err != nil {
  282. return nil, err
  283. }
  284. s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem()
  285. s.Set(reflect.ValueOf(&d))
  286. return b[x:], nil
  287. }
  288. }
  289. func makeUnmarshalDuration(sub *unmarshalInfo, name string) unmarshaler {
  290. return func(b []byte, f pointer, w int) ([]byte, error) {
  291. if w != WireBytes {
  292. return nil, errInternalBadWireType
  293. }
  294. x, n := decodeVarint(b)
  295. if n == 0 {
  296. return nil, io.ErrUnexpectedEOF
  297. }
  298. b = b[n:]
  299. if x > uint64(len(b)) {
  300. return nil, io.ErrUnexpectedEOF
  301. }
  302. m := &duration{}
  303. if err := Unmarshal(b[:x], m); err != nil {
  304. return nil, err
  305. }
  306. d, err := durationFromProto(m)
  307. if err != nil {
  308. return nil, err
  309. }
  310. s := f.asPointerTo(sub.typ).Elem()
  311. s.Set(reflect.ValueOf(d))
  312. return b[x:], nil
  313. }
  314. }
  315. func makeUnmarshalDurationPtrSlice(sub *unmarshalInfo, name string) unmarshaler {
  316. return func(b []byte, f pointer, w int) ([]byte, error) {
  317. if w != WireBytes {
  318. return nil, errInternalBadWireType
  319. }
  320. x, n := decodeVarint(b)
  321. if n == 0 {
  322. return nil, io.ErrUnexpectedEOF
  323. }
  324. b = b[n:]
  325. if x > uint64(len(b)) {
  326. return nil, io.ErrUnexpectedEOF
  327. }
  328. m := &duration{}
  329. if err := Unmarshal(b[:x], m); err != nil {
  330. return nil, err
  331. }
  332. d, err := durationFromProto(m)
  333. if err != nil {
  334. return nil, err
  335. }
  336. slice := f.getSlice(reflect.PtrTo(sub.typ))
  337. newSlice := reflect.Append(slice, reflect.ValueOf(&d))
  338. slice.Set(newSlice)
  339. return b[x:], nil
  340. }
  341. }
  342. func makeUnmarshalDurationSlice(sub *unmarshalInfo, name string) unmarshaler {
  343. return func(b []byte, f pointer, w int) ([]byte, error) {
  344. if w != WireBytes {
  345. return nil, errInternalBadWireType
  346. }
  347. x, n := decodeVarint(b)
  348. if n == 0 {
  349. return nil, io.ErrUnexpectedEOF
  350. }
  351. b = b[n:]
  352. if x > uint64(len(b)) {
  353. return nil, io.ErrUnexpectedEOF
  354. }
  355. m := &duration{}
  356. if err := Unmarshal(b[:x], m); err != nil {
  357. return nil, err
  358. }
  359. d, err := durationFromProto(m)
  360. if err != nil {
  361. return nil, err
  362. }
  363. slice := f.getSlice(sub.typ)
  364. newSlice := reflect.Append(slice, reflect.ValueOf(d))
  365. slice.Set(newSlice)
  366. return b[x:], nil
  367. }
  368. }