-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbool.go
More file actions
73 lines (58 loc) · 1.23 KB
/
Copy pathbool.go
File metadata and controls
73 lines (58 loc) · 1.23 KB
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
package config
import (
"reflect"
"strconv"
)
var boolType = reflect.TypeOf(false)
type boolSetter struct {
val *bool
}
func (bs *boolSetter) IsBoolFlag() bool {
return true
}
func (bs *boolSetter) String() string {
if bs.val == nil {
return strconv.FormatBool(false)
}
return strconv.FormatBool(*bs.val)
}
func (bs *boolSetter) Set(val string) error {
bval, err := strconv.ParseBool(val)
if err != nil {
return &ConversionError{Value: val, ToType: boolType}
}
*bs.val = bval
return nil
}
func (bs *boolSetter) SetInt(val int64) error {
*bs.val = val != 0
return nil
}
func (bs *boolSetter) SetUint(val uint64) error {
*bs.val = val != 0
return nil
}
func (bs *boolSetter) SetFloat(val float64) error {
*bs.val = val != 0
return nil
}
func (bs *boolSetter) SetBool(val bool) error {
*bs.val = val
return nil
}
func (bs *boolSetter) Get() interface{} {
if bs.val == nil {
return false
}
return *bs.val
}
type boolSetterCreator struct{}
func (bsc boolSetterCreator) Type() reflect.Type {
return boolType
}
func (bsc boolSetterCreator) Setter(val reflect.Value, tag reflect.StructTag) Setter {
return &boolSetter{val: val.Addr().Interface().(*bool)}
}
func init() {
DefaultSetterRegistry.Add(boolSetterCreator{})
}