-
Notifications
You must be signed in to change notification settings - Fork 4
/
boolean.go
69 lines (60 loc) · 1.62 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
package values
import (
"strings"
)
// Boolean represents a boolean with more features as the primitive type.
// @inline
type Boolean bool
func (instance Boolean) String() string {
s, err := instance.CheckedString()
if err != nil {
panic(err)
}
return s
}
// CheckedString is like String but also returns an optional error if there are any
// validation errors.
func (instance Boolean) CheckedString() (string, error) {
if instance == Boolean(true) {
return "true", nil
}
return "false", nil
}
// Set sets the given string to current object from a string.
func (instance *Boolean) Set(value string) error {
switch strings.ToLower(value) {
case "1":
fallthrough
case "on":
fallthrough
case "yes":
fallthrough
case "y":
fallthrough
case "true":
return instance.SetFromBool(true)
}
return instance.SetFromBool(false)
}
// SetFromBool sets the given boolean value to the current object.
func (instance *Boolean) SetFromBool(value bool) error {
*instance = Boolean(value)
return nil
}
// MarshalYAML is used until yaml marshalling. Do not call this method directly.
func (instance Boolean) MarshalYAML() (interface{}, error) {
return bool(instance), nil
}
// UnmarshalYAML is used until yaml unmarshalling. Do not call this method directly.
func (instance *Boolean) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value bool
if err := unmarshal(&value); err != nil {
return err
}
return instance.SetFromBool(value)
}
// Validate validates actions on this object and returns an error object if there are any.
func (instance Boolean) Validate() error {
_, err := instance.CheckedString()
return err
}