-
Notifications
You must be signed in to change notification settings - Fork 1
/
bool.go
52 lines (46 loc) · 950 Bytes
/
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
package convert
import (
"fmt"
"reflect"
"strings"
)
func Bool(in any) bool {
if in == nil {
return false
}
switch in.(type) {
case bool:
return in.(bool)
case int8, int16, int32, int, int64, uint8, uint16, uint32, uint, uint64:
return fmt.Sprintf("%d", in) != "0"
case float32, float64:
return fmt.Sprintf("%.10f", in) != "0.0000000000"
case []byte:
switch strings.ToLower(string(in.([]byte))) {
case "", "false", "f", "0":
return false
default:
return true
}
case string:
switch strings.ToLower(fmt.Sprintf("%s", in)) {
case "", "false", "f", "0":
return false
default:
return true
}
}
items := reflect.ValueOf(in)
if items.Kind() == reflect.Slice || items.Kind() == reflect.Map {
return items.Len() > 0
}
switch strings.ToLower(fmt.Sprintf("%s", in)) {
case "", "false", "f", "0":
return false
default:
return true
}
}
func BoolErr(in any) (bool, error) {
return Bool(in), nil
}