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