-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunpack.go
254 lines (245 loc) · 6.92 KB
/
unpack.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package reflectutils
import (
"encoding"
"encoding/json"
"flag"
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
)
var (
textUnmarshallerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
flagValueType = reflect.TypeOf((*flag.Value)(nil)).Elem()
)
type stringSetterOpts struct {
split string
sliceAppend bool
forceJSON bool
}
type StringSetterArg func(*stringSetterOpts)
// WithSplitOn specifies how to split strings into slices
// or arrays. An empty string indicates that input strings
// should not be split. That's different than the behavior
// of strings.Split(). If unspecified, strings will be split
// on comma (,).
func WithSplitOn(s string) StringSetterArg {
return func(o *stringSetterOpts) {
o.split = s
}
}
// Controls the behavior for setting existing existing slices.
// If this is true (the default) then additional setting to a
// slice appends to the existing slice. If false, slices are
// replaced.
func SliceAppend(b bool) StringSetterArg {
return func(o *stringSetterOpts) {
o.sliceAppend = b
}
}
// ForceJSON controls if types will be decoded with JSON
// unmarshal. This overrides normal decoding patterns. The default
// is false.
func ForceJSON(b bool) StringSetterArg {
return func(o *stringSetterOpts) {
o.forceJSON = b
}
}
// MakeStringSetter handles setting a reflect.Value from a string.
// Based on type, it returns a function to do the work. It is assumed that the
// reflect.Type matches the reflect.Value. If not, panic is likely.
//
// For arrays and slices, strings are split on comma to create the values for the
// elements.
//
// Any type that matches a type registered with RegisterStringSetter will be
// unpacked with the corresponding function. A string setter is pre-registered
// for time.Duration
// Anything that implements encoding.TextUnmarshaler will be unpacked that way.
// Anything that implements flag.Value will be unpacked that way.
//
// Maps, structs, channels, interfaces, channels, and funcs are not supported unless
// they happen to implent encoding.TextUnmarshaler.
func MakeStringSetter(t reflect.Type, optArgs ...StringSetterArg) (func(target reflect.Value, value string) error, error) {
opts := stringSetterOpts{
split: ",",
sliceAppend: true,
}
for _, f := range optArgs {
f(&opts)
}
if opts.forceJSON {
return func(target reflect.Value, value string) error {
p := reflect.New(t.Elem())
target.Set(p)
err := json.Unmarshal([]byte(value), target.Interface())
if err != nil {
return errors.WithStack(err)
}
return nil
}, nil
}
if setter, ok := settersByType[t]; ok {
return func(target reflect.Value, value string) error {
out := setter.Call([]reflect.Value{reflect.ValueOf(value)})
if !out[1].IsNil() {
return out[1].Interface().(error)
}
target.Set(out[0])
return nil
}, nil
}
if t.AssignableTo(textUnmarshallerType) {
return func(target reflect.Value, value string) error {
p := reflect.New(t.Elem())
target.Set(p)
err := target.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(value))
if err != nil {
return errors.WithStack(err)
}
return nil
}, nil
}
if reflect.PtrTo(t).AssignableTo(textUnmarshallerType) {
return func(target reflect.Value, value string) error {
err := target.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(value))
return errors.WithStack(err)
}, nil
}
if t.AssignableTo(flagValueType) {
return func(target reflect.Value, value string) error {
p := reflect.New(t.Elem())
target.Set(p)
err := target.Interface().(flag.Value).Set(value)
if err != nil {
return errors.WithStack(err)
}
return nil
}, nil
}
if reflect.PtrTo(t).AssignableTo(flagValueType) {
return func(target reflect.Value, value string) error {
err := target.Addr().Interface().(flag.Value).Set(value)
return errors.WithStack(err)
}, nil
}
switch t.Kind() {
case reflect.Ptr:
setElem, err := MakeStringSetter(t.Elem(), optArgs...)
if err != nil {
return nil, err
}
return func(target reflect.Value, value string) error {
p := reflect.New(t.Elem())
target.Set(p)
err := setElem(target.Elem(), value)
if err != nil {
return err
}
return nil
}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return func(target reflect.Value, value string) error {
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return errors.WithStack(err)
}
target.SetInt(i)
return nil
}, nil
case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return func(target reflect.Value, value string) error {
i, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return errors.WithStack(err)
}
target.SetUint(i)
return nil
}, nil
case reflect.Float32, reflect.Float64:
return func(target reflect.Value, value string) error {
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return errors.WithStack(err)
}
target.SetFloat(f)
return nil
}, nil
case reflect.String:
return func(target reflect.Value, value string) error {
target.SetString(value)
return nil
}, nil
case reflect.Complex64, reflect.Complex128:
return func(target reflect.Value, value string) error {
c, err := strconv.ParseComplex(value, 128)
if err != nil {
return errors.WithStack(err)
}
target.SetComplex(c)
return nil
}, nil
case reflect.Bool:
return func(target reflect.Value, value string) error {
b, err := strconv.ParseBool(value)
if err != nil {
return errors.WithStack(err)
}
target.SetBool(b)
return nil
}, nil
case reflect.Array:
setElem, err := MakeStringSetter(t.Elem())
if err != nil {
return nil, err
}
if opts.split == "" {
return func(target reflect.Value, value string) error {
return setElem(target.Index(0), value)
}, nil
}
return func(target reflect.Value, value string) error {
for i, v := range strings.SplitN(value, opts.split, target.Cap()) {
err := setElem(target.Index(i), v)
if err != nil {
return err
}
}
return nil
}, nil
case reflect.Slice:
setElem, err := MakeStringSetter(t.Elem())
if err != nil {
return nil, err
}
return func(target reflect.Value, value string) error {
var values []string
if opts.split != "" {
values = strings.Split(value, opts.split)
} else {
values = []string{value}
}
a := reflect.MakeSlice(target.Type(), len(values), len(values))
for i, v := range values {
err := setElem(a.Index(i), v)
if err != nil {
return err
}
}
if target.IsNil() || !opts.sliceAppend {
target.Set(a)
} else {
target.Set(reflect.AppendSlice(target, a))
}
return nil
}, nil
case reflect.Map:
fallthrough
case reflect.Struct:
fallthrough
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Invalid, reflect.UnsafePointer:
fallthrough
default:
return nil, errors.Errorf("type %s not supported", t)
}
}