-
Notifications
You must be signed in to change notification settings - Fork 25
/
defaults.go
466 lines (409 loc) · 12.2 KB
/
defaults.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package config
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/fluxninja/aperture/pkg/log"
)
// Most of the code ported from github.com/mcuadros/go-defaults
type fieldData struct {
Parent *fieldData
TagValue string
Value reflect.Value
Field reflect.StructField
}
type fillerFunc func(field *fieldData)
// filler contains all the functions to fill any struct field with any type allowing to define function by Kind, Type of field name.
type filler struct {
FuncByName map[string]fillerFunc
FuncByType map[typeHash]fillerFunc
FuncByKind map[reflect.Kind]fillerFunc
Tag string
}
// fill apply all the functions contained on Filler, setting all the possible
// values.
func (f *filler) fill(variable interface{}) {
fields := f.getFields(variable)
f.setDefaultValues(fields)
}
func (f *filler) getFields(variable interface{}) []*fieldData {
valueObject := reflect.ValueOf(variable).Elem()
return f.getFieldsFromValue(valueObject, nil)
}
func (f *filler) getFieldsFromValue(valueObject reflect.Value, parent *fieldData) []*fieldData {
typeObject := valueObject.Type()
count := valueObject.NumField()
var results []*fieldData
for i := 0; i < count; i++ {
value := valueObject.Field(i)
field := typeObject.Field(i)
if value.CanSet() {
results = append(results, &fieldData{
Value: value,
Field: field,
TagValue: field.Tag.Get(f.Tag),
Parent: parent,
})
}
}
return results
}
func (f *filler) setDefaultValues(fields []*fieldData) {
for _, field := range fields {
if f.isEmpty(field) {
f.setDefaultValue(field)
}
}
}
func (f *filler) isEmpty(field *fieldData) bool {
switch field.Value.Kind() {
case reflect.Bool:
return !field.Value.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Value.Int() == 0
case reflect.Float32, reflect.Float64:
return field.Value.Float() == .0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return field.Value.Uint() == 0
case reflect.Slice:
switch field.Value.Type().Elem().Kind() {
case reflect.Struct, reflect.Ptr, reflect.Interface:
// always assume the structs, ptrs, interfaces in the slice is empty and can be filled
// the actually filling logic should take care of the rest
return true
default:
return field.Value.Len() == 0
}
case reflect.String:
return field.Value.String() == ""
}
return true
}
func (f *filler) setDefaultValue(field *fieldData) {
getters := []func(field *fieldData) fillerFunc{
f.getFunctionByName,
f.getFunctionByType,
f.getFunctionByKind,
}
for _, getter := range getters {
filler := getter(field)
if filler != nil {
filler(field)
return
}
}
}
func (f *filler) getFunctionByName(field *fieldData) fillerFunc {
if f, ok := f.FuncByName[field.Field.Name]; ok {
return f
}
return nil
}
func (f *filler) getFunctionByType(field *fieldData) fillerFunc {
if f, ok := f.FuncByType[getTypeHash(field.Field.Type)]; ok {
return f
}
return nil
}
func (f *filler) getFunctionByKind(field *fieldData) fillerFunc {
if f, ok := f.FuncByKind[field.Field.Type.Kind()]; ok {
return f
}
return nil
}
// typeHash is a string representing a reflect.Type following the next pattern:
// <package.name>.<type.name>.
type typeHash string
// getTypeHash returns the TypeHash for a given reflect.Type.
func getTypeHash(t reflect.Type) typeHash {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return typeHash(fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()))
}
// SetDefaults applies the default values to the struct object, the struct type must have
// the StructTag with name "default" and the directed value.
// Example usage:
//
// type ExampleBasic struct {
// Foo bool `default:"true"`
// Bar string `default:"33"`
// Qux int8
// Dur time.Duration `default:"2m3s"`
// }
//
// foo := &ExampleBasic{}
// SetDefaults(foo)
func SetDefaults(variable interface{}) {
kind := reflect.ValueOf(variable).Elem().Kind()
if kind != reflect.Struct {
// Panic early with clear message to avoid cryptic errors from inner functions.
// Note: Perhaps we can lift this limitation.
panic(fmt.Sprintf("SetDefaults can be used only on structs, got %v", kind))
}
getDefaultFiller().fill(variable)
}
var defaultFiller *filler = nil
func init() {
defaultFiller = getDefaultFiller()
}
func getDefaultFiller() *filler {
if defaultFiller == nil {
defaultFiller = newDefaultFiller()
}
return defaultFiller
}
func newDefaultFiller() *filler {
filler := &filler{}
var setPtrDefaults func(val reflect.Value)
setPtrDefaults = func(val reflect.Value) {
// handle all non-nil pointers
if val.IsNil() {
return
}
if val.Elem().Kind() == reflect.Struct {
SetDefaults(val.Interface())
} else if val.Elem().Kind() == reflect.Ptr || val.Elem().Kind() == reflect.Interface {
setPtrDefaults(val.Elem())
}
}
funcs := make(map[reflect.Kind]fillerFunc, 0)
funcs[reflect.Bool] = func(field *fieldData) {
value, _ := strconv.ParseBool(field.TagValue)
field.Value.SetBool(value)
}
funcs[reflect.Int] = func(field *fieldData) {
value, _ := strconv.ParseInt(field.TagValue, 10, 64)
field.Value.SetInt(value)
}
funcs[reflect.Int8] = funcs[reflect.Int]
funcs[reflect.Int16] = funcs[reflect.Int]
funcs[reflect.Int32] = funcs[reflect.Int]
funcs[reflect.Int64] = func(field *fieldData) {
if field.Field.Type == reflect.TypeOf(time.Second) {
value, _ := time.ParseDuration(field.TagValue)
field.Value.Set(reflect.ValueOf(value))
} else {
value, _ := strconv.ParseInt(field.TagValue, 10, 64)
field.Value.SetInt(value)
}
}
funcs[reflect.Float32] = func(field *fieldData) {
value, _ := strconv.ParseFloat(field.TagValue, 64)
field.Value.SetFloat(value)
}
funcs[reflect.Float64] = funcs[reflect.Float32]
funcs[reflect.Uint] = func(field *fieldData) {
value, _ := strconv.ParseUint(field.TagValue, 10, 64)
field.Value.SetUint(value)
}
funcs[reflect.Uint8] = funcs[reflect.Uint]
funcs[reflect.Uint16] = funcs[reflect.Uint]
funcs[reflect.Uint32] = funcs[reflect.Uint]
funcs[reflect.Uint64] = funcs[reflect.Uint]
funcs[reflect.String] = func(field *fieldData) {
tagValue := parseDateTimeString(field.TagValue)
field.Value.SetString(tagValue)
}
funcs[reflect.Struct] = func(field *fieldData) {
fields := filler.getFieldsFromValue(field.Value, nil)
filler.setDefaultValues(fields)
}
funcs[reflect.Ptr] = func(field *fieldData) {
val := field.Value
setPtrDefaults(val)
}
funcs[reflect.Interface] = func(field *fieldData) {
val := field.Value
setPtrDefaults(val)
}
types := make(map[typeHash]fillerFunc, 5)
types["time.Duration"] = func(field *fieldData) {
d, _ := time.ParseDuration(field.TagValue)
field.Value.Set(reflect.ValueOf(d))
}
types[getTypeHash(reflect.TypeOf(Duration{}))] = func(field *fieldData) {
if value, ok := field.Value.Interface().(Duration); ok {
if value.AsDuration() != 0 {
return
}
if field.TagValue != "" {
durationJSON, _ := json.Marshal(field.TagValue)
dur := Duration{}
dur.duration = durationpb.New(0)
err := json.Unmarshal(durationJSON, &dur)
if err != nil {
log.Error().Err(err).Msg("Unable to unmarshal default duration")
}
field.Value.Set(reflect.ValueOf(dur))
}
}
}
types[getTypeHash(reflect.TypeOf(durationpb.New(0)))] = func(field *fieldData) {
iface := field.Value.Interface()
if value, ok := iface.(*durationpb.Duration); ok {
if value.AsDuration() != 0 {
return
}
if field.TagValue != "" {
durationJSON, _ := json.Marshal(field.TagValue)
dur := durationpb.New(0)
err := protojson.Unmarshal(durationJSON, dur)
if err != nil {
log.Error().Err(err).Msg("Unable to unmarshal duration default")
}
field.Value.Set(reflect.ValueOf(dur))
}
}
}
types[getTypeHash(reflect.TypeOf(Time{}))] = func(field *fieldData) {
if value, ok := field.Value.Interface().(Time); ok {
if field.TagValue != "" {
nullTime := time.Time{}
if value.timestamp.AsTime() != nullTime {
return
}
timestampJSON, _ := json.Marshal(field.TagValue)
t := &Time{
timestamp: timestamppb.Now(),
}
err := json.Unmarshal(timestampJSON, t)
if err != nil {
log.Error().Err(err).Msg("Unable to unmarshal default config.Timestamp")
}
field.Value.Set(reflect.ValueOf(t))
}
}
}
types[getTypeHash(reflect.TypeOf(timestamppb.Now()))] = func(field *fieldData) {
iface := field.Value.Interface()
if value, ok := iface.(*timestamppb.Timestamp); ok {
nullTime := time.Time{}
if value.AsTime() != nullTime {
return
}
if field.TagValue != "" {
timestampJSON, _ := json.Marshal(field.TagValue)
t := &Time{
timestamp: timestamppb.Now(),
}
err := json.Unmarshal(timestampJSON, t)
if err != nil {
log.Error().Err(err).Msg("Unable to unmarshal default timestamppb")
}
field.Value.Set(reflect.ValueOf(t))
}
}
}
funcs[reflect.Slice] = func(field *fieldData) {
k := field.Value.Type().Elem().Kind()
switch k {
case reflect.Uint8:
if field.Value.Bytes() != nil {
return
}
field.Value.SetBytes([]byte(field.TagValue))
case reflect.Struct:
count := field.Value.Len()
for i := 0; i < count; i++ {
fields := filler.getFieldsFromValue(field.Value.Index(i), nil)
filler.setDefaultValues(fields)
}
case reflect.Ptr, reflect.Interface:
count := field.Value.Len()
for i := 0; i < count; i++ {
val := field.Value.Index(i)
setPtrDefaults(val)
}
// FIXME: also descend into slice of slices
// FIXME: also descend into slice of maps
default:
reg := regexp.MustCompile(`^\[(.*)\]$`)
matches := reg.FindStringSubmatch(field.TagValue)
if len(matches) != 2 {
return
}
if matches[1] == "" {
field.Value.Set(reflect.MakeSlice(field.Value.Type(), 0, 0))
} else {
defaultValue := strings.Split(matches[1], ",")
result := reflect.MakeSlice(field.Value.Type(), len(defaultValue), len(defaultValue))
for i := 0; i < len(defaultValue); i++ {
itemValue := result.Index(i)
item := &fieldData{
Value: itemValue,
Field: reflect.StructField{},
TagValue: defaultValue[i],
Parent: nil,
}
funcs[k](item)
}
field.Value.Set(result)
}
}
}
funcs[reflect.Map] = func(field *fieldData) {
switch field.Field.Type.Elem().Kind() {
case reflect.Struct:
keys := field.Value.MapKeys()
for _, key := range keys {
// We need to pull the value from map, copy to a settable
// location, fill in defaults, and then put it back into map.
value := field.Value.MapIndex(key)
tmp := reflect.New(value.Type()).Elem()
tmp.Set(value)
fields := filler.getFieldsFromValue(tmp, nil)
filler.setDefaultValues(fields)
field.Value.SetMapIndex(key, tmp)
}
case reflect.Ptr, reflect.Interface:
iter := field.Value.MapRange()
for iter.Next() {
setPtrDefaults(iter.Value())
}
case reflect.Slice:
// FIXME: also descend into map of slices
case reflect.Map:
// FIXME: also descend into map of maps
}
// FIXME: handle actual default Tags on maps?
}
filler.FuncByKind = funcs
filler.FuncByType = types
filler.Tag = "default"
return filler
}
func parseDateTimeString(data string) string {
pattern := regexp.MustCompile(`\{\{(\w+\:(?:-|)\d*,(?:-|)\d*,(?:-|)\d*)\}\}`)
matches := pattern.FindAllStringSubmatch(data, -1) // matches is [][]string
for _, match := range matches {
tags := strings.Split(match[1], ":")
if len(tags) == 2 {
valueStrings := strings.Split(tags[1], ",")
if len(valueStrings) == 3 {
var values [3]int
for key, valueString := range valueStrings {
num, _ := strconv.ParseInt(valueString, 10, 64)
values[key] = int(num)
}
switch tags[0] {
case "date":
str := time.Now().AddDate(values[0], values[1], values[2]).Format("2006-01-02")
data = strings.ReplaceAll(data, match[0], str)
case "time":
str := time.Now().Add((time.Duration(values[0]) * time.Hour) +
(time.Duration(values[1]) * time.Minute) +
(time.Duration(values[2]) * time.Second)).Format("15:04:05")
data = strings.ReplaceAll(data, match[0], str)
}
}
}
}
return data
}