/
bool.go
145 lines (132 loc) · 3.54 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package comm
import (
"fmt"
"reflect"
"strings"
)
// RequiredBoolP returns the bool value of the key in the map. If either parsing error or the key is not
// found, raise a panic.
func RequiredBoolP(hint string, key string, m map[string]any) bool {
r, err := RequiredBool(hint, key, m)
if err != nil {
panic(err)
}
return r
}
// RequiredBool returns the bool value of the key in the map. If either parsing error or the key is not
// found, an error is returned.
func RequiredBool(hint string, key string, m map[string]any) (bool, error) {
v, has := m[key]
if !has {
return false, fmt.Errorf("%s.%s is required", hint, key)
}
return Bool(hint+"."+key, v)
}
// OptionalBoolP returns the bool value of the key in the map. If parsing error occurred,
// raise a panic. If the key is not found, return the default value.
func OptionalBoolP(hint string, key string, m map[string]any, devault bool) (result bool, has bool) {
var err error
result, has, err = OptionalBool(hint, key, m, devault)
if err != nil {
panic(err)
}
return
}
// OptionalBool returns the bool value of the key in the map. If parsing error occrred,
// returns the error. If the key is not found, return the default value.
func OptionalBool(hint string, key string, m map[string]any, devault bool) (result bool, has bool, err error) {
var v any
v, has = m[key]
if !has {
result = devault
return
}
result, err = Bool(hint+"."+key, v)
return
}
// Cast the value to bool. If parsing error occurred, raise a panic.
func BoolP(hint string, v any) bool {
r, err := Bool(hint, v)
if err != nil {
panic(err)
}
return r
}
// Cast the value to bool. If parsing error occurred, returns the error.
func Bool(hint string, v any) (bool, error) {
r, ok := v.(bool)
if !ok {
if str, isStr := v.(string); isStr {
str = strings.ToLower(strings.TrimSpace(str))
if str == "true" {
return true, nil
}
if str == "false" {
return false, nil
}
}
return false, fmt.Errorf("%s must be a bool, but now it is a %v(%v)", hint, reflect.TypeOf(v), v)
}
return r, nil
}
func BoolArrayP(hint string, v any) []bool {
r, err := BoolArray(hint, v)
if err != nil {
panic(err)
}
return r
}
func BoolArray(hint string, v any) ([]bool, error) {
r, ok := v.([]bool)
if !ok {
if r1, ok1 := v.([]any); ok1 {
var err error
r = make([]bool, len(r1))
for i, v := range r1 {
if r[i], err = Bool(fmt.Sprintf("%s[%d]", hint, i), v); err != nil {
break
}
}
if err == nil {
return r, nil
}
} else if r0, err := Bool(hint, v); err == nil {
return []bool{r0}, nil
}
return nil, fmt.Errorf("%s must be a bool array, but now it is a %v(%v)", hint, reflect.TypeOf(v), v)
}
return r, nil
}
func BoolMapP(hint string, v any) map[string]bool {
r, err := BoolMap(hint, v)
if err != nil {
panic(err)
}
return r
}
func BoolMap(hint string, v any) (map[string]bool, error) {
r, ok := v.(map[string]bool)
if !ok {
if r1, ok1 := v.(map[string]any); ok1 {
var err error
r = map[string]bool{}
for k, v := range r1 {
if r[k], err = Bool(hint+"."+k, v); err != nil {
break
}
}
if err == nil {
return r, nil
}
} else if r0, ok0 := v.(string); ok0 {
if posOfColon := strings.Index(r0, ":"); posOfColon > 0 && posOfColon != len(r0)-1 {
if value, err := Bool(hint, strings.TrimSpace(r0[posOfColon+1:])); err == nil {
key := strings.TrimSpace(r0[:posOfColon])
return map[string]bool{key: value}, nil
}
}
}
return nil, fmt.Errorf("%s must be a bool map, but now it is a %v(%v)", hint, reflect.TypeOf(v), v)
}
return r, nil
}