-
Notifications
You must be signed in to change notification settings - Fork 288
/
key.go
371 lines (323 loc) · 8.22 KB
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package exptel
//go:generate stringer -type=ValueType
import (
"encoding/json"
"fmt"
"strconv"
"unsafe"
"go.opentelemetry.io/otel/attribute"
)
// Key represents the key part in key-value pairs. It's a string. The
// allowed character set in the key depends on the use of the key.
type Key string
// KeyValue holds a key and value pair.
type KeyValue struct {
Key Key
Value Value
}
func NewKeyValuesFromOtel(kv []attribute.KeyValue) []KeyValue {
if kv == nil {
return nil
}
out := make([]KeyValue, len(kv))
for i := range kv {
var v Value
// NOTE: array types are not supported and will result in an empty key-value after conversion
switch kv[i].Value.Type() {
case attribute.BOOL:
v = Bool(kv[i].Value.AsBool())
case attribute.INT64:
v = Int64(kv[i].Value.AsInt64())
case attribute.FLOAT64:
v = Float64(kv[i].Value.AsFloat64())
case attribute.STRING:
v = String(kv[i].Value.AsString())
}
out[i] = KeyValue{
Key: Key(kv[i].Key),
Value: v,
}
}
return out
}
// ValueType describes the type of the data Value holds.
type ValueType int
// Value represents the value part in key-value pairs.
type Value struct {
vtype ValueType
numeric uint64
stringly string
// TODO Lazy value type?
}
const (
INVALID ValueType = iota // No value.
BOOL // Boolean value, use AsBool() to get it.
INT32 // 32 bit signed integral value, use AsInt32() to get it.
INT64 // 64 bit signed integral value, use AsInt64() to get it.
UINT32 // 32 bit unsigned integral value, use AsUint32() to get it.
UINT64 // 64 bit unsigned integral value, use AsUint64() to get it.
FLOAT32 // 32 bit floating point value, use AsFloat32() to get it.
FLOAT64 // 64 bit floating point value, use AsFloat64() to get it.
STRING // String value, use AsString() to get it.
)
// Bool creates a BOOL Value.
func Bool(v bool) Value {
return Value{
vtype: BOOL,
numeric: boolToRaw(v),
}
}
// Int64 creates an INT64 Value.
func Int64(v int64) Value {
return Value{
vtype: INT64,
numeric: int64ToRaw(v),
}
}
// Uint64 creates a UINT64 Value.
func Uint64(v uint64) Value {
return Value{
vtype: UINT64,
numeric: uint64ToRaw(v),
}
}
// Float64 creates a FLOAT64 Value.
func Float64(v float64) Value {
return Value{
vtype: FLOAT64,
numeric: float64ToRaw(v),
}
}
// Int32 creates an INT32 Value.
func Int32(v int32) Value {
return Value{
vtype: INT32,
numeric: int32ToRaw(v),
}
}
// Uint32 creates a UINT32 Value.
func Uint32(v uint32) Value {
return Value{
vtype: UINT32,
numeric: uint32ToRaw(v),
}
}
// Float32 creates a FLOAT32 Value.
func Float32(v float32) Value {
return Value{
vtype: FLOAT32,
numeric: float32ToRaw(v),
}
}
// String creates a STRING Value.
func String(v string) Value {
return Value{
vtype: STRING,
stringly: v,
}
}
// Int creates either an INT32 or an INT64 Value, depending on whether
// the int type is 32 or 64 bits wide.
func Int(v int) Value {
if unsafe.Sizeof(v) == 4 {
return Int32(int32(v))
}
return Int64(int64(v))
}
// Uint creates either a UINT32 or a UINT64 Value, depending on
// whether the uint type is 32 or 64 bits wide.
func Uint(v uint) Value {
if unsafe.Sizeof(v) == 4 {
return Uint32(uint32(v))
}
return Uint64(uint64(v))
}
// Bool creates a KeyValue instance with a BOOL Value.
func (k Key) Bool(v bool) KeyValue {
return KeyValue{
Key: k,
Value: Bool(v),
}
}
// Int64 creates a KeyValue instance with an INT64 Value.
func (k Key) Int64(v int64) KeyValue {
return KeyValue{
Key: k,
Value: Int64(v),
}
}
// Uint64 creates a KeyValue instance with a UINT64 Value.
func (k Key) Uint64(v uint64) KeyValue {
return KeyValue{
Key: k,
Value: Uint64(v),
}
}
// Float64 creates a KeyValue instance with a FLOAT64 Value.
func (k Key) Float64(v float64) KeyValue {
return KeyValue{
Key: k,
Value: Float64(v),
}
}
// Int32 creates a KeyValue instance with an INT32 Value.
func (k Key) Int32(v int32) KeyValue {
return KeyValue{
Key: k,
Value: Int32(v),
}
}
// Uint32 creates a KeyValue instance with a UINT32 Value.
func (k Key) Uint32(v uint32) KeyValue {
return KeyValue{
Key: k,
Value: Uint32(v),
}
}
// Float32 creates a KeyValue instance with a FLOAT32 Value.
func (k Key) Float32(v float32) KeyValue {
return KeyValue{
Key: k,
Value: Float32(v),
}
}
// String creates a KeyValue instance with a STRING Value.
func (k Key) String(v string) KeyValue {
return KeyValue{
Key: k,
Value: String(v),
}
}
// Int creates a KeyValue instance with either an INT32 or an INT64
// Value, depending on whether the int type is 32 or 64 bits wide.
func (k Key) Int(v int) KeyValue {
return KeyValue{
Key: k,
Value: Int(v),
}
}
// Uint creates a KeyValue instance with either an UINT32 or an UINT64
// Value, depending on whether the uint type is 32 or 64 bits wide.
func (k Key) Uint(v uint) KeyValue {
return KeyValue{
Key: k,
Value: Uint(v),
}
}
// Defined returns true for non-empty keys.
func (k Key) Defined() bool {
return len(k) != 0
}
// Type returns a type of the Value.
func (v *Value) Type() ValueType {
return v.vtype
}
// Bool returns the bool value. Make sure that the Value's type is
// BOOL.
func (v *Value) AsBool() bool {
return rawToBool(v.numeric)
}
// AsInt32 returns the int32 value. Make sure that the Value's type is
// INT32.
func (v *Value) AsInt32() int32 {
return rawToInt32(v.numeric)
}
// AsInt64 returns the int64 value. Make sure that the Value's type is
// INT64.
func (v *Value) AsInt64() int64 {
return rawToInt64(v.numeric)
}
// AsUint32 returns the uint32 value. Make sure that the Value's type
// is UINT32.
func (v *Value) AsUint32() uint32 {
return rawToUint32(v.numeric)
}
// AsUint64 returns the uint64 value. Make sure that the Value's type is
// UINT64.
func (v *Value) AsUint64() uint64 {
return rawToUint64(v.numeric)
}
// AsFloat32 returns the float32 value. Make sure that the Value's
// type is FLOAT32.
func (v *Value) AsFloat32() float32 {
return rawToFloat32(v.numeric)
}
// AsFloat64 returns the float64 value. Make sure that the Value's
// type is FLOAT64.
func (v *Value) AsFloat64() float64 {
return rawToFloat64(v.numeric)
}
// AsString returns the string value. Make sure that the Value's type
// is STRING.
func (v *Value) AsString() string {
return v.stringly
}
type unknownValueType struct{}
// AsInterface returns Value's data as interface{}.
func (v *Value) AsInterface() interface{} {
switch v.Type() {
case BOOL:
return v.AsBool()
case INT32:
return v.AsInt32()
case INT64:
return v.AsInt64()
case UINT32:
return v.AsUint32()
case UINT64:
return v.AsUint64()
case FLOAT32:
return v.AsFloat32()
case FLOAT64:
return v.AsFloat64()
case STRING:
return v.stringly
}
return unknownValueType{}
}
// Emit returns a string representation of Value's data.
func (v *Value) Emit() string {
switch v.Type() {
case BOOL:
return strconv.FormatBool(v.AsBool())
case INT32:
return strconv.FormatInt(int64(v.AsInt32()), 10)
case INT64:
return strconv.FormatInt(v.AsInt64(), 10)
case UINT32:
return strconv.FormatUint(uint64(v.AsUint32()), 10)
case UINT64:
return strconv.FormatUint(v.AsUint64(), 10)
case FLOAT32:
return fmt.Sprint(v.AsFloat32())
case FLOAT64:
return fmt.Sprint(v.AsFloat64())
case STRING:
return v.stringly
default:
return "unknown"
}
}
// MarshalJSON returns the JSON encoding of the Value.
func (v *Value) MarshalJSON() ([]byte, error) {
var jsonVal struct {
Type string
Value interface{}
}
jsonVal.Type = v.Type().String()
jsonVal.Value = v.AsInterface()
return json.Marshal(jsonVal)
}