Beispiel #1
0
/*
Unmarshal decodes AMQP-encoded bytes and stores the result in the value pointed to by v.
Types are converted as follows:

 +---------------------------+----------------------------------------------------------------------+
 |To Go types                |From AMQP types                                                       |
 +===========================+======================================================================+
 |bool                       |bool                                                                  |
 +---------------------------+----------------------------------------------------------------------+
 |int, int8, int16,          |Equivalent or smaller signed integer type: byte, short, int, long.    |
 |int32, int64               |                                                                      |
 +---------------------------+----------------------------------------------------------------------+
 |uint, uint8, uint16,       |Equivalent or smaller unsigned integer type: ubyte, ushort, uint,     |
 |uint32, uint64 types       |ulong                                                                 |
 +---------------------------+----------------------------------------------------------------------+
 |float32, float64           |Equivalent or smaller float or double.                                |
 +---------------------------+----------------------------------------------------------------------+
 |string, []byte             |string, symbol or binary.                                             |
 +---------------------------+----------------------------------------------------------------------+
 |Symbol                     |symbol                                                                |
 +---------------------------+----------------------------------------------------------------------+
 |map[K]T                    |map, provided all keys and values can unmarshal to types K, T         |
 +---------------------------+----------------------------------------------------------------------+
 |Map                        |map, any AMQP map                                                     |
 +---------------------------+----------------------------------------------------------------------+
 |interface{}                |Any AMQP value can be unmarshaled to an interface{} as follows:       |
 |                           +------------------------+---------------------------------------------+
 |                           |AMQP Type               |Go Type in interface{}                       |
 |                           +========================+=============================================+
 |                           |bool                    |bool                                         |
 |                           +------------------------+---------------------------------------------+
 |                           |byte,short,int,long     |int8,int16,int32,int64                       |
 |                           +------------------------+---------------------------------------------+
 |                           |ubyte,ushort,uint,ulong |uint8,uint16,uint32,uint64                   |
 |                           +------------------------+---------------------------------------------+
 |                           |float, double           |float32, float64                             |
 |                           +------------------------+---------------------------------------------+
 |                           |string                  |string                                       |
 |                           +------------------------+---------------------------------------------+
 |                           |symbol                  |Symbol                                       |
 |                           +------------------------+---------------------------------------------+
 |                           |binary                  |Binary                                       |
 |                           +------------------------+---------------------------------------------+
 |                           |nulll                   |nil                                          |
 |                           +------------------------+---------------------------------------------+
 |                           |map                     |Map                                          |
 |                           +------------------------+---------------------------------------------+
 |                           |list                    |List                                         |
 +---------------------------+------------------------+---------------------------------------------+

The following Go types cannot be unmarshaled: uintptr, function, interface, channel.

TODO

Go types: array, struct.

AMQP types: decimal32/64/128, char (round trip), timestamp, uuid, array, multi-section message bodies.

AMQP maps with mixed/unhashable key types need an alternate representation.

Described types.
*/
func Unmarshal(bytes []byte, v interface{}) (n int, err error) {
	defer doRecover(&err)

	data := C.pn_data(0)
	defer C.pn_data_free(data)
	n = decode(data, bytes)
	if n == 0 {
		err = internal.Errorf("not enough data")
	} else {
		unmarshal(v, data)
	}
	return
}
Beispiel #2
0
/*
Unmarshal decodes AMQP-encoded bytes and stores the result in the value pointed to by v.
Types are converted as follows:

 +---------------------------+----------------------------------------------------------------------+
 |To Go types                |From AMQP types                                                       |
 +===========================+======================================================================+
 |bool                       |bool                                                                  |
 +---------------------------+----------------------------------------------------------------------+
 |int, int8, int16,          |Equivalent or smaller signed integer type: byte, short, int, long.    |
 |int32, int64               |                                                                      |
 +---------------------------+----------------------------------------------------------------------+
 |uint, uint8, uint16,       |Equivalent or smaller unsigned integer type: ubyte, ushort, uint,     |
 |uint32, uint64 types       |ulong                                                                 |
 +---------------------------+----------------------------------------------------------------------+
 |float32, float64           |Equivalent or smaller float or double.                                |
 +---------------------------+----------------------------------------------------------------------+
 |string, []byte             |string, symbol or binary.                                             |
 +---------------------------+----------------------------------------------------------------------+
 |Symbol                     |symbol                                                                |
 +---------------------------+----------------------------------------------------------------------+
 |map[K]T                    |map, provided all keys and values can unmarshal to types K, T         |
 +---------------------------+----------------------------------------------------------------------+
 |Map                        |map, any AMQP map                                                     |
 +---------------------------+----------------------------------------------------------------------+
 |interface{}                |Any AMQP value can be unmarshaled to an interface{} as follows:       |
 |                           +------------------------+---------------------------------------------+
 |                           |AMQP Type               |Go Type in interface{}                       |
 |                           +========================+=============================================+
 |                           |bool                    |bool                                         |
 |                           +------------------------+---------------------------------------------+
 |                           |byte,short,int,long     |int8,int16,int32,int64                       |
 |                           +------------------------+---------------------------------------------+
 |                           |ubyte,ushort,uint,ulong |uint8,uint16,uint32,uint64                   |
 |                           +------------------------+---------------------------------------------+
 |                           |float, double           |float32, float64                             |
 |                           +------------------------+---------------------------------------------+
 |                           |string                  |string                                       |
 |                           +------------------------+---------------------------------------------+
 |                           |symbol                  |Symbol                                       |
 |                           +------------------------+---------------------------------------------+
 |                           |binary                  |Binary                                       |
 |                           +------------------------+---------------------------------------------+
 |                           |nulll                   |nil                                          |
 |                           +------------------------+---------------------------------------------+
 |                           |map                     |Map                                          |
 |                           +------------------------+---------------------------------------------+
 |                           |list                    |List                                         |
 +---------------------------+------------------------+---------------------------------------------+

The following Go types cannot be unmarshaled: uintptr, function, interface, channel.

TODO

Go types: array, struct.

AMQP types: decimal32/64/128, char (round trip), timestamp, uuid, array, multi-section message bodies.

AMQP maps with mixed/unhashable key types need an alternate representation.

Described types.
*/
func Unmarshal(bytes []byte, v interface{}) (n int, err error) {
	defer doRecover(&err)

	data := C.pn_data(0)
	defer C.pn_data_free(data)
	n, err = decode(data, bytes)
	if err != nil {
		return 0, err
	}
	if n == 0 {
		return 0, fmt.Errorf("not enough data")
	} else {
		unmarshal(v, data)
	}
	return n, nil
}
Beispiel #3
0
// Decode reads the next AMQP value from the Reader and stores it in the value pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of AMQP into a Go value.
//
func (d *Decoder) Decode(v interface{}) (err error) {
	defer doRecover(&err)
	data := C.pn_data(0)
	defer C.pn_data_free(data)
	var n int
	for n == 0 && err == nil {
		n = decode(data, d.buffer.Bytes())
		if n == 0 { // n == 0 means not enough data, read more
			err = d.more()
		} else {
			unmarshal(v, data)
		}
	}
	d.buffer.Next(n)
	return
}
Beispiel #4
0
/*
Marshal encodes a Go value as AMQP data in buffer.
If buffer is nil, or is not large enough, a new buffer  is created.

Returns the buffer used for encoding with len() adjusted to the actual size of data.

Go types are encoded as follows

 +-------------------------------------+--------------------------------------------+
 |Go type                              |AMQP type                                   |
 +-------------------------------------+--------------------------------------------+
 |bool                                 |bool                                        |
 +-------------------------------------+--------------------------------------------+
 |int8, int16, int32, int64 (int)      |byte, short, int, long (int or long)        |
 +-------------------------------------+--------------------------------------------+
 |uint8, uint16, uint32, uint64 (uint) |ubyte, ushort, uint, ulong (uint or ulong)  |
 +-------------------------------------+--------------------------------------------+
 |float32, float64                     |float, double.                              |
 +-------------------------------------+--------------------------------------------+
 |string                               |string                                      |
 +-------------------------------------+--------------------------------------------+
 |[]byte, Binary                       |binary                                      |
 +-------------------------------------+--------------------------------------------+
 |Symbol                               |symbol                                      |
 +-------------------------------------+--------------------------------------------+
 |interface{}                          |the contained type                          |
 +-------------------------------------+--------------------------------------------+
 |nil                                  |null                                        |
 +-------------------------------------+--------------------------------------------+
 |map[K]T                              |map with K and T converted as above         |
 +-------------------------------------+--------------------------------------------+
 |Map                                  |map, may have mixed types for keys, values  |
 +-------------------------------------+--------------------------------------------+
 |[]T                                  |list with T converted as above              |
 +-------------------------------------+--------------------------------------------+
 |List                                 |list, may have mixed types  values          |
 +-------------------------------------+--------------------------------------------+

The following Go types cannot be marshaled: uintptr, function, interface, channel

TODO

Go types: array, slice, struct, complex64/128.

AMQP types: decimal32/64/128, char, timestamp, uuid, array, multi-section message bodies.

Described types.

*/
func Marshal(v interface{}, buffer []byte) (outbuf []byte, err error) {
	defer doRecover(&err)
	data := C.pn_data(0)
	defer C.pn_data_free(data)
	marshal(v, data)
	encode := func(buf []byte) ([]byte, error) {
		n := int(C.pn_data_encode(data, cPtr(buf), cLen(buf)))
		switch {
		case n == int(C.PN_OVERFLOW):
			return buf, overflow
		case n < 0:
			return buf, dataError("marshal error", data)
		default:
			return buf[:n], nil
		}
	}
	return encodeGrow(buffer, encode)
}
Beispiel #5
0
func (pd pnDecoder) free()    { C.pn_data_free((*C.pn_data_t)(pd.data)) }
Beispiel #6
0
func (d Data) Free()                   { C.pn_data_free(d.pn) }