-
Notifications
You must be signed in to change notification settings - Fork 14
/
set.go
181 lines (155 loc) · 4.64 KB
/
set.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 env
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
var (
binaryType = reflect.TypeOf([]uint8{})
)
// setField determines a field's type and parses the given value
// accordingly. An error will be returned if the field is unexported.
func setBuiltInField(fieldValue reflect.Value, value string) (err error) {
switch fieldValue.Kind() {
case reflect.Bool:
return setBool(fieldValue, value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return setInt(fieldValue, value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return setUint(fieldValue, value)
case reflect.Float32, reflect.Float64:
return setFloat(fieldValue, value)
case reflect.String:
return setString(fieldValue, value)
default:
return fmt.Errorf("%s is not supported", fieldValue.Kind())
}
}
func setBool(fieldValue reflect.Value, value string) (err error) {
var b bool
if b, err = strconv.ParseBool(value); err != nil {
return err
}
fieldValue.SetBool(b)
return
}
func setInt(fieldValue reflect.Value, value string) (err error) {
if fieldValue.Type() == reflect.TypeOf((*time.Duration)(nil)).Elem() {
return setDuration(fieldValue, value)
}
var i int64
if i, err = strconv.ParseInt(value, 0, 64); err != nil {
return err
}
fieldValue.SetInt(int64(i))
return
}
func setUint(fieldValue reflect.Value, value string) (err error) {
var i uint64
if i, err = strconv.ParseUint(value, 0, 64); err != nil {
return err
}
fieldValue.SetUint(uint64(i))
return
}
func setFloat(fieldValue reflect.Value, value string) (err error) {
var f float64
if f, err = strconv.ParseFloat(value, 64); err != nil {
return err
}
fieldValue.SetFloat(f)
return
}
func setDuration(fieldValue reflect.Value, value string) (err error) {
var d time.Duration
if d, err = time.ParseDuration(value); err != nil {
return err
}
fieldValue.SetInt(d.Nanoseconds())
return
}
func setString(fieldValue reflect.Value, value string) (err error) {
fieldValue.SetString(value)
return
}
func setSlice(t reflect.StructField, v reflect.Value, value string) (err error) {
// []uint8 and []byte are special cases, as they can be used to store
// binary data, which we'll favour over storing comma-separated uint8s.
if t.Type == binaryType {
v.SetBytes([]byte(value))
return
}
// Allow the user to provide their own delimiter, falling back to a
// comma if one isn't provided.
delimiter := getDelimiter(t)
rawValues := split(value, delimiter)
if len(rawValues) == 0 {
return
}
sliceValue, err := makeSlice(v, len(rawValues))
if err != nil {
return
}
populateSlice(sliceValue, rawValues)
v.Set(sliceValue)
return
}
func makeSlice(v reflect.Value, n int) (slice reflect.Value, err error) {
switch v.Type() {
case reflect.TypeOf([]string{}):
slice = reflect.MakeSlice(reflect.TypeOf([]string{}), n, n)
case reflect.TypeOf([]bool{}):
slice = reflect.MakeSlice(reflect.TypeOf([]bool{}), n, n)
case reflect.TypeOf([]int{}):
slice = reflect.MakeSlice(reflect.TypeOf([]int{}), n, n)
case reflect.TypeOf([]int8{}):
slice = reflect.MakeSlice(reflect.TypeOf([]int8{}), n, n)
case reflect.TypeOf([]int16{}):
slice = reflect.MakeSlice(reflect.TypeOf([]int16{}), n, n)
case reflect.TypeOf([]int32{}):
slice = reflect.MakeSlice(reflect.TypeOf([]int32{}), n, n)
case reflect.TypeOf([]int64{}):
slice = reflect.MakeSlice(reflect.TypeOf([]int64{}), n, n)
case reflect.TypeOf([]uint{}):
slice = reflect.MakeSlice(reflect.TypeOf([]uint{}), n, n)
case reflect.TypeOf([]uint16{}):
slice = reflect.MakeSlice(reflect.TypeOf([]uint16{}), n, n)
case reflect.TypeOf([]uint32{}):
slice = reflect.MakeSlice(reflect.TypeOf([]uint32{}), n, n)
case reflect.TypeOf([]uint64{}):
slice = reflect.MakeSlice(reflect.TypeOf([]uint64{}), n, n)
case reflect.TypeOf([]float32{}):
slice = reflect.MakeSlice(reflect.TypeOf([]float32{}), n, n)
case reflect.TypeOf([]float64{}):
slice = reflect.MakeSlice(reflect.TypeOf([]float64{}), n, n)
case reflect.TypeOf([]time.Duration{}):
slice = reflect.MakeSlice(reflect.TypeOf([]time.Duration{}), n, n)
default:
err = fmt.Errorf("%v is not supported", v.Type())
}
return
}
func populateSlice(sliceValue reflect.Value, rawItems []string) {
for i, item := range rawItems {
setBuiltInField(sliceValue.Index(i), item)
}
}
func split(value string, delimeter string) []string {
var out []string
raw := strings.Split(value, delimeter)
for _, r := range raw {
sanitised := strings.Trim(r, " ")
if len(sanitised) > 0 {
out = append(out, sanitised)
}
}
return out
}
func getDelimiter(t reflect.StructField) string {
if d, ok := t.Tag.Lookup("delimiter"); ok {
return d
}
return ","
}