/
unpack.go
181 lines (174 loc) · 4.86 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
package reflectutils
import (
"encoding"
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
)
var textUnmarshallerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
type stringSetterOpts struct {
split string
}
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
}
}
// 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.
//
// Anything that implements encoding.TextUnmarshaler 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: ",",
}
for _, f := range optArgs {
f(&opts)
}
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
}
switch t.Kind() {
case reflect.Ptr:
setElem, err := MakeStringSetter(t.Elem())
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
}
}
target.Set(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)
}
}