-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalue.go
126 lines (114 loc) · 2.01 KB
/
value.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
package flagutil
import (
"flag"
"reflect"
)
// OverrideSet returns a wrapper around v which Set() method is replaced by f.
func OverrideSet(v flag.Value, f func(string) error) flag.Value {
return value{
value: v,
doSet: f,
}
}
type flagSetPair [2]*flag.FlagSet
func (p flagSetPair) Set(name, s string) error {
for i := 0; i < len(p); i++ {
if p[i] == nil {
continue
}
if err := p[i].Set(name, s); err != nil {
return err
}
}
return nil
}
type value struct {
value flag.Value
doSet func(string) error
doGet func() interface{}
doString func() string
doIsBoolFlag func() bool
}
func (v value) Set(s string) error {
if fn := v.doSet; fn != nil {
return fn(s)
}
if v := v.value; v != nil {
return v.Set(s)
}
return nil
}
func (v value) Get() interface{} {
if fn := v.doGet; fn != nil {
return fn()
}
if g, ok := v.value.(flag.Getter); ok {
return g.Get()
}
return nil
}
func (v value) String() string {
if fn := v.doString; fn != nil {
return fn()
}
if v := v.value; v != nil {
return v.String()
}
return ""
}
func (v value) IsBoolFlag() bool {
if fn := v.doIsBoolFlag; fn != nil {
return fn()
}
if b, ok := v.value.(interface {
IsBoolFlag() bool
}); ok {
return b.IsBoolFlag()
}
return false
}
type valuePair [2]flag.Value
func (p valuePair) Set(val string) error {
for _, v := range p {
if err := v.Set(val); err != nil {
return err
}
}
return nil
}
func (p valuePair) Get() interface{} {
var (
v0 interface{}
v1 interface{}
)
if g0, ok := p[0].(flag.Getter); ok {
v0 = g0.Get()
}
if g1, ok := p[1].(flag.Getter); ok {
v1 = g1.Get()
}
if !reflect.DeepEqual(v0, v1) {
return nil
}
return v0
}
func (p valuePair) String() string {
if p.isZero() {
return ""
}
s0 := p[0].String()
s1 := p[1].String()
if s0 != s1 {
return ""
}
return s0
}
func (p valuePair) IsBoolFlag() bool {
if isBoolValue(p[0]) && isBoolValue(p[1]) {
return true
}
return false
}
func (p valuePair) isZero() bool {
return p == valuePair{}
}