-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolean.go
90 lines (77 loc) · 1.58 KB
/
boolean.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
package data
import (
"fmt"
"strings"
)
type Boolean bool
const (
True Boolean = true
False Boolean = false
)
func MarshalBool(input interface{}) (bool, error) {
switch input.(type) {
case string:
if IsTrue(input.(string)) {
return true, nil
} else if IsFalse(input.(string)) {
return false, nil
}
case int:
if input.(int) == 1 {
return true, nil
} else if input.(int) == 0 {
return false, nil
}
case bool:
if input.(bool) {
return true, nil
} else if input.(bool) {
return false, nil
}
}
return false, fmt.Errorf("failed to marshal boolean value")
}
func IsTrue(value string) bool {
value = strings.ToLower(value)
for _, trueValue := range True.Strings() {
if trueValue == value {
return true
}
}
return false
}
func IsFalse(value string) bool {
value = strings.ToLower(value)
for _, falseValue := range False.Strings() {
if falseValue == value {
return true
}
}
return false
}
func IsBoolean(value string) bool { return IsFalse(value) || IsTrue(value) }
///////////////////////////////////////////////////////////////////////////////
func (self Boolean) Bool() bool { return bool(self) }
// TODO: Maybe in future give more options for string output, as in "1" "t"
// "yes"
func (self Boolean) String() string {
if self == True {
return "true"
} else {
return "false"
}
}
func (self Boolean) Int() int {
if self == True {
return 1
} else {
return 0
}
}
func (self Boolean) Strings() []string {
if self == True {
return []string{"true", "yes", "y", "t", "1"}
} else {
return []string{"false", "no", "n", "f", "0"}
}
}