/
cast.go
86 lines (80 loc) · 2.17 KB
/
cast.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
package datapoint
import (
"fmt"
)
// CastIntegerValue casts a signed integer to a datapoint Value
func CastIntegerValue(value interface{}) (metricValue Value, err error) {
switch val := value.(type) {
case int64:
metricValue = intWire(val)
case int32:
metricValue = intWire(int64(val))
case int16:
metricValue = intWire(int64(val))
case int8:
metricValue = intWire(int64(val))
case int:
metricValue = intWire(int64(val))
default:
err = fmt.Errorf("unknown metric value type %T", val)
}
return
}
// CastUIntegerValue casts an unsigned integer to a datapoint Value
func CastUIntegerValue(value interface{}) (metricValue Value, err error) {
switch val := value.(type) {
case uint64:
metricValue = intWire(int64(val))
case uint32:
metricValue = intWire(int64(val))
case uint16:
metricValue = intWire(int64(val))
case uint8:
metricValue = intWire(int64(val))
case uint:
metricValue = intWire(int64(val))
default:
err = fmt.Errorf("unknown metric value type %T", val)
}
return
}
// CastFloatValue casts a float to datapoint Value
func CastFloatValue(value interface{}) (metricValue Value, err error) {
switch val := value.(type) {
case float64:
metricValue = floatWire(val)
case float32:
metricValue = floatWire(float64(val))
default:
err = fmt.Errorf("unknown metric value type %T", val)
}
return
}
// CastMetricValue casts an interface to datapoint Value
func CastMetricValue(value interface{}) (metricValue Value, err error) {
switch val := value.(type) {
case int64, int32, int16, int8, int:
metricValue, err = CastIntegerValue(value)
case uint64, uint32, uint16, uint8, uint:
metricValue, err = CastUIntegerValue(value)
case float64, float32:
metricValue, err = CastFloatValue(value)
default:
err = fmt.Errorf("unknown metric value type %T", val)
}
return
}
// CastMetricValueWithBool casts an interface to datapoint Value and handles bool
func CastMetricValueWithBool(value interface{}) (metricValue Value, err error) {
switch v := value.(type) {
case bool:
if v {
metricValue, err = intWire(int64(1)), nil
} else {
metricValue, err = intWire(int64(0)), nil
}
default:
metricValue, err = CastMetricValue(value)
}
return
}