-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.go
383 lines (316 loc) · 9.25 KB
/
fields.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package skyconf
import (
"encoding"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
// Setter is implemented by types can self-deserialize values.
type Setter interface {
Set(value string) error
}
type fieldInfo struct {
nameParts []string
structField reflect.Value
options fieldOptions
}
type fieldOptions struct {
defaultValue string
optional bool
flatten bool
source string
refresh time.Duration
id string
}
func (o *fieldOptions) String() string {
return fmt.Sprintf(`{defaultValue:%s optional:%t flatten:%t source:%s refresh:%s id:%s}`,
o.defaultValue, o.optional, o.flatten, o.source, o.refresh, o.id)
}
// inherit copies the options from the parent.
func (o *fieldOptions) inherit(parent fieldOptions) {
o.source = parent.source
}
var ErrInvalidStruct = errors.New("config must be a pointer to a struct")
var ErrBadTags = errors.New("error parsing tags for field")
// extractFields uses reflection to examine the struct and extract the fields.
func extractFields(withUntagged bool, prefix []string, target interface{}, parentOptions fieldOptions) (fields []fieldInfo, err error) {
if prefix == nil {
prefix = []string{}
}
s := reflect.ValueOf(target)
// Make sure the config is a pointer to struct
if s.Kind() != reflect.Ptr {
return nil, ErrInvalidStruct
}
if s = s.Elem(); s.Kind() != reflect.Struct {
return nil, ErrInvalidStruct
}
targetType := s.Type()
// Iterate over the fields of the struct.
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
// Skip unexported fields.
if !f.CanSet() {
continue
}
structField := targetType.Field(i)
// Get the 'sky' tag.
tags, tagged := structField.Tag.Lookup("sky")
// If there is no tag (not even an empty tag), ignore the field if withUntagged == false
if !tagged && !withUntagged {
continue
}
// If explicitly ignored using "-", ignore the field.
if tags == "-" {
continue
}
fieldName := structField.Name
// Put together the field key and options.
var options fieldOptions
var keyPart string
keyPart, options, err = parseTag(tags, parentOptions)
if err != nil {
err = fmt.Errorf("%w %s: %s", ErrBadTags, fieldName, err)
return
}
// If the key part is empty, use the field name. They will be formatted and joined later by a parameter source.
if keyPart == "" {
keyPart = fieldName
}
// If a field-id was not set, use the key part.
if options.id == "" {
options.id = keyPart
}
// Make the field key by appending the field key part to the prefix.
// This might be ignored if the field is flattened.
fieldKey := append(prefix, keyPart)
// If the field is a pointer, and it's nil, create a new instance.
// Iterate over the pointer until we get to the actual struct.
for f.Kind() == reflect.Ptr {
if f.IsNil() {
// If the field is not a struct, we can't zero it out.
if f.Type().Elem().Kind() != reflect.Struct {
break
}
// Initialize the pointer with a new instance.
f.Set(reflect.New(f.Type().Elem()))
}
// Drill down to the next level.
f = f.Elem()
}
switch {
// If the field is a struct, and it's not a Setter, TextUnmarshaler, or BinaryUnmarshaler, i.e. it can't
// deserialize itself, recursively extract fields, appending the field key as we go.
case f.Kind() == reflect.Struct &&
setterFrom(f) == nil && textUnmarshaler(f) == nil && binaryUnmarshaler(f) == nil:
// If the field is anonymous, and it's set to flatten, we don't want to append the field key part.
innerPrefix := fieldKey
if structField.Anonymous || options.flatten {
innerPrefix = prefix
}
embeddedPtr := f.Addr().Interface()
// Recursively extract fields from the embedded struct.
var innerFields []fieldInfo
innerFields, err = extractFields(withUntagged, innerPrefix, embeddedPtr, options)
if err != nil {
return
}
// Append the inner fields to the list of fields.
fields = append(fields, innerFields...)
default:
// Append the field to the list of fields.
fields = append(fields, fieldInfo{
nameParts: fieldKey,
structField: f,
options: options,
})
}
}
return fields, nil
}
// parseTag parses the tag and returns the key and options.
func parseTag(tag string, parentOptions fieldOptions) (key string, f fieldOptions, err error) {
// Inherit the parent options.
f.inherit(parentOptions)
if tag == "" {
return
}
// Split the tag into parts.
parts := strings.Split(tag, ",")
// The first part is the key.
key = parts[0]
if len(parts) == 1 {
return
}
// Process the options.
for _, part := range parts[1:] {
// Split the part into key and value.
vals := strings.SplitN(part, ":", 2)
prop := vals[0]
switch len(vals) {
case 1:
switch prop {
case "optional":
f.optional = true
case "flatten":
f.flatten = true
}
case 2:
val := strings.TrimSpace(vals[1])
if val == "" {
err = fmt.Errorf("tag %q missing a value", prop)
return
}
switch prop {
case "default":
f.defaultValue = val
case "source":
f.source = val
case "refresh": // refresh is a duration
f.refresh, err = time.ParseDuration(val)
if err != nil || f.refresh <= 0 {
err = fmt.Errorf("invalid duration %q: %w", val, err)
return
}
case "id":
f.id = val
}
}
}
return
}
// processFieldValue sets the value of a field based on its type.
func processFieldValue(isDefaultValue bool, value string, field reflect.Value) (err error) {
t := field.Type()
// If the field is a pointer, dereference it.
if t.Kind() == reflect.Ptr {
t = t.Elem()
if field.IsNil() {
field.Set(reflect.New(t))
}
field = field.Elem()
}
// If the field is a zero value, and the value is the default value, skip it.
if isDefaultValue && !field.IsZero() {
return nil
}
// If it implements the Setter interface, use it.
if setter := setterFrom(field); setter != nil {
return setter.Set(value)
}
// If it implements the TextUnmarshaler use it.
if tu := textUnmarshaler(field); tu != nil {
return tu.UnmarshalText([]byte(value))
}
// If it implements the BinaryUnmarshaler use it.
if bu := binaryUnmarshaler(field); bu != nil {
return bu.UnmarshalBinary([]byte(value))
}
// Process the value based on the type of the field.
switch t.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var val int64
// If the field is a time.Duration, parse the duration.
if field.Kind() == reflect.Int64 && t.PkgPath() == "time" && t.Name() == "Duration" {
var d time.Duration
d, err = time.ParseDuration(value)
val = int64(d)
} else {
// Otherwise, parse the integer.
val, err = strconv.ParseInt(value, 0, t.Bits())
}
if err == nil { // if no error
field.SetInt(val)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// Parse the unsigned integer.
var val uint64
val, err = strconv.ParseUint(value, 0, t.Bits())
if err == nil { // if no error
field.SetUint(val)
}
case reflect.Bool:
// Parse the boolean.
var val bool
val, err = strconv.ParseBool(value)
if err == nil { // if no error
field.SetBool(val)
}
case reflect.Float32, reflect.Float64:
// Parse the float.
var val float64
val, err = strconv.ParseFloat(value, t.Bits())
if err == nil { // if no error
field.SetFloat(val)
}
case reflect.Slice:
// Split the value into parts and load them into the slice.
vals := strings.Split(value, ";")
sl := reflect.MakeSlice(t, len(vals), len(vals))
for i, val := range vals {
err = processFieldValue(false, val, sl.Index(i))
if err != nil {
return
}
}
field.Set(sl)
case reflect.Map:
// Split the value into pairs and load them into the map.
mp := reflect.MakeMap(t)
if len(strings.TrimSpace(value)) != 0 {
pairs := strings.Split(value, ";")
for _, pair := range pairs {
kvpair := strings.Split(pair, ":")
if len(kvpair) != 2 {
err = fmt.Errorf("invalid map item: %q", pair)
return
}
k := reflect.New(t.Key()).Elem()
err = processFieldValue(false, kvpair[0], k)
if err != nil {
return
}
v := reflect.New(t.Elem()).Elem()
err = processFieldValue(false, kvpair[1], v)
if err != nil {
return
}
mp.SetMapIndex(k, v)
}
}
field.Set(mp)
default:
err = fmt.Errorf("unexpected type %s when processing values", field.Type())
}
return
}
func interfaceFrom(field reflect.Value, fn func(interface{}, *bool)) {
if !field.CanInterface() {
return
}
var ok bool
fn(field.Interface(), &ok)
if !ok && field.CanAddr() {
fn(field.Addr().Interface(), &ok)
}
}
// setterFrom gets Setter from the field.
func setterFrom(field reflect.Value) (s Setter) {
interfaceFrom(field, func(v interface{}, ok *bool) { s, *ok = v.(Setter) })
return s
}
// textUnmarshaler gets encoding.TextUnmarshaler from the field.
func textUnmarshaler(field reflect.Value) (t encoding.TextUnmarshaler) {
interfaceFrom(field, func(v interface{}, ok *bool) { t, *ok = v.(encoding.TextUnmarshaler) })
return t
}
// binaryUnmarshaler gets encoding.BinaryUnmarshaler from the field.
func binaryUnmarshaler(field reflect.Value) (b encoding.BinaryUnmarshaler) {
interfaceFrom(field, func(v interface{}, ok *bool) { b, *ok = v.(encoding.BinaryUnmarshaler) })
return b
}