Debian Codegraph @

go/pkg/amqp/types.go

raw
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 "bytes"
27 "fmt"
28 "time"
29 "unsafe"
30 )
31
32 func (t C.pn_type_t) String() string {
33 switch C.pn_type_t(t) {
34 case C.PN_NULL:
35 return "null"
36 case C.PN_BOOL:
37 return "bool"
38 case C.PN_UBYTE:
39 return "ubyte"
40 case C.PN_BYTE:
41 return "byte"
42 case C.PN_USHORT:
43 return "ushort"
44 case C.PN_SHORT:
45 return "short"
46 case C.PN_CHAR:
47 return "char"
48 case C.PN_UINT:
49 return "uint"
50 case C.PN_INT:
51 return "int"
52 case C.PN_ULONG:
53 return "ulong"
54 case C.PN_LONG:
55 return "long"
56 case C.PN_TIMESTAMP:
57 return "timestamp"
58 case C.PN_FLOAT:
59 return "float"
60 case C.PN_DOUBLE:
61 return "double"
62 case C.PN_DECIMAL32:
63 return "decimal32"
64 case C.PN_DECIMAL64:
65 return "decimal64"
66 case C.PN_DECIMAL128:
67 return "decimal128"
68 case C.PN_UUID:
69 return "uuid"
70 case C.PN_BINARY:
71 return "binary"
72 case C.PN_STRING:
73 return "string"
74 case C.PN_SYMBOL:
75 return "symbol"
76 case C.PN_DESCRIBED:
77 return "described"
78 case C.PN_ARRAY:
79 return "array"
80 case C.PN_LIST:
81 return "list"
82 case C.PN_MAP:
83 return "map"
84 default:
85 return fmt.Sprintf("<bad-type %v>", int(t))
86 }
87 }
88
89 // The AMQP map type. A generic map that can have mixed-type keys and values.
90 type Map map[interface{}]interface{}
91
92 // The most general AMQP map type, for unusual interoperability cases.
93 //
94 // This is not a Go Map but a sequence of {key, value} pairs.
95 //
96 // An AnyMap lets you control or examine the encoded ordering of key,value pairs
97 // and use key values that are not legal as Go map keys.
98 //
99 // The amqp.Map, or plain Go map types are easier to use for most cases.
100 type AnyMap []KeyValue
101
102 // Return a Map constructed from an AnyMap.
103 // Panic if the AnyMap has key values that are not valid Go map keys (e.g. maps, slices)
104 func (a AnyMap) Map() (m Map) {
105 for _, kv := range a {
106 m[kv.Key] = kv.Value
107 }
108 return
109 }
110
111 // KeyValue pair, used by AnyMap
112 type KeyValue struct{ Key, Value interface{} }
113
114 // The AMQP list type. A generic list that can hold mixed-type values.
115 type List []interface{}
116
117 // The generic AMQP array type, used to unmarshal an array with nested array,
118 // map or list elements. Arrays of simple type T unmarshal to []T
119 type Array []interface{}
120
121 // Symbol is a string that is encoded as an AMQP symbol
122 type Symbol string
123
124 func (s Symbol) String() string { return string(s) }
125 func (s Symbol) GoString() string { return fmt.Sprintf("s\"%s\"", s) }
126
127 // Binary is a string that is encoded as an AMQP binary.
128 // It is a string rather than a byte[] because byte[] is not hashable and can't be used as
129 // a map key, AMQP frequently uses binary types as map keys. It can convert to and from []byte
130 type Binary string
131
132 func (b Binary) String() string { return string(b) }
133 func (b Binary) GoString() string { return fmt.Sprintf("b\"%s\"", b) }
134
135 // GoString for Map prints values with their types, useful for debugging.
136 func (m Map) GoString() string {
137 out := &bytes.Buffer{}
138 fmt.Fprintf(out, "%T{", m)
139 i := len(m)
140 for k, v := range m {
141 fmt.Fprintf(out, "%T(%#v): %T(%#v)", k, k, v, v)
142 i--
143 if i > 0 {
144 fmt.Fprint(out, ", ")
145 }
146 }
147 fmt.Fprint(out, "}")
148 return out.String()
149 }
150
151 // GoString for List prints values with their types, useful for debugging.
152 func (l List) GoString() string {
153 out := &bytes.Buffer{}
154 fmt.Fprintf(out, "%T{", l)
155 for i := 0; i < len(l); i++ {
156 fmt.Fprintf(out, "%T(%#v)", l[i], l[i])
157 if i == len(l)-1 {
158 fmt.Fprint(out, ", ")
159 }
160 }
161 fmt.Fprint(out, "}")
162 return out.String()
163 }
164
165 // pnTime converts Go time.Time to Proton millisecond Unix time.
166 // Take care to convert zero values to zero values.
167 func pnTime(t time.Time) C.pn_timestamp_t {
168 if t.IsZero() {
169 return C.pn_timestamp_t(0)
170 }
171 return C.pn_timestamp_t(t.UnixNano() / int64(time.Millisecond))
172 }
173
174 // goTime converts a pn_timestamp_t to a Go time.Time.
175 // Take care to convert zero values to zero values.
176 func goTime(t C.pn_timestamp_t) time.Time {
177 if t == 0 {
178 return time.Time{}
179 }
180 return time.Unix(0, int64(t)*int64(time.Millisecond))
181 }
182
183 func pnDuration(d time.Duration) C.pn_millis_t {
184 return (C.pn_millis_t)(d / (time.Millisecond))
185 }
186
187 func goDuration(d C.pn_millis_t) time.Duration {
188 return time.Duration(d) * time.Millisecond
189 }
190
191 func goBytes(cBytes C.pn_bytes_t) (bytes []byte) {
192 if cBytes.start != nil {
193 bytes = C.GoBytes(unsafe.Pointer(cBytes.start), C.int(cBytes.size))
194 }
195 return
196 }
197
198 func goString(cBytes C.pn_bytes_t) (str string) {
199 if cBytes.start != nil {
200 str = C.GoStringN(cBytes.start, C.int(cBytes.size))
201 }
202 return
203 }
204
205 func pnBytes(b []byte) C.pn_bytes_t {
206 if len(b) == 0 {
207 return C.pn_bytes_t{0, nil}
208 } else {
209 return C.pn_bytes_t{C.size_t(len(b)), (*C.char)(unsafe.Pointer(&b[0]))}
210 }
211 }
212
213 func cPtr(b []byte) *C.char {
214 if len(b) == 0 {
215 return nil
216 }
217 return (*C.char)(unsafe.Pointer(&b[0]))
218 }
219
220 func cLen(b []byte) C.size_t {
221 return C.size_t(len(b))
222 }
223
224 // AnnotationKey is used as a map key for AMQP annotation maps which are
225 // allowed to have keys that are either symbol or ulong but no other type.
226 //
227 type AnnotationKey struct {
228 value interface{}
229 }
230
231 func AnnotationKeySymbol(v Symbol) AnnotationKey { return AnnotationKey{v} }
232 func AnnotationKeyUint64(v uint64) AnnotationKey { return AnnotationKey{v} }
233 func AnnotationKeyString(v string) AnnotationKey { return AnnotationKey{Symbol(v)} }
234
235 // Returns the value which must be Symbol, uint64 or nil
236 func (k AnnotationKey) Get() interface{} { return k.value }
237
238 func (k AnnotationKey) String() string { return fmt.Sprintf("%v", k.Get()) }
239
240 // Described represents an AMQP described type, which is really
241 // just a pair of AMQP values - the first is treated as a "descriptor",
242 // and is normally a string or ulong providing information about the type.
243 // The second is the "value" and can be any AMQP value.
244 type Described struct {
245 Descriptor interface{}
246 Value interface{}
247 }
248
249 // UUID is an AMQP 128-bit Universally Unique Identifier, as defined by RFC-4122 section 4.1.2
250 type UUID [16]byte
251
252 func (u UUID) String() string {
253 return fmt.Sprintf("UUID(%x-%x-%x-%x-%x)", u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
254 }
255
256 // Char is an AMQP unicode character, equivalent to a Go rune.
257 // It is defined as a distinct type so it can be distinguished from an AMQP int
258 type Char rune
259
260 const intIs64 = unsafe.Sizeof(int(0)) == 8