-
Notifications
You must be signed in to change notification settings - Fork 7
/
bool.go
63 lines (56 loc) · 1.61 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
package refconv
import (
"fmt"
"reflect"
"time"
"github.com/cstockton/go-conv/internal/refutil"
)
type boolConverter interface {
Bool() (bool, error)
}
// Bool attempts to convert the given value to bool, returns the zero value
// and an error on failure.
func (c Conv) Bool(from interface{}) (bool, error) {
if T, ok := from.(string); ok {
return c.convStrToBool(T)
} else if T, ok := from.(bool); ok {
return T, nil
} else if c, ok := from.(boolConverter); ok {
return c.Bool()
}
value := refutil.IndirectVal(reflect.ValueOf(from))
kind := value.Kind()
switch {
case reflect.String == kind:
return c.convStrToBool(value.String())
case refutil.IsKindNumeric(kind):
if parsed, ok := c.convNumToBool(kind, value); ok {
return parsed, nil
}
case reflect.Bool == kind:
return value.Bool(), nil
case refutil.IsKindLength(kind):
return value.Len() > 0, nil
case reflect.Struct == kind && value.CanInterface():
v := value.Interface()
if t, ok := v.(time.Time); ok {
return emptyTime != t, nil
}
}
return false, newConvErr(from, "bool")
}
func (c Conv) convStrToBool(v string) (bool, error) {
// @TODO Need to find a clean way to expose the truth list to be modified by
// API to allow INTL.
if 1 > len(v) || len(v) > 5 {
return false, fmt.Errorf("cannot parse string with len %d as bool", len(v))
}
// @TODO lut
switch v {
case "1", "t", "T", "true", "True", "TRUE", "y", "Y", "yes", "Yes", "YES":
return true, nil
case "0", "f", "F", "false", "False", "FALSE", "n", "N", "no", "No", "NO":
return false, nil
}
return false, fmt.Errorf("cannot parse %#v (type string) as bool", v)
}