-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.go
177 lines (140 loc) · 4.51 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// This is copied from Golang's flag library, because it is private :(
package flagstruct
import (
"fmt"
"reflect"
"strconv"
"time"
)
// boolValue represents a boolean value.
type boolValue bool
// Set implements the Value interface.
func (b *boolValue) Set(s string) error {
v, err := strconv.ParseBool(s)
*b = boolValue(v)
return err
}
// Get implements the Value interface.
func (b *boolValue) Get() interface{} { return bool(*b) }
// String implements the Value interface.
func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
// IsBoolFlag signals boolean flag behavior to Go's flag library.
func (b *boolValue) IsBoolFlag() bool { return true }
// intValue represents an integer value.
type intValue int
// Set implements the Value interface.
func (i *intValue) Set(s string) error {
v, err := strconv.ParseInt(s, 0, 64)
*i = intValue(v)
return err
}
// Get implements the Value interface.
func (i *intValue) Get() interface{} { return int(*i) }
// String implements the Value interface.
func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
// int64Value represents a 64-bit integer value.
type int64Value int64
// Set implements the Value interface.
func (i *int64Value) Set(s string) error {
v, err := strconv.ParseInt(s, 0, 64)
*i = int64Value(v)
return err
}
// Get implements the Value interface.
func (i *int64Value) Get() interface{} { return int64(*i) }
// String implements the Value interface.
func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
// uintValue represents an unsigned integer value.
type uintValue uint
// Set implements the Value interface.
func (i *uintValue) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 64)
*i = uintValue(v)
return err
}
// Get implements the Value interface.
func (i *uintValue) Get() interface{} { return uint(*i) }
// String implements the Value interface.
func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
// uint64Value represents an unsigned 64-bit integer value.
type uint64Value uint64
// Set implements the Value interface.
func (i *uint64Value) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 64)
*i = uint64Value(v)
return err
}
// Get implements the Value interface.
func (i *uint64Value) Get() interface{} { return uint64(*i) }
// String implements the Value interface.
func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
// stringValue represents a string value.
type stringValue string
// Set implements the Value interface.
func (s *stringValue) Set(val string) error {
*s = stringValue(val)
return nil
}
// Get implements the Value interface.
func (s *stringValue) Get() interface{} { return string(*s) }
// String implements the Value interface.
func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
// float64Value represents a 64-bit floating point value.
type float64Value float64
// Set implements the Value interface.
func (f *float64Value) Set(s string) error {
v, err := strconv.ParseFloat(s, 64)
*f = float64Value(v)
return err
}
// Get implements the Value interface.
func (f *float64Value) Get() interface{} { return float64(*f) }
// String implements the Value interface.
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
// durationValue represents a duration of time.
type durationValue time.Duration
// Set implements the Value interface.
func (d *durationValue) Set(s string) error {
v, err := time.ParseDuration(s)
*d = durationValue(v)
return err
}
// Get implements the Value interface.
func (d *durationValue) Get() interface{} { return time.Duration(*d) }
// String implements the Value interface.
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
// Value is an interface used for flag values.
type Value interface {
String() string
Set(string) error
Get() interface{}
}
// valueFromPointer uses reflection to determine what value type to use.
func valueFromPointer(ptr interface{}) (Value, error) {
switch f := ptr.(type) {
case *bool:
return (*boolValue)(f), nil
case *float64:
return (*float64Value)(f), nil
case *int:
return (*intValue)(f), nil
case *int64:
return (*int64Value)(f), nil
case *string:
return (*stringValue)(f), nil
case *uint:
return (*uintValue)(f), nil
case *uint64:
return (*uint64Value)(f), nil
case *time.Duration:
return (*durationValue)(f), nil
case Value:
return f, nil
default:
if ptr == nil {
return nil, unhandledTypeError{nil}
}
t := reflect.ValueOf(ptr).Elem().Interface()
return nil, unhandledTypeError{t}
}
}