1 // Protocol Buffers for Go with Gadgets
3 // Copyright (c) 2018, The GoGo Authors. All rights reserved.
4 // http://github.com/gogo/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
36 func makeUnmarshalMessage(sub *unmarshalInfo, name string) unmarshaler {
37 return func(b []byte, f pointer, w int) ([]byte, error) {
39 return nil, errInternalBadWireType
41 x, n := decodeVarint(b)
43 return nil, io.ErrUnexpectedEOF
46 if x > uint64(len(b)) {
47 return nil, io.ErrUnexpectedEOF
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()
55 v = valToPointer(reflect.New(sub.typ))
58 err := sub.unmarshal(v, b[:x])
60 if r, ok := err.(*RequiredNotSetError); ok {
61 r.field = name + "." + r.field
70 func makeUnmarshalMessageSlice(sub *unmarshalInfo, name string) unmarshaler {
71 return func(b []byte, f pointer, w int) ([]byte, error) {
73 return nil, errInternalBadWireType
75 x, n := decodeVarint(b)
77 return nil, io.ErrUnexpectedEOF
80 if x > uint64(len(b)) {
81 return nil, io.ErrUnexpectedEOF
83 v := valToPointer(reflect.New(sub.typ))
84 err := sub.unmarshal(v, b[:x])
86 if r, ok := err.(*RequiredNotSetError); ok {
87 r.field = name + "." + r.field
92 f.appendRef(v, sub.typ) // gogo: changed from f.appendPointer(v)
97 func makeUnmarshalCustomPtr(sub *unmarshalInfo, name string) unmarshaler {
98 return func(b []byte, f pointer, w int) ([]byte, error) {
100 return nil, errInternalBadWireType
102 x, n := decodeVarint(b)
104 return nil, io.ErrUnexpectedEOF
107 if x > uint64(len(b)) {
108 return nil, io.ErrUnexpectedEOF
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 {
121 func makeUnmarshalCustomSlice(sub *unmarshalInfo, name string) unmarshaler {
122 return func(b []byte, f pointer, w int) ([]byte, error) {
124 return nil, errInternalBadWireType
126 x, n := decodeVarint(b)
128 return nil, io.ErrUnexpectedEOF
131 if x > uint64(len(b)) {
132 return nil, io.ErrUnexpectedEOF
134 m := reflect.New(sub.typ)
135 c := m.Interface().(custom)
136 if err := c.Unmarshal(b[:x]); err != nil {
140 f.appendRef(v, sub.typ)
145 func makeUnmarshalCustom(sub *unmarshalInfo, name string) unmarshaler {
146 return func(b []byte, f pointer, w int) ([]byte, error) {
148 return nil, errInternalBadWireType
150 x, n := decodeVarint(b)
152 return nil, io.ErrUnexpectedEOF
155 if x > uint64(len(b)) {
156 return nil, io.ErrUnexpectedEOF
159 m := f.asPointerTo(sub.typ).Interface().(custom)
160 if err := m.Unmarshal(b[:x]); err != nil {
167 func makeUnmarshalTime(sub *unmarshalInfo, name string) unmarshaler {
168 return func(b []byte, f pointer, w int) ([]byte, error) {
170 return nil, errInternalBadWireType
172 x, n := decodeVarint(b)
174 return nil, io.ErrUnexpectedEOF
177 if x > uint64(len(b)) {
178 return nil, io.ErrUnexpectedEOF
181 if err := Unmarshal(b[:x], m); err != nil {
184 t, err := timestampFromProto(m)
188 s := f.asPointerTo(sub.typ).Elem()
189 s.Set(reflect.ValueOf(t))
194 func makeUnmarshalTimePtr(sub *unmarshalInfo, name string) unmarshaler {
195 return func(b []byte, f pointer, w int) ([]byte, error) {
197 return nil, errInternalBadWireType
199 x, n := decodeVarint(b)
201 return nil, io.ErrUnexpectedEOF
204 if x > uint64(len(b)) {
205 return nil, io.ErrUnexpectedEOF
208 if err := Unmarshal(b[:x], m); err != nil {
211 t, err := timestampFromProto(m)
215 s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem()
216 s.Set(reflect.ValueOf(&t))
221 func makeUnmarshalTimePtrSlice(sub *unmarshalInfo, name string) unmarshaler {
222 return func(b []byte, f pointer, w int) ([]byte, error) {
224 return nil, errInternalBadWireType
226 x, n := decodeVarint(b)
228 return nil, io.ErrUnexpectedEOF
231 if x > uint64(len(b)) {
232 return nil, io.ErrUnexpectedEOF
235 if err := Unmarshal(b[:x], m); err != nil {
238 t, err := timestampFromProto(m)
242 slice := f.getSlice(reflect.PtrTo(sub.typ))
243 newSlice := reflect.Append(slice, reflect.ValueOf(&t))
249 func makeUnmarshalTimeSlice(sub *unmarshalInfo, name string) unmarshaler {
250 return func(b []byte, f pointer, w int) ([]byte, error) {
252 return nil, errInternalBadWireType
254 x, n := decodeVarint(b)
256 return nil, io.ErrUnexpectedEOF
259 if x > uint64(len(b)) {
260 return nil, io.ErrUnexpectedEOF
263 if err := Unmarshal(b[:x], m); err != nil {
266 t, err := timestampFromProto(m)
270 slice := f.getSlice(sub.typ)
271 newSlice := reflect.Append(slice, reflect.ValueOf(t))
277 func makeUnmarshalDurationPtr(sub *unmarshalInfo, name string) unmarshaler {
278 return func(b []byte, f pointer, w int) ([]byte, error) {
280 return nil, errInternalBadWireType
282 x, n := decodeVarint(b)
284 return nil, io.ErrUnexpectedEOF
287 if x > uint64(len(b)) {
288 return nil, io.ErrUnexpectedEOF
291 if err := Unmarshal(b[:x], m); err != nil {
294 d, err := durationFromProto(m)
298 s := f.asPointerTo(reflect.PtrTo(sub.typ)).Elem()
299 s.Set(reflect.ValueOf(&d))
304 func makeUnmarshalDuration(sub *unmarshalInfo, name string) unmarshaler {
305 return func(b []byte, f pointer, w int) ([]byte, error) {
307 return nil, errInternalBadWireType
309 x, n := decodeVarint(b)
311 return nil, io.ErrUnexpectedEOF
314 if x > uint64(len(b)) {
315 return nil, io.ErrUnexpectedEOF
318 if err := Unmarshal(b[:x], m); err != nil {
321 d, err := durationFromProto(m)
325 s := f.asPointerTo(sub.typ).Elem()
326 s.Set(reflect.ValueOf(d))
331 func makeUnmarshalDurationPtrSlice(sub *unmarshalInfo, name string) unmarshaler {
332 return func(b []byte, f pointer, w int) ([]byte, error) {
334 return nil, errInternalBadWireType
336 x, n := decodeVarint(b)
338 return nil, io.ErrUnexpectedEOF
341 if x > uint64(len(b)) {
342 return nil, io.ErrUnexpectedEOF
345 if err := Unmarshal(b[:x], m); err != nil {
348 d, err := durationFromProto(m)
352 slice := f.getSlice(reflect.PtrTo(sub.typ))
353 newSlice := reflect.Append(slice, reflect.ValueOf(&d))
359 func makeUnmarshalDurationSlice(sub *unmarshalInfo, name string) unmarshaler {
360 return func(b []byte, f pointer, w int) ([]byte, error) {
362 return nil, errInternalBadWireType
364 x, n := decodeVarint(b)
366 return nil, io.ErrUnexpectedEOF
369 if x > uint64(len(b)) {
370 return nil, io.ErrUnexpectedEOF
373 if err := Unmarshal(b[:x], m); err != nil {
376 d, err := durationFromProto(m)
380 slice := f.getSlice(sub.typ)
381 newSlice := reflect.Append(slice, reflect.ValueOf(d))