forked from Symantec/tricorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
338 lines (320 loc) · 7.26 KB
/
api.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
// Package types contains the various types for metric values.
package types
import (
"fmt"
"github.com/Symantec/tricorder/go/tricorder/duration"
"time"
)
// Type represents the type of a metric value
type Type string
const (
Unknown Type = ""
Bool Type = "bool"
Int8 Type = "int8"
Int16 Type = "int16"
Int32 Type = "int32"
Int64 Type = "int64"
Uint8 Type = "uint8"
Uint16 Type = "uint16"
Uint32 Type = "uint32"
Uint64 Type = "uint64"
Float32 Type = "float32"
Float64 Type = "float64"
String Type = "string"
GoTime Type = "goTime"
GoDuration Type = "goDuration"
Dist Type = "distribution"
List Type = "list"
// for JSON RPC only
Time Type = "time"
// for JSON RPC only
Duration Type = "duration"
)
// FromGoValue returns the type of a value found in the GoRPC API.
// FromGoValue returns Unknown if it cannot determine the type.
// In case value is a slice of unknown type, FromGoValue returns Unknown
// rather than List.
func FromGoValue(value interface{}) Type {
kind, _ := FromGoValueWithSubType(value)
return kind
}
// FromGoValueWithSubType returns both the type and sub-type of the value
// found in the GoRPC API.
// FromGoValueWithSubType returns Unknown, Unknown if it cannot determine the
// type.
// In case value is a slice of an unknown type, FromGoValueWithSubType returns
// Unknown, Unknown rather than List, Unknown.
func FromGoValueWithSubType(value interface{}) (kind, subType Type) {
switch i := value.(type) {
case bool:
kind = Bool
case int8:
kind = Int8
case int16:
kind = Int16
case int32:
kind = Int32
case int64:
kind = Int64
case uint8:
kind = Uint8
case uint16:
kind = Uint16
case uint32:
kind = Uint32
case uint64:
kind = Uint64
case float32:
kind = Float32
case float64:
kind = Float64
case string:
kind = String
case time.Time:
kind = GoTime
case time.Duration:
kind = GoDuration
case goValue:
kind = i.Type()
case []bool:
kind = List
subType = Bool
case []int8:
kind = List
subType = Int8
case []int16:
kind = List
subType = Int16
case []int32:
kind = List
subType = Int32
case []int64:
kind = List
subType = Int64
case []uint8:
kind = List
subType = Uint8
case []uint16:
kind = List
subType = Uint16
case []uint32:
kind = List
subType = Uint32
case []uint64:
kind = List
subType = Uint64
case []float32:
kind = List
subType = Float32
case []float64:
kind = List
subType = Float64
case []string:
kind = List
subType = String
case []time.Time:
kind = List
subType = GoTime
case []time.Duration:
kind = List
subType = GoDuration
default:
}
return
}
// SafeZeroValue is like ZeroValue except it returns an error instead of
// panicing
func (t Type) SafeZeroValue() (interface{}, error) {
switch t {
case Bool:
return false, nil
case Int8:
return int8(0), nil
case Int16:
return int16(0), nil
case Int32:
return int32(0), nil
case Int64:
return int64(0), nil
case Uint8:
return uint8(0), nil
case Uint16:
return uint16(0), nil
case Uint32:
return uint32(0), nil
case Uint64:
return uint64(0), nil
case Float32:
return float32(0), nil
case Float64:
return float64(0), nil
case String:
return "", nil
case Time, Duration:
return "0.000000000", nil
case GoTime:
return time.Time{}, nil
case GoDuration:
return time.Duration(0), nil
default:
return nil, fmt.Errorf("Cannot create zero value for type '%s'", t)
}
}
// ZeroValue returns the zero value for this type.
// ZeroValue panics if this type is Dist, List, or Unknown.
func (t Type) ZeroValue() interface{} {
result, err := t.SafeZeroValue()
if err != nil {
panic(err)
}
return result
}
// SafeNilSlice is like NilSlice except it returns an error instead of
// panicing
func (t Type) SafeNilSlice() (interface{}, error) {
switch t {
case Bool:
return ([]bool)(nil), nil
case Int8:
return ([]int8)(nil), nil
case Int16:
return ([]int16)(nil), nil
case Int32:
return ([]int32)(nil), nil
case Int64:
return ([]int64)(nil), nil
case Uint8:
return ([]uint8)(nil), nil
case Uint16:
return ([]uint16)(nil), nil
case Uint32:
return ([]uint32)(nil), nil
case Uint64:
return ([]uint64)(nil), nil
case Float32:
return ([]float32)(nil), nil
case Float64:
return ([]float64)(nil), nil
case String, Time, Duration:
return ([]string)(nil), nil
case GoTime:
return ([]time.Time)(nil), nil
case GoDuration:
return ([]time.Duration)(nil), nil
default:
return nil, fmt.Errorf("Cannot create nil slice of type '%s'", t)
}
}
// NilSlice returns the nil slice of this type.
// NilSlice panics if this type is Dist, List or Unknown.
func (t Type) NilSlice() interface{} {
result, err := t.SafeNilSlice()
if err != nil {
panic(err)
}
return result
}
// CanToFromFloat returns true if this type supports conversion to/from float64
func (t Type) CanToFromFloat() bool {
switch t {
case Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64, Float32, Float64, GoTime, GoDuration:
return true
default:
return false
}
}
// FromFloat converts a float64 to a value according to this type
// FromFloat panics if this type doesn't support conversion from float64
func (t Type) FromFloat(value float64) interface{} {
switch t {
case Int8:
return int8(round(value))
case Int16:
return int16(round(value))
case Int32:
return int32(round(value))
case Int64:
return int64(round(value))
case Uint8:
return uint8(round(value))
case Uint16:
return uint16(round(value))
case Uint32:
return uint32(round(value))
case Uint64:
return uint64(round(value))
case Float32:
return float32(value)
case Float64:
return value
case GoTime:
return duration.FloatToTime(value)
case GoDuration:
return duration.FromFloat(value)
default:
panic("Type doesn't support converstion from a float")
}
}
// ToFloat converts a value of this type to a float64
// ToFloat panics if this type doesn't support conversion to float64
func (t Type) ToFloat(x interface{}) float64 {
switch t {
case Int8:
return float64(x.(int8))
case Int16:
return float64(x.(int16))
case Int32:
return float64(x.(int32))
case Int64:
return float64(x.(int64))
case Uint8:
return float64(x.(uint8))
case Uint16:
return float64(x.(uint16))
case Uint32:
return float64(x.(uint32))
case Uint64:
return float64(x.(uint64))
case Float32:
return float64(x.(float32))
case Float64:
return x.(float64)
case GoTime:
return duration.TimeToFloat(x.(time.Time))
case GoDuration:
return duration.ToFloat(x.(time.Duration))
default:
panic("Type doesn't support conversion to float")
}
}
func (t Type) IsInt() bool {
return t == Int8 || t == Int16 || t == Int32 || t == Int64
}
func (t Type) IsUint() bool {
return t == Uint8 || t == Uint16 || t == Uint32 || t == Uint64
}
func (t Type) IsFloat() bool {
return t == Float32 || t == Float64
}
func (t Type) Bits() int {
switch t {
case Int8, Uint8:
return 8
case Int16, Uint16:
return 16
case Int32, Uint32, Float32:
return 32
case Int64, Uint64, Float64:
return 64
default:
return 0
}
}
// UsesSubType returns true if this type uses a sub-type.
func (t Type) UsesSubType() bool {
return t == List
}
// SupportsEquality returns true if this type supports equality.
func (t Type) SupportsEquality() bool {
return t != List && t != Dist && t != Unknown
}