Debian Codegraph @

go/pkg/amqp/unmarshal.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 oor more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 package amqp
21
22 // #include <proton/codec.h>
23 import "C"
24
25 import (
26 "bytes"
27 "fmt"
28 "io"
29 "reflect"
30 "time"
31 "unsafe"
32 )
33
34 const minDecode = 1024
35
36 // Error returned if AMQP data cannot be unmarshaled as the desired Go type.
37 type UnmarshalError struct {
38 // The name of the AMQP type.
39 AMQPType string
40 // The Go type.
41 GoType reflect.Type
42
43 s string
44 }
45
46 func (e UnmarshalError) Error() string { return e.s }
47
48 // Error returned if there are not enough bytes to decode a complete AMQP value.
49 var EndOfData = &UnmarshalError{s: "Not enough data for AMQP value"}
50
51 var badData = &UnmarshalError{s: "Unexpected error in data"}
52
53 func newUnmarshalError(pnType C.pn_type_t, v interface{}) *UnmarshalError {
54 e := &UnmarshalError{
55 AMQPType: C.pn_type_t(pnType).String(),
56 GoType: reflect.TypeOf(v),
57 }
58 if e.GoType == nil || e.GoType.Kind() != reflect.Ptr {
59 e.s = fmt.Sprintf("cannot unmarshal to Go type %v, not a pointer", e.GoType)
60 } else {
61 e.s = fmt.Sprintf("cannot unmarshal AMQP %v to Go %v", e.AMQPType, e.GoType.Elem())
62 }
63 return e
64 }
65
66 func doPanic(data *C.pn_data_t, v interface{}) {
67 e := newUnmarshalError(C.pn_data_type(data), v)
68 panic(e)
69 }
70
71 func doPanicMsg(data *C.pn_data_t, v interface{}, msg string) {
72 e := newUnmarshalError(C.pn_data_type(data), v)
73 e.s = e.s + ": " + msg
74 panic(e)
75 }
76
77 func panicIfBadData(data *C.pn_data_t, v interface{}) {
78 if C.pn_data_errno(data) != 0 {
79 doPanicMsg(data, v, PnError(C.pn_data_error(data)).Error())
80 }
81 }
82
83 func panicUnless(ok bool, data *C.pn_data_t, v interface{}) {
84 if !ok {
85 doPanic(data, v)
86 }
87 }
88
89 func checkOp(ok bool, v interface{}) {
90 if !ok {
91 panic(&badData)
92 }
93 }
94
95 //
96 // Decoding from a pn_data_t
97 //
98 // NOTE: we use panic() to signal a decoding error, simplifies decoding logic.
99 // We recover() at the highest possible level - i.e. in the exported Unmarshal or Decode.
100 //
101
102 // Decoder decodes AMQP values from an io.Reader.
103 //
104 type Decoder struct {
105 reader io.Reader
106 buffer bytes.Buffer
107 }
108
109 // NewDecoder returns a new decoder that reads from r.
110 //
111 // The decoder has it's own buffer and may read more data than required for the
112 // AMQP values requested. Use Buffered to see if there is data left in the
113 // buffer.
114 //
115 func NewDecoder(r io.Reader) *Decoder {
116 return &Decoder{r, bytes.Buffer{}}
117 }
118
119 // Buffered returns a reader of the data remaining in the Decoder's buffer. The
120 // reader is valid until the next call to Decode.
121 //
122 func (d *Decoder) Buffered() io.Reader {
123 return bytes.NewReader(d.buffer.Bytes())
124 }
125
126 // Decode reads the next AMQP value from the Reader and stores it in the value pointed to by v.
127 //
128 // See the documentation for Unmarshal for details about the conversion of AMQP into a Go value.
129 //
130 func (d *Decoder) Decode(v interface{}) (err error) {
131 data := C.pn_data(0)
132 defer C.pn_data_free(data)
133 var n int
134 for n, err = decode(data, d.buffer.Bytes()); err == EndOfData; {
135 err = d.more()
136 if err == nil {
137 n, err = decode(data, d.buffer.Bytes())
138 }
139 }
140 if err == nil {
141 if err = recoverUnmarshal(v, data); err == nil {
142 d.buffer.Next(n)
143 }
144 }
145 return
146 }
147
148 /*
149
150 Unmarshal decodes AMQP-encoded bytes and stores the result in the Go value
151 pointed to by v. Legal conversions from the source AMQP type to the target Go
152 type as follows:
153
154 +----------------------------+-------------------------------------------------+
155 |Target Go type | Allowed AMQP types
156 +============================+==================================================+
157 |bool |bool |
158 +----------------------------+--------------------------------------------------+
159 |int, int8, int16, int32, |Equivalent or smaller signed integer type: |
160 |int64 |byte, short, int, long or char |
161 +----------------------------+--------------------------------------------------+
162 |uint, uint8, uint16, uint32,|Equivalent or smaller unsigned integer type: |
163 |uint64 |ubyte, ushort, uint, ulong |
164 +----------------------------+--------------------------------------------------+
165 |float32, float64 |Equivalent or smaller float or double |
166 +----------------------------+--------------------------------------------------+
167 |string, []byte |string, symbol or binary |
168 +----------------------------+--------------------------------------------------+
169 |Symbol |symbol |
170 +----------------------------+--------------------------------------------------+
171 |Char |char |
172 +----------------------------+--------------------------------------------------+
173 |Described |AMQP described type [1] |
174 +----------------------------+--------------------------------------------------+
175 |Time |timestamp |
176 +----------------------------+--------------------------------------------------+
177 |UUID |uuid |
178 +----------------------------+--------------------------------------------------+
179 |map[interface{}]interface{} |Any AMQP map |
180 +----------------------------+--------------------------------------------------+
181 |map[K]T |map, provided all keys and values can unmarshal |
182 | |to types K,T |
183 +----------------------------+--------------------------------------------------+
184 |[]interface{} |AMQP list or array |
185 +----------------------------+--------------------------------------------------+
186 |[]T |list or array if elements can unmarshal as T |
187 +----------------------------+------------------n-------------------------------+
188 |interface{} |any AMQP type[2] |
189 +----------------------------+--------------------------------------------------+
190
191 [1] An AMQP described value can also unmarshal to a plain value, discarding the
192 descriptor. Unmarshalling into the special amqp.Described type preserves the
193 descriptor.
194
195 [2] Any AMQP value can be unmarshalled to an interface{}. The Go type is
196 determined by the AMQP type as follows:
197
198 +----------------------------+--------------------------------------------------+
199 |Source AMQP Type |Go Type in target interface{} |
200 +============================+==================================================+
201 |bool |bool |
202 +----------------------------+--------------------------------------------------+
203 |byte,short,int,long |int8,int16,int32,int64 |
204 +----------------------------+--------------------------------------------------+
205 |ubyte,ushort,uint,ulong |uint8,uint16,uint32,uint64 |
206 +----------------------------+--------------------------------------------------+
207 |float, double |float32, float64 |
208 +----------------------------+--------------------------------------------------+
209 |string |string |
210 +----------------------------+--------------------------------------------------+
211 |symbol |Symbol |
212 +----------------------------+--------------------------------------------------+
213 |char |Char |
214 +----------------------------+--------------------------------------------------+
215 |binary |Binary |
216 +----------------------------+--------------------------------------------------+
217 |null |nil |
218 +----------------------------+--------------------------------------------------+
219 |described type |Described |
220 +----------------------------+--------------------------------------------------+
221 |timestamp |time.Time |
222 +----------------------------+--------------------------------------------------+
223 |uuid |UUID |
224 +----------------------------+--------------------------------------------------+
225 |map |Map or AnyMap[4] |
226 +----------------------------+--------------------------------------------------+
227 |list |List |
228 +----------------------------+--------------------------------------------------+
229 |array |[]T for simple types, T is chosen as above [3] |
230 +----------------------------+--------------------------------------------------+
231
232 [3] An AMQP array of simple types unmarshalls as a slice of the corresponding Go type.
233 An AMQP array containing complex types (lists, maps or nested arrays) unmarshals
234 to the generic array type amqp.Array
235
236 [4] An AMQP map unmarshals as the generic `type Map map[interface{}]interface{}`
237 unless it contains key values that are illegal as Go map types, in which case
238 it unmarshals as type AnyMap.
239
240 The following Go types cannot be unmarshaled: uintptr, function, interface,
241 channel, array (use slice), struct
242
243 AMQP types not yet supported: decimal32/64/128
244 */
245 func Unmarshal(bytes []byte, v interface{}) (n int, err error) {
246 data := C.pn_data(0)
247 defer C.pn_data_free(data)
248 n, err = decode(data, bytes)
249 if err == nil {
250 err = recoverUnmarshal(v, data)
251 }
252 return
253 }
254
255 // Internal
256 func UnmarshalUnsafe(pnData unsafe.Pointer, v interface{}) (err error) {
257 return recoverUnmarshal(v, (*C.pn_data_t)(pnData))
258 }
259
260 // more reads more data when we can't parse a complete AMQP type
261 func (d *Decoder) more() error {
262 var readSize int64 = minDecode
263 if int64(d.buffer.Len()) > readSize { // Grow by doubling
264 readSize = int64(d.buffer.Len())
265 }
266 var n int64
267 n, err := d.buffer.ReadFrom(io.LimitReader(d.reader, readSize))
268 if n == 0 && err == nil { // ReadFrom won't report io.EOF, just returns 0
269 err = io.EOF
270 }
271 return err
272 }
273
274 // Call unmarshal(), convert panic to error value
275 func recoverUnmarshal(v interface{}, data *C.pn_data_t) (err error) {
276 defer func() {
277 if r := recover(); r != nil {
278 if uerr, ok := r.(*UnmarshalError); ok {
279 err = uerr
280 } else {
281 panic(r)
282 }
283 }
284 }()
285 unmarshal(v, data)
286 return nil
287 }
288
289 // Unmarshal from data into value pointed at by v. Returns v.
290 // NOTE: If you update this you also need to update getInterface()
291 func unmarshal(v interface{}, data *C.pn_data_t) {
292 rt := reflect.TypeOf(v)
293 rv := reflect.ValueOf(v)
294 panicUnless(v != nil && rt.Kind() == reflect.Ptr && !rv.IsNil(), data, v)
295
296 // Check for PN_DESCRIBED first, as described types can unmarshal into any of the Go types.
297 // An interface{} target is handled in the switch below, even for described types.
298 if _, isInterface := v.(*interface{}); !isInterface && bool(C.pn_data_is_described(data)) {
299 getDescribed(data, v)
300 return
301 }
302
303 // Unmarshal based on the target type
304 pnType := C.pn_data_type(data)
305 switch v := v.(type) {
306
307 case *bool:
308 panicUnless(pnType == C.PN_BOOL, data, v)
309 *v = bool(C.pn_data_get_bool(data))
310
311 case *int8:
312 panicUnless(pnType == C.PN_BYTE, data, v)
313 *v = int8(C.pn_data_get_byte(data))
314
315 case *uint8:
316 panicUnless(pnType == C.PN_UBYTE, data, v)
317 *v = uint8(C.pn_data_get_ubyte(data))
318
319 case *int16:
320 switch C.pn_data_type(data) {
321 case C.PN_BYTE:
322 *v = int16(C.pn_data_get_byte(data))
323 case C.PN_SHORT:
324 *v = int16(C.pn_data_get_short(data))
325 default:
326 doPanic(data, v)
327 }
328
329 case *uint16:
330 switch pnType {
331 case C.PN_UBYTE:
332 *v = uint16(C.pn_data_get_ubyte(data))
333 case C.PN_USHORT:
334 *v = uint16(C.pn_data_get_ushort(data))
335 default:
336 doPanic(data, v)
337 }
338
339 case *int32:
340 switch pnType {
341 case C.PN_CHAR:
342 *v = int32(C.pn_data_get_char(data))
343 case C.PN_BYTE:
344 *v = int32(C.pn_data_get_byte(data))
345 case C.PN_SHORT:
346 *v = int32(C.pn_data_get_short(data))
347 case C.PN_INT:
348 *v = int32(C.pn_data_get_int(data))
349 default:
350 doPanic(data, v)
351 }
352
353 case *uint32:
354 switch pnType {
355 case C.PN_CHAR:
356 *v = uint32(C.pn_data_get_char(data))
357 case C.PN_UBYTE:
358 *v = uint32(C.pn_data_get_ubyte(data))
359 case C.PN_USHORT:
360 *v = uint32(C.pn_data_get_ushort(data))
361 case C.PN_UINT:
362 *v = uint32(C.pn_data_get_uint(data))
363 default:
364 doPanic(data, v)
365 }
366
367 case *int64:
368 switch pnType {
369 case C.PN_CHAR:
370 *v = int64(C.pn_data_get_char(data))
371 case C.PN_BYTE:
372 *v = int64(C.pn_data_get_byte(data))
373 case C.PN_SHORT:
374 *v = int64(C.pn_data_get_short(data))
375 case C.PN_INT:
376 *v = int64(C.pn_data_get_int(data))
377 case C.PN_LONG:
378 *v = int64(C.pn_data_get_long(data))
379 default:
380 doPanic(data, v)
381 }
382
383 case *uint64:
384 switch pnType {
385 case C.PN_CHAR:
386 *v = uint64(C.pn_data_get_char(data))
387 case C.PN_UBYTE:
388 *v = uint64(C.pn_data_get_ubyte(data))
389 case C.PN_USHORT:
390 *v = uint64(C.pn_data_get_ushort(data))
391 case C.PN_ULONG:
392 *v = uint64(C.pn_data_get_ulong(data))
393 default:
394 doPanic(data, v)
395 }
396
397 case *int:
398 switch pnType {
399 case C.PN_CHAR:
400 *v = int(C.pn_data_get_char(data))
401 case C.PN_BYTE:
402 *v = int(C.pn_data_get_byte(data))
403 case C.PN_SHORT:
404 *v = int(C.pn_data_get_short(data))
405 case C.PN_INT:
406 *v = int(C.pn_data_get_int(data))
407 case C.PN_LONG:
408 if intIs64 {
409 *v = int(C.pn_data_get_long(data))
410 } else {
411 doPanic(data, v)
412 }
413 default:
414 doPanic(data, v)
415 }
416
417 case *uint:
418 switch pnType {
419 case C.PN_CHAR:
420 *v = uint(C.pn_data_get_char(data))
421 case C.PN_UBYTE:
422 *v = uint(C.pn_data_get_ubyte(data))
423 case C.PN_USHORT:
424 *v = uint(C.pn_data_get_ushort(data))
425 case C.PN_UINT:
426 *v = uint(C.pn_data_get_uint(data))
427 case C.PN_ULONG:
428 if intIs64 {
429 *v = uint(C.pn_data_get_ulong(data))
430 } else {
431 doPanic(data, v)
432 }
433 default:
434 doPanic(data, v)
435 }
436
437 case *float32:
438 panicUnless(pnType == C.PN_FLOAT, data, v)
439 *v = float32(C.pn_data_get_float(data))
440
441 case *float64:
442 switch pnType {
443 case C.PN_FLOAT:
444 *v = float64(C.pn_data_get_float(data))
445 case C.PN_DOUBLE:
446 *v = float64(C.pn_data_get_double(data))
447 default:
448 doPanic(data, v)
449 }
450
451 case *string:
452 switch pnType {
453 case C.PN_STRING:
454 *v = goString(C.pn_data_get_string(data))
455 case C.PN_SYMBOL:
456 *v = goString(C.pn_data_get_symbol(data))
457 case C.PN_BINARY:
458 *v = goString(C.pn_data_get_binary(data))
459 default:
460 doPanic(data, v)
461 }
462
463 case *[]byte:
464 switch pnType {
465 case C.PN_STRING:
466 *v = goBytes(C.pn_data_get_string(data))
467 case C.PN_SYMBOL:
468 *v = goBytes(C.pn_data_get_symbol(data))
469 case C.PN_BINARY:
470 *v = goBytes(C.pn_data_get_binary(data))
471 default:
472 doPanic(data, v)
473 }
474 return
475
476 case *Char:
477 panicUnless(pnType == C.PN_CHAR, data, v)
478 *v = Char(C.pn_data_get_char(data))
479
480 case *Binary:
481 panicUnless(pnType == C.PN_BINARY, data, v)
482 *v = Binary(goBytes(C.pn_data_get_binary(data)))
483
484 case *Symbol:
485 panicUnless(pnType == C.PN_SYMBOL, data, v)
486 *v = Symbol(goBytes(C.pn_data_get_symbol(data)))
487
488 case *time.Time:
489 panicUnless(pnType == C.PN_TIMESTAMP, data, v)
490 *v = goTime(C.pn_data_get_timestamp(data))
491
492 case *UUID:
493 panicUnless(pnType == C.PN_UUID, data, v)
494 pn := C.pn_data_get_uuid(data)
495 copy((*v)[:], C.GoBytes(unsafe.Pointer(&pn.bytes), 16))
496
497 case *AnnotationKey:
498 panicUnless(pnType == C.PN_ULONG || pnType == C.PN_SYMBOL || pnType == C.PN_STRING, data, v)
499 unmarshal(&v.value, data)
500
501 case *AnyMap:
502 panicUnless(C.pn_data_type(data) == C.PN_MAP, data, v)
503 n := int(C.pn_data_get_map(data)) / 2
504 if cap(*v) < n {
505 *v = make(AnyMap, n)
506 }
507 *v = (*v)[:n]
508 data.enter(*v)
509 defer data.exit(*v)
510 for i := 0; i < n; i++ {
511 data.next(*v)
512 unmarshal(&(*v)[i].Key, data)
513 data.next(*v)
514 unmarshal(&(*v)[i].Value, data)
515 }
516
517 case *interface{}:
518 getInterface(data, v)
519
520 default: // This is not one of the fixed well-known types, reflect for map and slice types
521
522 switch rt.Elem().Kind() {
523 case reflect.Map:
524 getMap(data, v)
525 case reflect.Slice:
526 getSequence(data, v)
527 default:
528 doPanic(data, v)
529 }
530 }
531 }
532
533 // Unmarshalling into an interface{} the type is determined by the AMQP source type,
534 // since the interface{} target can hold any Go type.
535 func getInterface(data *C.pn_data_t, vp *interface{}) {
536 pnType := C.pn_data_type(data)
537 switch pnType {
538 case C.PN_BOOL:
539 *vp = bool(C.pn_data_get_bool(data))
540 case C.PN_UBYTE:
541 *vp = uint8(C.pn_data_get_ubyte(data))
542 case C.PN_BYTE:
543 *vp = int8(C.pn_data_get_byte(data))
544 case C.PN_USHORT:
545 *vp = uint16(C.pn_data_get_ushort(data))
546 case C.PN_SHORT:
547 *vp = int16(C.pn_data_get_short(data))
548 case C.PN_UINT:
549 *vp = uint32(C.pn_data_get_uint(data))
550 case C.PN_INT:
551 *vp = int32(C.pn_data_get_int(data))
552 case C.PN_CHAR:
553 *vp = Char(C.pn_data_get_char(data))
554 case C.PN_ULONG:
555 *vp = uint64(C.pn_data_get_ulong(data))
556 case C.PN_LONG:
557 *vp = int64(C.pn_data_get_long(data))
558 case C.PN_FLOAT:
559 *vp = float32(C.pn_data_get_float(data))
560 case C.PN_DOUBLE:
561 *vp = float64(C.pn_data_get_double(data))
562 case C.PN_BINARY:
563 *vp = Binary(goBytes(C.pn_data_get_binary(data)))
564 case C.PN_STRING:
565 *vp = goString(C.pn_data_get_string(data))
566 case C.PN_SYMBOL:
567 *vp = Symbol(goString(C.pn_data_get_symbol(data)))
568 case C.PN_TIMESTAMP:
569 *vp = goTime(C.pn_data_get_timestamp(data))
570 case C.PN_UUID:
571 var u UUID
572 unmarshal(&u, data)
573 *vp = u
574 case C.PN_MAP:
575 // We will try to unmarshal as a Map first, if that fails try AnyMap
576 m := make(Map, int(C.pn_data_get_map(data))/2)
577 if err := recoverUnmarshal(&m, data); err == nil {
578 *vp = m
579 } else {
580 am := make(AnyMap, int(C.pn_data_get_map(data))/2)
581 unmarshal(&am, data)
582 *vp = am
583 }
584 case C.PN_LIST:
585 l := List{}
586 unmarshal(&l, data)
587 *vp = l
588 case C.PN_ARRAY:
589 sp := getArrayStore(data) // interface{} containing T* for suitable T
590 unmarshal(sp, data)
591 *vp = reflect.ValueOf(sp).Elem().Interface()
592 case C.PN_DESCRIBED:
593 d := Described{}
594 unmarshal(&d, data)
595 *vp = d
596 case C.PN_NULL:
597 *vp = nil
598 case C.PN_INVALID:
599 // Allow decoding from an empty data object to an interface, treat it like NULL.
600 // This happens when optional values or properties are omitted from a message.
601 *vp = nil
602 default: // Don't know how to handle this
603 panic(newUnmarshalError(pnType, vp))
604 }
605 }
606
607 // Return an interface{} containing a pointer to an appropriate slice or Array
608 func getArrayStore(data *C.pn_data_t) interface{} {
609 // TODO aconway 2017-11-10: described arrays.
610 switch C.pn_data_get_array_type(data) {
611 case C.PN_BOOL:
612 return new([]bool)
613 case C.PN_UBYTE:
614 return new([]uint8)
615 case C.PN_BYTE:
616 return new([]int8)
617 case C.PN_USHORT:
618 return new([]uint16)
619 case C.PN_SHORT:
620 return new([]int16)
621 case C.PN_UINT:
622 return new([]uint32)
623 case C.PN_INT:
624 return new([]int32)
625 case C.PN_CHAR:
626 return new([]Char)
627 case C.PN_ULONG:
628 return new([]uint64)
629 case C.PN_LONG:
630 return new([]int64)
631 case C.PN_FLOAT:
632 return new([]float32)
633 case C.PN_DOUBLE:
634 return new([]float64)
635 case C.PN_BINARY:
636 return new([]Binary)
637 case C.PN_STRING:
638 return new([]string)
639 case C.PN_SYMBOL:
640 return new([]Symbol)
641 case C.PN_TIMESTAMP:
642 return new([]time.Time)
643 case C.PN_UUID:
644 return new([]UUID)
645 }
646 return new(Array) // Not a simple type, use generic Array
647 }
648
649 var typeOfInterface = reflect.TypeOf(interface{}(nil))
650
651 // get into map pointed at by v
652 func getMap(data *C.pn_data_t, v interface{}) {
653 panicUnless(C.pn_data_type(data) == C.PN_MAP, data, v)
654 n := int(C.pn_data_get_map(data)) / 2
655 mapValue := reflect.ValueOf(v).Elem()
656 mapValue.Set(reflect.MakeMap(mapValue.Type())) // Clear the map
657 data.enter(v)
658 defer data.exit(v)
659 // Allocate re-usable key/val values
660 keyType := mapValue.Type().Key()
661 keyPtr := reflect.New(keyType)
662 valPtr := reflect.New(mapValue.Type().Elem())
663 for i := 0; i < n; i++ {
664 data.next(v)
665 unmarshal(keyPtr.Interface(), data)
666 if keyType.Kind() == reflect.Interface && !keyPtr.Elem().Elem().Type().Comparable() {
667 doPanicMsg(data, v, fmt.Sprintf("key %#v is not comparable", keyPtr.Elem().Interface()))
668 }
669 data.next(v)
670 unmarshal(valPtr.Interface(), data)
671 mapValue.SetMapIndex(keyPtr.Elem(), valPtr.Elem())
672 }
673 }
674
675 func getSequence(data *C.pn_data_t, vp interface{}) {
676 var count int
677 pnType := C.pn_data_type(data)
678 switch pnType {
679 case C.PN_LIST:
680 count = int(C.pn_data_get_list(data))
681 case C.PN_ARRAY:
682 count = int(C.pn_data_get_array(data))
683 default:
684 doPanic(data, vp)
685 }
686 listValue := reflect.MakeSlice(reflect.TypeOf(vp).Elem(), count, count)
687 data.enter(vp)
688 defer data.exit(vp)
689 for i := 0; i < count; i++ {
690 data.next(vp)
691 val := reflect.New(listValue.Type().Elem())
692 unmarshal(val.Interface(), data)
693 listValue.Index(i).Set(val.Elem())
694 }
695 reflect.ValueOf(vp).Elem().Set(listValue)
696 }
697
698 func getDescribed(data *C.pn_data_t, vp interface{}) {
699 d, isDescribed := vp.(*Described)
700 data.enter(vp)
701 defer data.exit(vp)
702 data.next(vp)
703 if isDescribed {
704 unmarshal(&d.Descriptor, data)
705 data.next(vp)
706 unmarshal(&d.Value, data)
707 } else {
708 data.next(vp) // Skip descriptor
709 unmarshal(vp, data) // Unmarshal plain value
710 }
711 }
712
713 // decode from bytes.
714 // Return bytes decoded or 0 if we could not decode a complete object.
715 //
716 func decode(data *C.pn_data_t, bytes []byte) (int, error) {
717 n := C.pn_data_decode(data, cPtr(bytes), cLen(bytes))
718 if n == C.PN_UNDERFLOW {
719 C.pn_error_clear(C.pn_data_error(data))
720 return 0, EndOfData
721 } else if n <= 0 {
722 return 0, &UnmarshalError{s: fmt.Sprintf("unmarshal %v", PnErrorCode(n))}
723 }
724 return int(n), nil
725 }
726
727 // Checked versions of pn_data functions
728
729 func (data *C.pn_data_t) enter(v interface{}) { checkOp(bool(C.pn_data_enter(data)), v) }
730 func (data *C.pn_data_t) exit(v interface{}) { checkOp(bool(C.pn_data_exit(data)), v) }
731 func (data *C.pn_data_t) next(v interface{}) { checkOp(bool(C.pn_data_next(data)), v) }