forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast.go
88 lines (81 loc) · 1.56 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
87
88
package query
import "github.com/influxdata/influxql"
// castToType will coerce the underlying interface type to another
// interface depending on the type.
func castToType(v interface{}, typ influxql.DataType) interface{} {
switch typ {
case influxql.Float:
if val, ok := castToFloat(v); ok {
v = val
}
case influxql.Integer:
if val, ok := castToInteger(v); ok {
v = val
}
case influxql.Unsigned:
if val, ok := castToUnsigned(v); ok {
v = val
}
case influxql.String:
if val, ok := castToString(v); ok {
v = val
}
case influxql.Boolean:
if val, ok := castToBoolean(v); ok {
v = val
}
}
return v
}
func castToFloat(v interface{}) (float64, bool) {
switch v := v.(type) {
case float64:
return v, true
case int64:
return float64(v), true
case uint64:
return float64(v), true
default:
return float64(0), false
}
}
func castToInteger(v interface{}) (int64, bool) {
switch v := v.(type) {
case float64:
return int64(v), true
case int64:
return v, true
case uint64:
return int64(v), true
default:
return int64(0), false
}
}
func castToUnsigned(v interface{}) (uint64, bool) {
switch v := v.(type) {
case float64:
return uint64(v), true
case uint64:
return v, true
case int64:
return uint64(v), true
default:
return uint64(0), false
}
}
func castToString(v interface{}) (string, bool) {
switch v := v.(type) {
case string:
return v, true
default:
return "", false
}
}
func castToBoolean(v interface{}) (bool, bool) {
switch v := v.(type) {
case bool:
return v, true
default:
return false, false
}
}