| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or 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 |
"fmt" |
| 27 |
"io" |
| 28 |
"reflect" |
| 29 |
"time" |
| 30 |
"unsafe" |
| 31 |
) |
| 32 |
|
| 33 |
// Error returned if Go data cannot be marshaled as an AMQP type. |
| 34 |
type MarshalError struct { |
| 35 |
// The Go type. |
| 36 |
GoType reflect.Type |
| 37 |
s string |
| 38 |
} |
| 39 |
|
| 40 |
func (e MarshalError) Error() string { return e.s } |
| 41 |
|
| 42 |
func newMarshalError(v interface{}, s string) *MarshalError { |
| 43 |
t := reflect.TypeOf(v) |
| 44 |
return &MarshalError{GoType: t, s: fmt.Sprintf("cannot marshal %s: %s", t, s)} |
| 45 |
} |
| 46 |
|
| 47 |
func dataMarshalError(v interface{}, data *C.pn_data_t) error { |
| 48 |
if pe := PnError(C.pn_data_error(data)); pe != nil { |
| 49 |
return newMarshalError(v, pe.Error()) |
| 50 |
} |
| 51 |
return nil |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
Marshal encodes a Go value as AMQP data in buffer. |
| 56 |
If buffer is nil, or is not large enough, a new buffer is created. |
| 57 |
|
| 58 |
Returns the buffer used for encoding with len() adjusted to the actual size of data. |
| 59 |
|
| 60 |
Go types are encoded as follows |
| 61 |
|
| 62 |
+-------------------------------------+--------------------------------------------+ |
| 63 |
|Go type |AMQP type | |
| 64 |
+-------------------------------------+--------------------------------------------+ |
| 65 |
|bool |bool | |
| 66 |
+-------------------------------------+--------------------------------------------+ |
| 67 |
|int8, int16, int32, int64 (int) |byte, short, int, long (int or long) | |
| 68 |
+-------------------------------------+--------------------------------------------+ |
| 69 |
|uint8, uint16, uint32, uint64 (uint) |ubyte, ushort, uint, ulong (uint or ulong) | |
| 70 |
+-------------------------------------+--------------------------------------------+ |
| 71 |
|float32, float64 |float, double. | |
| 72 |
+-------------------------------------+--------------------------------------------+ |
| 73 |
|string |string | |
| 74 |
+-------------------------------------+--------------------------------------------+ |
| 75 |
|[]byte, Binary |binary | |
| 76 |
+-------------------------------------+--------------------------------------------+ |
| 77 |
|Symbol |symbol | |
| 78 |
+-------------------------------------+--------------------------------------------+ |
| 79 |
|Char |char | |
| 80 |
+-------------------------------------+--------------------------------------------+ |
| 81 |
|interface{} |the contained type | |
| 82 |
+-------------------------------------+--------------------------------------------+ |
| 83 |
|nil |null | |
| 84 |
+-------------------------------------+--------------------------------------------+ |
| 85 |
|map[K]T |map with K and T converted as above | |
| 86 |
+-------------------------------------+--------------------------------------------+ |
| 87 |
|Map |map, may have mixed types for keys, values | |
| 88 |
+-------------------------------------+--------------------------------------------+ |
| 89 |
|AnyMap |map (See AnyMap) | |
| 90 |
+-------------------------------------+--------------------------------------------+ |
| 91 |
|List, []interface{} |list, may have mixed-type values | |
| 92 |
+-------------------------------------+--------------------------------------------+ |
| 93 |
|[]T, [N]T |array, T is mapped as per this table | |
| 94 |
+-------------------------------------+--------------------------------------------+ |
| 95 |
|Described |described type | |
| 96 |
+-------------------------------------+--------------------------------------------+ |
| 97 |
|time.Time |timestamp | |
| 98 |
+-------------------------------------+--------------------------------------------+ |
| 99 |
|UUID |uuid | |
| 100 |
+-------------------------------------+--------------------------------------------+ |
| 101 |
|
| 102 |
The following Go types cannot be marshaled: uintptr, function, channel, struct, complex64/128 |
| 103 |
|
| 104 |
AMQP types not yet supported: decimal32/64/128 |
| 105 |
*/ |
| 106 |
|
| 107 |
func Marshal(v interface{}, buffer []byte) (outbuf []byte, err error) { |
| 108 |
data := C.pn_data(0) |
| 109 |
defer C.pn_data_free(data) |
| 110 |
if err = recoverMarshal(v, data); err != nil { |
| 111 |
return buffer, err |
| 112 |
} |
| 113 |
encode := func(buf []byte) ([]byte, error) { |
| 114 |
n := int(C.pn_data_encode(data, cPtr(buf), cLen(buf))) |
| 115 |
switch { |
| 116 |
case n == int(C.PN_OVERFLOW): |
| 117 |
return buf, overflow |
| 118 |
case n < 0: |
| 119 |
return buf, dataMarshalError(v, data) |
| 120 |
default: |
| 121 |
return buf[:n], nil |
| 122 |
} |
| 123 |
} |
| 124 |
return encodeGrow(buffer, encode) |
| 125 |
} |
| 126 |
|
| 127 |
// Internal use only |
| 128 |
func MarshalUnsafe(v interface{}, pnData unsafe.Pointer) (err error) { |
| 129 |
return recoverMarshal(v, (*C.pn_data_t)(pnData)) |
| 130 |
} |
| 131 |
|
| 132 |
func recoverMarshal(v interface{}, data *C.pn_data_t) (err error) { |
| 133 |
defer func() { // Convert panic to error return |
| 134 |
if r := recover(); r != nil { |
| 135 |
if err2, ok := r.(*MarshalError); ok { |
| 136 |
err = err2 // Convert internal panic to error |
| 137 |
} else { |
| 138 |
panic(r) // Unrecognized error, continue to panic |
| 139 |
} |
| 140 |
} |
| 141 |
}() |
| 142 |
marshal(v, data) // Panics on error |
| 143 |
return |
| 144 |
} |
| 145 |
|
| 146 |
const minEncode = 256 |
| 147 |
|
| 148 |
// overflow is returned when an encoding function can't fit data in the buffer. |
| 149 |
var overflow = fmt.Errorf("buffer too small") |
| 150 |
|
| 151 |
// encodeFn encodes into buffer[0:len(buffer)]. |
| 152 |
// Returns buffer with length adjusted for data encoded. |
| 153 |
// If buffer too small, returns overflow as error. |
| 154 |
type encodeFn func(buffer []byte) ([]byte, error) |
| 155 |
|
| 156 |
// encodeGrow calls encode() into buffer, if it returns overflow grows the buffer. |
| 157 |
// Returns the final buffer. |
| 158 |
func encodeGrow(buffer []byte, encode encodeFn) ([]byte, error) { |
| 159 |
if buffer == nil || len(buffer) == 0 { |
| 160 |
buffer = make([]byte, minEncode) |
| 161 |
} |
| 162 |
var err error |
| 163 |
for buffer, err = encode(buffer); err == overflow; buffer, err = encode(buffer) { |
| 164 |
buffer = make([]byte, 2*len(buffer)) |
| 165 |
} |
| 166 |
return buffer, err |
| 167 |
} |
| 168 |
|
| 169 |
// Marshal v to data |
| 170 |
func marshal(i interface{}, data *C.pn_data_t) { |
| 171 |
switch v := i.(type) { |
| 172 |
case nil: |
| 173 |
C.pn_data_put_null(data) |
| 174 |
case bool: |
| 175 |
C.pn_data_put_bool(data, C.bool(v)) |
| 176 |
|
| 177 |
// Signed integers |
| 178 |
case int8: |
| 179 |
C.pn_data_put_byte(data, C.int8_t(v)) |
| 180 |
case int16: |
| 181 |
C.pn_data_put_short(data, C.int16_t(v)) |
| 182 |
case int32: |
| 183 |
C.pn_data_put_int(data, C.int32_t(v)) |
| 184 |
case int64: |
| 185 |
C.pn_data_put_long(data, C.int64_t(v)) |
| 186 |
case int: |
| 187 |
if intIs64 { |
| 188 |
C.pn_data_put_long(data, C.int64_t(v)) |
| 189 |
} else { |
| 190 |
C.pn_data_put_int(data, C.int32_t(v)) |
| 191 |
} |
| 192 |
|
| 193 |
// Unsigned integers |
| 194 |
case uint8: |
| 195 |
C.pn_data_put_ubyte(data, C.uint8_t(v)) |
| 196 |
case uint16: |
| 197 |
C.pn_data_put_ushort(data, C.uint16_t(v)) |
| 198 |
case uint32: |
| 199 |
C.pn_data_put_uint(data, C.uint32_t(v)) |
| 200 |
case uint64: |
| 201 |
C.pn_data_put_ulong(data, C.uint64_t(v)) |
| 202 |
case uint: |
| 203 |
if intIs64 { |
| 204 |
C.pn_data_put_ulong(data, C.uint64_t(v)) |
| 205 |
} else { |
| 206 |
C.pn_data_put_uint(data, C.uint32_t(v)) |
| 207 |
} |
| 208 |
|
| 209 |
// Floating point |
| 210 |
case float32: |
| 211 |
C.pn_data_put_float(data, C.float(v)) |
| 212 |
case float64: |
| 213 |
C.pn_data_put_double(data, C.double(v)) |
| 214 |
|
| 215 |
// String-like (string, binary, symbol) |
| 216 |
case string: |
| 217 |
C.pn_data_put_string(data, pnBytes([]byte(v))) |
| 218 |
case []byte: |
| 219 |
C.pn_data_put_binary(data, pnBytes(v)) |
| 220 |
case Binary: |
| 221 |
C.pn_data_put_binary(data, pnBytes([]byte(v))) |
| 222 |
case Symbol: |
| 223 |
C.pn_data_put_symbol(data, pnBytes([]byte(v))) |
| 224 |
|
| 225 |
// Other simple types |
| 226 |
case time.Time: |
| 227 |
C.pn_data_put_timestamp(data, pnTime(v)) |
| 228 |
case UUID: |
| 229 |
C.pn_data_put_uuid(data, *(*C.pn_uuid_t)(unsafe.Pointer(&v[0]))) |
| 230 |
case Char: |
| 231 |
C.pn_data_put_char(data, (C.pn_char_t)(v)) |
| 232 |
|
| 233 |
// Described types |
| 234 |
case Described: |
| 235 |
C.pn_data_put_described(data) |
| 236 |
C.pn_data_enter(data) |
| 237 |
marshal(v.Descriptor, data) |
| 238 |
marshal(v.Value, data) |
| 239 |
C.pn_data_exit(data) |
| 240 |
|
| 241 |
// Restricted type annotation-key, marshals as contained value |
| 242 |
case AnnotationKey: |
| 243 |
marshal(v.Get(), data) |
| 244 |
|
| 245 |
// Special type to represent AMQP maps with keys that are illegal in Go |
| 246 |
case AnyMap: |
| 247 |
C.pn_data_put_map(data) |
| 248 |
C.pn_data_enter(data) |
| 249 |
defer C.pn_data_exit(data) |
| 250 |
for _, kv := range v { |
| 251 |
marshal(kv.Key, data) |
| 252 |
marshal(kv.Value, data) |
| 253 |
} |
| 254 |
|
| 255 |
default: |
| 256 |
// Examine complex types (Go map, slice, array) by reflected structure |
| 257 |
switch reflect.TypeOf(i).Kind() { |
| 258 |
|
| 259 |
case reflect.Map: |
| 260 |
m := reflect.ValueOf(v) |
| 261 |
C.pn_data_put_map(data) |
| 262 |
if C.pn_data_enter(data) { |
| 263 |
defer C.pn_data_exit(data) |
| 264 |
} else { |
| 265 |
panic(dataMarshalError(i, data)) |
| 266 |
} |
| 267 |
for _, key := range m.MapKeys() { |
| 268 |
marshal(key.Interface(), data) |
| 269 |
marshal(m.MapIndex(key).Interface(), data) |
| 270 |
} |
| 271 |
|
| 272 |
case reflect.Slice, reflect.Array: |
| 273 |
// Note: Go array and slice are mapped the same way: |
| 274 |
// if element type is an interface, map to AMQP list (mixed type) |
| 275 |
// if element type is a non-interface type map to AMQP array (single type) |
| 276 |
s := reflect.ValueOf(v) |
| 277 |
if pnType, ok := arrayTypeMap[s.Type().Elem()]; ok { |
| 278 |
C.pn_data_put_array(data, false, pnType) |
| 279 |
} else { |
| 280 |
C.pn_data_put_list(data) |
| 281 |
} |
| 282 |
C.pn_data_enter(data) |
| 283 |
defer C.pn_data_exit(data) |
| 284 |
for j := 0; j < s.Len(); j++ { |
| 285 |
marshal(s.Index(j).Interface(), data) |
| 286 |
} |
| 287 |
|
| 288 |
default: |
| 289 |
panic(newMarshalError(v, "no conversion")) |
| 290 |
} |
| 291 |
} |
| 292 |
if err := dataMarshalError(i, data); err != nil { |
| 293 |
panic(err) |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// Mapping froo Go element type to AMQP array type for types that can go in an AMQP array |
| 298 |
// NOTE: this must be kept consistent with marshal() which does the actual marshalling. |
| 299 |
var arrayTypeMap = map[reflect.Type]C.pn_type_t{ |
| 300 |
nil: C.PN_NULL, |
| 301 |
reflect.TypeOf(true): C.PN_BOOL, |
| 302 |
|
| 303 |
reflect.TypeOf(int8(0)): C.PN_BYTE, |
| 304 |
reflect.TypeOf(int16(0)): C.PN_INT, |
| 305 |
reflect.TypeOf(int32(0)): C.PN_SHORT, |
| 306 |
reflect.TypeOf(int64(0)): C.PN_LONG, |
| 307 |
|
| 308 |
reflect.TypeOf(uint8(0)): C.PN_UBYTE, |
| 309 |
reflect.TypeOf(uint16(0)): C.PN_UINT, |
| 310 |
reflect.TypeOf(uint32(0)): C.PN_USHORT, |
| 311 |
reflect.TypeOf(uint64(0)): C.PN_ULONG, |
| 312 |
|
| 313 |
reflect.TypeOf(float32(0)): C.PN_FLOAT, |
| 314 |
reflect.TypeOf(float64(0)): C.PN_DOUBLE, |
| 315 |
|
| 316 |
reflect.TypeOf(""): C.PN_STRING, |
| 317 |
reflect.TypeOf((*Symbol)(nil)).Elem(): C.PN_SYMBOL, |
| 318 |
reflect.TypeOf((*Binary)(nil)).Elem(): C.PN_BINARY, |
| 319 |
reflect.TypeOf([]byte{}): C.PN_BINARY, |
| 320 |
|
| 321 |
reflect.TypeOf((*time.Time)(nil)).Elem(): C.PN_TIMESTAMP, |
| 322 |
reflect.TypeOf((*UUID)(nil)).Elem(): C.PN_UUID, |
| 323 |
reflect.TypeOf((*Char)(nil)).Elem(): C.PN_CHAR, |
| 324 |
} |
| 325 |
|
| 326 |
// Compute mapping of int/uint at runtime as they depend on execution environment. |
| 327 |
func init() { |
| 328 |
if intIs64 { |
| 329 |
arrayTypeMap[reflect.TypeOf(int(0))] = C.PN_LONG |
| 330 |
arrayTypeMap[reflect.TypeOf(uint(0))] = C.PN_ULONG |
| 331 |
} else { |
| 332 |
arrayTypeMap[reflect.TypeOf(int(0))] = C.PN_INT |
| 333 |
arrayTypeMap[reflect.TypeOf(uint(0))] = C.PN_UINT |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func clearMarshal(v interface{}, data *C.pn_data_t) { |
| 338 |
C.pn_data_clear(data) |
| 339 |
marshal(v, data) |
| 340 |
} |
| 341 |
|
| 342 |
// Encoder encodes AMQP values to an io.Writer |
| 343 |
type Encoder struct { |
| 344 |
writer io.Writer |
| 345 |
buffer []byte |
| 346 |
} |
| 347 |
|
| 348 |
// New encoder returns a new encoder that writes to w. |
| 349 |
func NewEncoder(w io.Writer) *Encoder { |
| 350 |
return &Encoder{w, make([]byte, minEncode)} |
| 351 |
} |
| 352 |
|
| 353 |
func (e *Encoder) Encode(v interface{}) (err error) { |
| 354 |
e.buffer, err = Marshal(v, e.buffer) |
| 355 |
if err == nil { |
| 356 |
_, err = e.writer.Write(e.buffer) |
| 357 |
} |
| 358 |
return err |
| 359 |
} |