-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.go
81 lines (77 loc) · 1 KB
/
bool.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
package tox
import (
"strconv"
)
// ToBool converts any data type to a bool, if the conversion fails, it returns false.
func ToBool(v interface{}) bool {
switch v := v.(type) {
case bool:
return v
case float32:
if v != 0.0 {
return true
}
case float64:
if v != 0.0 {
return true
}
case int:
if v != 0 {
return true
}
case int8:
if v != 0 {
return true
}
case int16:
if v != 0 {
return true
}
case int32:
if v != 0 {
return true
}
case int64:
if v != 0 {
return true
}
case uint:
if v != 0 {
return true
}
case uint8:
if v != 0 {
return true
}
case uint16:
if v != 0 {
return true
}
case uint32:
if v != 0 {
return true
}
case uint64:
if v != 0 {
return true
}
case string:
i, _ := strconv.ParseBool(v)
return i
}
return false
}
func ToBoolPtr(v interface{}) *bool {
if v == nil {
return nil
}
ret := ToBool(v)
return &ret
}
func TriBool(b *bool) bool {
if b != nil {
return *b
} else {
return false
}
}