-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields_test.go
597 lines (557 loc) · 15 KB
/
fields_test.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package skyconf
import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
"time"
)
func Test_parseTag(t *testing.T) {
tests := []struct {
name string
tag string
parentOptions fieldOptions
wantKey string
wantF fieldOptions
wantErr assert.ErrorAssertionFunc
}{
{
name: "empty tag",
tag: "",
wantKey: "",
wantF: fieldOptions{},
wantErr: assert.NoError,
},
{
name: "key only tag",
tag: "key",
wantKey: "key",
wantF: fieldOptions{},
wantErr: assert.NoError,
},
{
name: "optional tag",
tag: ",optional",
wantKey: "",
wantF: fieldOptions{optional: true},
wantErr: assert.NoError,
},
{
name: "flatten tag",
tag: ",flatten",
wantKey: "",
wantF: fieldOptions{flatten: true},
wantErr: assert.NoError,
},
{
name: "default tag",
tag: ",default:default",
wantKey: "",
wantF: fieldOptions{defaultValue: "default"},
wantErr: assert.NoError,
},
{
name: "source tag",
tag: ",source:source",
wantKey: "",
wantF: fieldOptions{source: "source"},
wantErr: assert.NoError,
},
{
name: "optional,flatten,default,source tag",
tag: ",optional,flatten,default:default,source:source",
wantKey: "",
wantF: fieldOptions{optional: true, flatten: true, defaultValue: "default", source: "source"},
wantErr: assert.NoError,
},
{
name: "missing value",
tag: ",default:",
wantErr: assert.Error,
},
{
name: "missing value",
tag: ",source:",
wantErr: assert.Error,
},
{
name: "inherit parent option for 'source'",
tag: "key",
parentOptions: fieldOptions{
defaultValue: "default",
optional: true,
flatten: true,
source: "parent-source",
},
wantKey: "key",
wantF: fieldOptions{
source: "parent-source",
},
wantErr: assert.NoError,
},
{
name: "inherit parent options, but attribute options take precedence",
tag: "key,source:my-source",
parentOptions: fieldOptions{
defaultValue: "default",
optional: true,
flatten: true,
source: "parent-source",
},
wantKey: "key",
wantF: fieldOptions{
source: "my-source",
},
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotKey, gotF, err := parseTag(tt.tag, tt.parentOptions)
if !tt.wantErr(t, err, "parseTag(%v)", tt.tag) {
return
}
if err != nil {
return
}
assert.Equalf(t, tt.wantKey, gotKey, "parseTag(%v)", tt.tag)
assert.Equalf(t, tt.wantF, gotF, "parseTag(%v)", tt.tag)
})
}
}
type mockSetter string
func (m *mockSetter) Set(value string) error {
*m = mockSetter(value)
return nil
}
type mockSetterWithErr string
func (m *mockSetterWithErr) Set(_ string) error {
return assert.AnError
}
type mockTextUnmarshaler string
func (m *mockTextUnmarshaler) UnmarshalText(text []byte) error {
*m = mockTextUnmarshaler(text)
return nil
}
type mockTextUnmarshalerWithErr string
func (m *mockTextUnmarshalerWithErr) UnmarshalText(_ []byte) error {
return assert.AnError
}
type mockBinaryUnmarshaler string
func (m *mockBinaryUnmarshaler) UnmarshalBinary(data []byte) error {
*m = mockBinaryUnmarshaler(data)
return nil
}
type mockBinaryUnmarshalerWithErr string
func (m *mockBinaryUnmarshalerWithErr) UnmarshalBinary(_ []byte) error {
return assert.AnError
}
func Test_processFieldValue(t *testing.T) {
nonZeroString := new(string)
*nonZeroString = "non-zero-value"
tests := []struct {
name string
isDefaultValue bool
value string
field reflect.Value
expected interface{}
expectErr bool
}{
{
name: "string field",
isDefaultValue: false,
value: "test",
field: reflect.ValueOf(new(string)).Elem(),
expected: "test",
expectErr: false,
},
{
name: "int field",
isDefaultValue: false,
value: "123",
field: reflect.ValueOf(new(int)).Elem(),
expected: 123,
expectErr: false,
},
{
name: "bool field",
isDefaultValue: false,
value: "true",
field: reflect.ValueOf(new(bool)).Elem(),
expected: true,
expectErr: false,
},
{
name: "float field",
isDefaultValue: false,
value: "1.23",
field: reflect.ValueOf(new(float64)).Elem(),
expected: 1.23,
expectErr: false,
},
{
name: "duration field",
isDefaultValue: false,
value: "1h",
field: reflect.ValueOf(new(time.Duration)).Elem(),
expected: time.Hour,
expectErr: false,
},
{
name: "slice field",
isDefaultValue: false,
value: "a;b;c",
field: reflect.ValueOf(new([]string)).Elem(),
expected: []string{"a", "b", "c"},
expectErr: false,
},
{
name: "slice field with mismatched type",
isDefaultValue: false,
value: "1;2;A",
field: reflect.ValueOf(new([]int)).Elem(),
expected: []string{},
expectErr: true,
},
{
name: "map field",
isDefaultValue: false,
value: "key1:val1;key2:val2",
field: reflect.ValueOf(new(map[string]string)).Elem(),
expected: map[string]string{"key1": "val1", "key2": "val2"},
expectErr: false,
},
{
name: "map field with invalid entry",
isDefaultValue: false,
value: "key1:val1;key2-val2",
field: reflect.ValueOf(new(map[string]string)).Elem(),
expected: map[string]string{},
expectErr: true,
},
{
name: "map field with invalid key",
isDefaultValue: false,
value: "1:1;key2:2",
field: reflect.ValueOf(new(map[int]int)).Elem(),
expected: map[int]int{},
expectErr: true,
},
{
name: "map field with invalid value",
isDefaultValue: false,
value: "key1:1;key2:val2",
field: reflect.ValueOf(new(map[string]int)).Elem(),
expected: map[string]int{},
expectErr: true,
},
{
name: "invalid int field",
isDefaultValue: false,
value: "abc",
field: reflect.ValueOf(new(int)).Elem(),
expected: 0,
expectErr: true,
},
{
name: "uint field",
isDefaultValue: false,
value: "123",
field: reflect.ValueOf(new(uint)).Elem(),
expected: uint(123),
expectErr: false,
},
{
name: "uint8 field",
isDefaultValue: false,
value: "255",
field: reflect.ValueOf(new(uint8)).Elem(),
expected: uint8(255),
expectErr: false,
},
{
name: "uint16 field",
isDefaultValue: false,
value: "65535",
field: reflect.ValueOf(new(uint16)).Elem(),
expected: uint16(65535),
expectErr: false,
},
{
name: "uint32 field",
isDefaultValue: false,
value: "4294967295",
field: reflect.ValueOf(new(uint32)).Elem(),
expected: uint32(4294967295),
expectErr: false,
},
{
name: "uint64 field",
isDefaultValue: false,
value: "18446744073709551615",
field: reflect.ValueOf(new(uint64)).Elem(),
expected: uint64(18446744073709551615),
expectErr: false,
},
{
name: "invalid uint field",
isDefaultValue: false,
value: "-1",
field: reflect.ValueOf(new(uint)).Elem(),
expected: uint(0),
expectErr: true,
},
{
name: "setter field",
isDefaultValue: false,
value: "test",
field: reflect.ValueOf(new(mockSetter)).Elem(),
expected: mockSetter("test"),
},
{
name: "setter field with error",
isDefaultValue: false,
value: "test",
field: reflect.ValueOf(new(mockSetterWithErr)).Elem(),
expectErr: true,
},
{
name: "text unmarshaler field",
isDefaultValue: false,
value: "test",
field: reflect.ValueOf(new(mockTextUnmarshaler)).Elem(),
expected: mockTextUnmarshaler("test"),
},
{
name: "text unmarshaler field with error",
isDefaultValue: false,
value: "test",
field: reflect.ValueOf(new(mockTextUnmarshalerWithErr)).Elem(),
expectErr: true,
},
{
name: "binary unmarshaler field",
isDefaultValue: false,
value: "test",
field: reflect.ValueOf(new(mockBinaryUnmarshaler)).Elem(),
expected: mockBinaryUnmarshaler("test"),
},
{
name: "binary unmarshaler field with error",
isDefaultValue: false,
value: "test",
field: reflect.ValueOf(new(mockBinaryUnmarshalerWithErr)).Elem(),
expectErr: true,
},
{
name: "non-zero default value",
isDefaultValue: true,
value: "test",
field: reflect.ValueOf(nonZeroString).Elem(),
expected: *nonZeroString,
},
{
name: "pointer field",
isDefaultValue: false,
value: *nonZeroString,
field: reflect.ValueOf(new(*string)).Elem(),
expected: nonZeroString,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := processFieldValue(tt.isDefaultValue, tt.value, tt.field)
if tt.expectErr {
assert.Error(t, err)
} else {
if assert.NoError(t, err) {
assert.Equal(t, tt.expected, tt.field.Interface())
}
}
})
}
}
func Test_extractFields(t *testing.T) {
prefix := []string{"prefix"}
var target interface{}
var err error
// Tags with errors
err = nil
target = &struct {
Field1 string `sky:"field1,source:"`
}{}
_, err = extractFields(true, nil, target, fieldOptions{})
assert.Error(t, err)
// Bad config; not a struct
err = nil
targetStr := "not a struct"
target = &targetStr
_, err = extractFields(true, nil, target, fieldOptions{})
assert.Error(t, err)
// Bad config; not a pointer to a struct
err = nil
target = struct {
Field1 string `sky:"field1"`
}{}
_, err = extractFields(true, nil, target, fieldOptions{})
assert.Error(t, err)
// Uninitialised struct field will be initialised with zero value
err = nil
type AStruct struct {
Field2 string `sky:"field2"`
}
type AConfig struct {
Field1 string `sky:"field1"`
A *AStruct
}
target = &AConfig{}
_, err = extractFields(true, nil, target, fieldOptions{})
assert.NoError(t, err)
assert.NotNil(t, target.(*AConfig).A)
// A realistic example
err = nil
type Embedded1 struct {
EmbeddedField1 string `sky:"embedded1_field1"`
EmbeddedField2 int `sky:"embedded1_field2"`
}
type Embedded2 struct {
EmbeddedField1 string `sky:"field1"`
EmbeddedField2 int `sky:"field2"`
}
type ConfigStruct struct {
Field1 string `sky:""`
Field2 int `sky:"field2,optional"`
Field3 bool `sky:"field3,flatten"`
Field4 string `sky:"field4,default:default"`
Field5 string `sky:"field5,source:source"`
Field6 string
Field7 int
Field8 time.Duration
Field9 int `sky:"-"`
Embedded1 `sky:""`
Embedded2
// Unexported fields are ignored
field10 string `sky:"field10"`
}
target = &ConfigStruct{}
// withUntagged = true
gotFields, err := extractFields(true, prefix, target, fieldOptions{})
assert.NoError(t, err)
expectedFields := []fieldInfo{
{
nameParts: []string{"prefix", "Field1"},
structField: reflect.ValueOf(""),
options: fieldOptions{id: "Field1"},
},
{
nameParts: []string{"prefix", "field2"},
structField: reflect.ValueOf(0),
options: fieldOptions{optional: true, id: "field2"},
},
{
nameParts: []string{"prefix", "field3"},
structField: reflect.ValueOf(false),
options: fieldOptions{flatten: true, id: "field3"},
},
{
nameParts: []string{"prefix", "field4"},
structField: reflect.ValueOf(""),
options: fieldOptions{defaultValue: "default", id: "field4"},
},
{
nameParts: []string{"prefix", "field5"},
structField: reflect.ValueOf(""),
options: fieldOptions{source: "source", id: "field5"},
},
{
nameParts: []string{"prefix", "Field6"},
structField: reflect.ValueOf(""),
options: fieldOptions{id: "Field6"},
},
{
nameParts: []string{"prefix", "Field7"},
structField: reflect.ValueOf(0),
options: fieldOptions{id: "Field7"},
},
{
nameParts: []string{"prefix", "Field8"},
structField: reflect.ValueOf(time.Duration(0)),
options: fieldOptions{id: "Field8"},
},
{
nameParts: []string{"prefix", "embedded1_field1"},
structField: reflect.ValueOf("string"),
options: fieldOptions{id: "embedded1_field1"},
},
{
nameParts: []string{"prefix", "embedded1_field2"},
structField: reflect.ValueOf(0),
options: fieldOptions{id: "embedded1_field2"},
},
{
nameParts: []string{"prefix", "field1"},
structField: reflect.ValueOf("string"),
options: fieldOptions{id: "field1"},
},
{
nameParts: []string{"prefix", "field2"},
structField: reflect.ValueOf(0),
options: fieldOptions{id: "field2"},
},
}
if !assert.Equal(t, len(expectedFields), len(gotFields)) {
return
}
for i, expectedField := range expectedFields {
assert.Equal(t, expectedField.nameParts, gotFields[i].nameParts)
assert.Equal(t, expectedField.structField.Type(), gotFields[i].structField.Type())
assert.Equal(t, expectedField.options, gotFields[i].options)
}
// withUntagged = false
gotFields, err = extractFields(false, prefix, target, fieldOptions{})
assert.NoError(t, err)
expectedFields = []fieldInfo{
{
nameParts: []string{"prefix", "Field1"},
structField: reflect.ValueOf(""),
options: fieldOptions{id: "Field1"},
},
{
nameParts: []string{"prefix", "field2"},
structField: reflect.ValueOf(0),
options: fieldOptions{optional: true, id: "field2"},
},
{
nameParts: []string{"prefix", "field3"},
structField: reflect.ValueOf(false),
options: fieldOptions{flatten: true, id: "field3"},
},
{
nameParts: []string{"prefix", "field4"},
structField: reflect.ValueOf(""),
options: fieldOptions{defaultValue: "default", id: "field4"},
},
{
nameParts: []string{"prefix", "field5"},
structField: reflect.ValueOf(""),
options: fieldOptions{source: "source", id: "field5"},
},
{
nameParts: []string{"prefix", "embedded1_field1"},
structField: reflect.ValueOf("string"),
options: fieldOptions{id: "embedded1_field1"},
},
{
nameParts: []string{"prefix", "embedded1_field2"},
structField: reflect.ValueOf(0),
options: fieldOptions{id: "embedded1_field2"},
},
}
if !assert.Equal(t, len(expectedFields), len(gotFields)) {
return
}
for i, expectedField := range expectedFields {
assert.Equal(t, expectedField.nameParts, gotFields[i].nameParts)
assert.Equal(t, expectedField.structField.Type(), gotFields[i].structField.Type())
assert.Equal(t, expectedField.options, gotFields[i].options)
}
}