-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
119 lines (115 loc) · 2.64 KB
/
helpers.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
package gmap
import (
"strconv"
)
// Helper function to convert an interface{} to string
func interfaceToString(v interface{}, def string) (string, error) {
switch v.(type) {
case string:
return v.(string), nil
case bool:
return strconv.FormatBool(v.(bool)), nil
case float64:
return strconv.FormatFloat(v.(float64), 'f', -1, 64), nil
case int:
return strconv.Itoa(v.(int)), nil
case int8:
return strconv.FormatInt(int64(v.(int8)), 10), nil
case int16:
return strconv.FormatInt(int64(v.(int16)), 10), nil
case int32:
return strconv.FormatInt(int64(v.(int32)), 10), nil
case int64:
return strconv.FormatInt(v.(int64), 10), nil
case uint:
return strconv.FormatUint(uint64(v.(uint)), 10), nil
case uint8:
return strconv.FormatUint(uint64(v.(uint8)), 10), nil
case uint16:
return strconv.FormatUint(uint64(v.(uint16)), 10), nil
case uint32:
return strconv.FormatUint(uint64(v.(uint32)), 10), nil
case uint64:
return strconv.FormatUint(v.(uint64), 10), nil
default:
return def, ErrTypeMismatch
}
}
// Helper function to convert an interface{} to int
func interfaceToInt(v interface{}, def int) (int, error) {
switch v.(type) {
case int:
return v.(int), nil
case int8:
return int(v.(int8)), nil
case int16:
return int(v.(int16)), nil
case int32:
return int(v.(int32)), nil
case int64:
return int(v.(int64)), nil
case uint:
return int(v.(uint)), nil
case uint8:
return int(v.(uint8)), nil
case uint16:
return int(v.(uint16)), nil
case uint32:
return int(v.(uint32)), nil
case uint64:
return int(v.(uint64)), nil
case float32:
return int(v.(float32)), nil
case float64:
return int(v.(float64)), nil
case string:
return strconv.Atoi(v.(string))
case bool:
i := 0
if v.(bool) {
i = 1
}
return i, nil
default:
return def, ErrTypeMismatch
}
}
// Helper function to convert an interface{} to float64
func interfaceToFloat64(v interface{}, def float64) (float64, error) {
switch v.(type) {
case uint:
return float64(v.(uint)), nil
case uint8:
return float64(v.(uint8)), nil
case uint16:
return float64(v.(uint16)), nil
case uint32:
return float64(v.(uint32)), nil
case uint64:
return float64(v.(uint64)), nil
case int:
return float64(v.(int)), nil
case int8:
return float64(v.(int8)), nil
case int16:
return float64(v.(int16)), nil
case int32:
return float64(v.(int32)), nil
case int64:
return float64(v.(int64)), nil
case float32:
return float64(v.(float32)), nil
case float64:
return v.(float64), nil
case bool:
f := 0.0
if v.(bool) {
f = 1.0
}
return f, nil
case string:
return strconv.ParseFloat(v.(string), 64)
default:
return def, ErrTypeMismatch
}
}