forked from abh/geodns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
typeutil.go
60 lines (55 loc) · 995 Bytes
/
typeutil.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
package typeutil
import (
"log"
"strconv"
)
func ToBool(v interface{}) (rv bool) {
switch v.(type) {
case bool:
rv = v.(bool)
case string:
str := v.(string)
switch str {
case "true":
rv = true
case "1":
rv = true
}
case float64:
if v.(float64) > 0 {
rv = true
}
default:
log.Println("Can't convert", v, "to bool")
panic("Can't convert value")
}
return rv
}
func ToString(v interface{}) (rv string) {
switch v.(type) {
case string:
rv = v.(string)
case float64:
rv = strconv.FormatFloat(v.(float64), 'f', -1, 64)
default:
log.Println("Can't convert", v, "to string")
panic("Can't convert value")
}
return rv
}
func ToInt(v interface{}) (rv int) {
switch v.(type) {
case string:
i, err := strconv.Atoi(v.(string))
if err != nil {
panic("Error converting weight to integer")
}
rv = i
case float64:
rv = int(v.(float64))
default:
log.Println("Can't convert", v, "to integer")
panic("Can't convert value")
}
return rv
}