-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfx_test.go
More file actions
810 lines (719 loc) · 22.8 KB
/
confx_test.go
File metadata and controls
810 lines (719 loc) · 22.8 KB
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
package confx_test
import (
"context"
"encoding/base64"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/qor5/confx"
"github.com/samber/lo"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type DatabaseConfig struct {
Host string `confx:"host" usage:"Database host" validate:"required"`
Port int `confx:"port" usage:"Database port" validate:"gte=1,lte=65535"`
}
type JWTConfig struct {
SigningAlgorithm string `confx:"signingAlgorithm"`
PrivateKey string `confx:"privateKey"`
PublicKey string `confx:"publicKey"`
}
type AuthConfig struct {
JWT *JWTConfig `confx:"jwt"`
AccessTokenMaxAge time.Duration `confx:"accessTokenMaxAge"`
RefreshTokenMaxAge time.Duration `confx:"refreshTokenMaxAge"`
}
type JSONPath struct {
Path string `confx:"path" json:"path"`
Hash bool `confx:"hash" json:"hash"`
}
type ProtectionConfig struct {
PrivateFields []JSONPath `confx:"privateFields"`
PublicFields []JSONPath `confx:"publicFields"`
}
type ExtraConfig struct {
Bool bool `confx:"bool"`
Float32 float32 `confx:"float32"`
Float64 float64 `confx:"float64"`
Int int `confx:"int"`
Int8 int8 `confx:"int8"`
Int16 int16 `confx:"int16"`
Int32 int32 `confx:"int32"`
Int64 int64 `confx:"int64"`
Duration time.Duration `confx:"duration"`
String string `confx:"string"`
Uint64 uint64 `confx:"uint64"`
Uint32 uint32 `confx:"uint32"`
Uint16 uint16 `confx:"uint16"`
Uint8 uint8 `confx:"uint8"`
Uint uint `confx:"uint"`
BoolSlice []bool `confx:"boolSlice" validate:"required,min=1"`
Float32Slice []float32 `confx:"float32Slice"`
Float64Slice []float64 `confx:"float64Slice"`
IntSlice []int `confx:"intSlice"`
Int32Slice []int32 `confx:"int32Slice"`
Int64Slice []int64 `confx:"int64Slice"`
DurationSlice []time.Duration `confx:"durationSlice"`
StringSlice []string `confx:"stringSlice"`
UintSlice []uint `confx:"uintSlice"`
Uint32Slice []uint32 `confx:"uint32Slice"`
Uint64Slice []uint64 `confx:"uint64Slice"`
Uint8Slice []uint8 `confx:"uint8Slice"`
Bytes []byte `confx:"bytes"`
StringToString map[string]string `confx:"stringToString"`
StringToInt map[string]int `confx:"stringToInt"`
StringToInt64 map[string]int64 `confx:"stringToInt64"`
EmptyStringToInt64 map[string]int64 `confx:"emptyStringToInt64"`
BytesForEnv []byte `confx:"bytesForEnv"`
Time time.Time `confx:"time" validate:"required"`
StringPtr *string `confx:"stringPtr"`
StringPtrPtr **string `confx:"stringPtrPtr"`
}
// TestConfig defines a configuration structure for testing purposes.
type TestConfig struct {
Port int `confx:"port" usage:"Port to run the server on" validate:"gte=1,lte=65535"`
LogFiles []string `confx:"logFiles" usage:"List of log files" validate:"dive,required"`
RetryCounts []int `confx:"retryCounts" usage:"List of retry counts" validate:"dive,gte=0"`
Verbose bool `confx:"verbose" usage:"Enable verbose logging"`
MaxIdleConns int `usage:"Maximum number of idle connections"` // Missing confx tag, should default to field name.
Timeout time.Duration `confx:"timeout" usage:"Request timeout duration" validate:"gte=0"`
Database DatabaseConfig `confx:"database"`
Auth *AuthConfig `confx:"auth"`
Extra ExtraConfig `confx:"extra"`
Protection ProtectionConfig `confx:"protection"`
Ignore string `confx:"-"` // Ignored field.
private string // Private field.
privateFunc func() // Private function.
}
func TestInitializeLoader(t *testing.T) {
viper.Reset()
now := time.Now()
def := TestConfig{
Port: 8080,
LogFiles: []string{"/var/log/app1.log", "/var/log/app2.log"},
RetryCounts: []int{3, 5},
Verbose: true,
MaxIdleConns: 1,
Timeout: 30 * time.Second,
Database: DatabaseConfig{
Host: "localhost",
Port: 5432,
},
Extra: ExtraConfig{
Bool: true,
Float32: 1.1,
Float64: 2.2,
Int: 3,
Int8: 4,
Int16: 5,
Int32: 6,
Int64: 7,
Duration: 8 * time.Second,
String: "string",
Uint64: 9,
Uint32: 10,
Uint16: 11,
Uint8: 12,
Uint: 13,
BoolSlice: []bool{true, false},
Float32Slice: []float32{1.1, 2.2},
Float64Slice: []float64{1.1, 2.2},
IntSlice: []int{1, 2},
Int32Slice: []int32{1, 2},
Int64Slice: []int64{1, 2},
DurationSlice: []time.Duration{1 * time.Second, 2 * time.Second},
StringSlice: []string{"a", "b", "c"},
UintSlice: []uint{1, 2},
Uint32Slice: []uint32{1, 2},
Uint64Slice: []uint64{1, 2},
Uint8Slice: []uint8{1, 2},
Bytes: []byte("abc"),
Time: now,
StringToInt64: map[string]int64{
"keyA": 1,
},
},
Ignore: "ignored",
private: "private",
privateFunc: func() {},
}
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
loader, err := confx.Initialize(def, confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"))
require.NoError(t, err)
// Create a temporary configuration file.
tempDir := t.TempDir()
configFilePath := filepath.Join(tempDir, "config.yaml")
configFileContent := `
port: 6060
logFiles:
- "/var/log/app3.log"
- "/var/log/app4.log"
retryCounts:
- 33
- 55
# verbose: false
MaxIdleConns: 10
timeout: "33s"
database:
host: "localhost"
port: 5433
ignore: "ignored" # will be ignored
protection:
privateFields:
- path: fieldA
hash: true
- path: fieldB
hash: true
publicFields:
- path: fieldC
hash: true
- path: fieldD
hash: true
`
err = os.WriteFile(configFilePath, []byte(configFileContent), 0o644)
require.NoError(t, err)
// Set environment variables.
t.Setenv("APP_PORT", "9090")
t.Setenv("APP_LOG_FILES", "/tmp/app1.log,/tmp/app2.log")
t.Setenv("APP_DATABASE_HOST", "127.0.0.1")
t.Setenv("APP_DATABASE_PORT", "3306")
t.Setenv("APP_EXTRA_STRING_SLICE", "X,Y,Z")
t.Setenv("APP_EXTRA_INT_SLICE", "888,999")
t.Setenv("APP_EXTRA_STRING_TO_STRING", "key1=value1,key2=value2")
t.Setenv("APP_EXTRA_BYTES_FOR_ENV", base64.StdEncoding.EncodeToString([]byte("xyz")))
t.Setenv("APP_PROTECTION_PUBLIC_FIELDS", `[{"path":"fieldCFromEnv","hash":false},{"path":"fieldDFromEnv","hash":false}]`)
// Simulate command-line arguments.
args := []string{
"--port=7070",
"--retry-counts=4", "--retry-counts=6",
"--log-files=/tmp/app3.log", "--log-files=/tmp/app4.log",
"--extra-bool-slice=false", "--extra-bool-slice=true", "--extra-bool-slice=false",
"--extra-float64-slice=11.11,22.22",
"--extra-string-to-int=key1=1",
"--extra-string-to-int=key2=2",
"--extra-string-to-int64=key1=1,key2=2",
`--protection-private-fields=[{"path":"fieldAFromFlag","hash":false},{"path":"fieldBFromFlag","hash":false}]`,
}
err = flagSet.Parse(args)
require.NoError(t, err)
// Load configuration with the config file path.
config, err := loader(context.Background(), configFilePath)
require.NoError(t, err)
expected := TestConfig{
Port: 7070, // flag.
LogFiles: []string{"/tmp/app3.log", "/tmp/app4.log"}, // flag.
RetryCounts: []int{4, 6}, // flag.
Verbose: true, // default.
MaxIdleConns: 10, // yaml.
Timeout: 33 * time.Second, // yaml.
Database: DatabaseConfig{
Host: "127.0.0.1", // env.
Port: 3306, // env.
},
Auth: &AuthConfig{
JWT: &JWTConfig{},
},
Protection: ProtectionConfig{
PrivateFields: []JSONPath{
{
Path: "fieldAFromFlag", // flag
Hash: false,
},
{
Path: "fieldBFromFlag", // flag
Hash: false,
},
},
PublicFields: []JSONPath{
{
Path: "fieldCFromEnv", // env
Hash: false,
},
{
Path: "fieldDFromEnv", // env
Hash: false,
},
},
},
Extra: ExtraConfig{
Bool: true,
Float32: 1.1,
Float64: 2.2,
Int: 3,
Int8: 4,
Int16: 5,
Int32: 6,
Int64: 7,
Duration: 8 * time.Second,
String: "string",
Uint64: 9,
Uint32: 10,
Uint16: 11,
Uint8: 12,
Uint: 13,
BoolSlice: []bool{false, true, false}, // flag
Float32Slice: []float32{1.1, 2.2},
Float64Slice: []float64{11.11, 22.22}, // flag
IntSlice: []int{888, 999}, // env
Int32Slice: []int32{1, 2},
Int64Slice: []int64{1, 2},
DurationSlice: []time.Duration{1 * time.Second, 2 * time.Second},
StringSlice: []string{"X", "Y", "Z"}, // env
UintSlice: []uint{1, 2},
Uint32Slice: []uint32{1, 2},
Uint64Slice: []uint64{1, 2},
Uint8Slice: []uint8{1, 2},
Bytes: []byte("abc"),
StringToString: map[string]string{
"key1": "value1",
"key2": "value2",
}, // env
StringToInt: map[string]int{
"key1": 1,
"key2": 2,
}, // flag
StringToInt64: map[string]int64{
"key1": 1,
"key2": 2,
}, // flag
EmptyStringToInt64: map[string]int64{},
BytesForEnv: []byte("xyz"),
Time: now,
StringPtr: lo.ToPtr(""),
StringPtrPtr: lo.ToPtr(lo.ToPtr("")),
},
Ignore: "", // Ignored
private: "",
privateFunc: nil,
}
// Compare Time fields separately since they might have different internal representations
assert.WithinDuration(t, expected.Extra.Time, config.Extra.Time, time.Second)
// Set Time fields to zero value for full struct comparison
expected.Extra.Time = time.Time{}
config.Extra.Time = time.Time{}
assert.Equal(t, expected, config)
}
func TestValidationFailure(t *testing.T) {
viper.Reset()
def := &TestConfig{
Port: 8080,
LogFiles: []string{"/var/log/app1.log", ""}, // Invalid: empty string
RetryCounts: []int{-1, 5}, // Invalid: negative number
Verbose: false,
MaxIdleConns: 1,
Timeout: -10 * time.Second, // Invalid: negative duration
Database: DatabaseConfig{
Host: "", // Invalid: required
Port: 70000, // Invalid: exceeds max
},
Extra: ExtraConfig{
Time: time.Time{}, // Invalid: zero value
},
}
flagSet := pflag.NewFlagSet("test_validation_failure", pflag.ContinueOnError)
loader, err := confx.Initialize(def, confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"))
require.NoError(t, err)
// Load configuration without specifying a config file path.
config, err := loader(context.Background(), "")
assert.ErrorContains(t, err, `Key: 'TestConfig.LogFiles[1]' Error:Field validation for 'LogFiles[1]' failed on the 'required' tag`)
assert.ErrorContains(t, err, `Key: 'TestConfig.RetryCounts[0]' Error:Field validation for 'RetryCounts[0]' failed on the 'gte' tag`)
assert.ErrorContains(t, err, `Key: 'TestConfig.Timeout' Error:Field validation for 'Timeout' failed on the 'gte' tag`)
assert.ErrorContains(t, err, `Key: 'TestConfig.Database.Host' Error:Field validation for 'Host' failed on the 'required' tag`)
assert.ErrorContains(t, err, `Key: 'TestConfig.Database.Port' Error:Field validation for 'Port' failed on the 'lte' tag`)
assert.ErrorContains(t, err, `Key: 'TestConfig.Extra.BoolSlice' Error:Field validation for 'BoolSlice' failed on the 'min' tag`)
assert.ErrorContains(t, err, `Key: 'TestConfig.Extra.Time' Error:Field validation for 'Time' failed on the 'required' tag`)
assert.Nil(t, config)
}
func TestMapHandling(t *testing.T) {
viper.Reset()
now := time.Now()
def := TestConfig{
Port: 8080,
Database: DatabaseConfig{
Host: "localhost",
Port: 5432,
},
Extra: ExtraConfig{
BoolSlice: []bool{true, false},
StringToString: map[string]string{"key1": "value1"},
StringToInt: map[string]int{"key1": 1},
Time: now,
},
}
flagSet := pflag.NewFlagSet("test_maps", pflag.ContinueOnError)
loader, err := confx.Initialize(def, confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"))
require.NoError(t, err)
// Test command line arguments for maps
args := []string{
"--extra-string-to-string=key2=value2",
"--extra-string-to-string=key3=value3",
"--extra-string-to-int=key2=2,key3=3",
}
err = flagSet.Parse(args)
require.NoError(t, err)
config, err := loader(context.Background(), "")
require.NoError(t, err)
expected := map[string]string{
"key2": "value2",
"key3": "value3",
}
assert.Equal(t, expected, config.Extra.StringToString)
expectedInts := map[string]int{
"key2": 2,
"key3": 3,
}
assert.Equal(t, expectedInts, config.Extra.StringToInt)
}
func TestTimeHandling(t *testing.T) {
viper.Reset()
timeStr := "2023-01-02T15:04:05Z"
expectedTime, err := time.Parse(time.RFC3339, timeStr)
require.NoError(t, err)
def := TestConfig{
Port: 8080,
Database: DatabaseConfig{
Host: "localhost",
Port: 5432,
},
Extra: ExtraConfig{
BoolSlice: []bool{true, false},
StringToString: map[string]string{
"key1": "value1",
},
Time: time.Now(),
},
}
flagSet := pflag.NewFlagSet("test_time", pflag.ContinueOnError)
loader, err := confx.Initialize(def, confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"))
require.NoError(t, err)
// Create config file with time
tempDir := t.TempDir()
configFilePath := filepath.Join(tempDir, "config.yaml")
configFileContent := fmt.Sprintf(`
port: 8080
database:
host: "localhost"
port: 5432
extra:
time: "%s"
`, timeStr)
err = os.WriteFile(configFilePath, []byte(configFileContent), 0o644)
require.NoError(t, err)
config, err := loader(context.Background(), configFilePath)
require.NoError(t, err)
assert.Equal(t, expectedTime.UTC(), config.Extra.Time.UTC())
}
type SquashConfig struct {
Port int `confx:"port" usage:"Port to run the server on" validate:"gte=1,lte=65535"`
Host string `confx:"host" usage:"Host to run the server on" validate:"required"`
Timeout time.Duration
}
type ConfigWithSquash struct {
Base SquashConfig `confx:",squash"`
LogFile string `confx:"logFile" usage:"Log file path"`
}
type InvalidSquashConfig struct {
Time time.Time `confx:",squash"` // Invalid: time.Time
LogFile string `confx:"logFile"`
}
type InvalidSquashConfig2 struct {
IntField int `confx:",squash"` // Invalid: non-struct
LogFile string `confx:"logFile"`
}
func TestSquashTag(t *testing.T) {
t.Run("valid squash", func(t *testing.T) {
viper.Reset()
def := ConfigWithSquash{
Base: SquashConfig{
Port: 8080,
Host: "localhost",
Timeout: 30 * time.Second,
},
LogFile: "/var/log/app.log",
}
flagSet := pflag.NewFlagSet("test_squash", pflag.ContinueOnError)
loader, err := confx.Initialize(def, confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"))
require.NoError(t, err)
// Create a temporary configuration file
tempDir := t.TempDir()
configFilePath := filepath.Join(tempDir, "config.yaml")
configFileContent := `
port: 9090
host: "127.0.0.1"
timeout: "60s"
logFile: "/tmp/app.log"
`
err = os.WriteFile(configFilePath, []byte(configFileContent), 0o644)
require.NoError(t, err)
config, err := loader(context.Background(), configFilePath)
require.NoError(t, err)
expected := ConfigWithSquash{
Base: SquashConfig{
Port: 9090,
Host: "127.0.0.1",
Timeout: 60 * time.Second,
},
LogFile: "/tmp/app.log",
}
assert.Equal(t, expected, config)
})
t.Run("invalid squash time.Time", func(t *testing.T) {
viper.Reset()
def := InvalidSquashConfig{
Time: time.Now(),
LogFile: "/var/log/app.log",
}
flagSet := pflag.NewFlagSet("test_invalid_squash", pflag.ContinueOnError)
_, err := confx.Initialize(def, confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"))
require.Error(t, err)
assert.Contains(t, err.Error(), `unsupported squash type: "time.Time"`)
})
t.Run("invalid squash non-struct", func(t *testing.T) {
viper.Reset()
def := InvalidSquashConfig2{
IntField: 123,
LogFile: "/var/log/app.log",
}
flagSet := pflag.NewFlagSet("test_invalid_squash2", pflag.ContinueOnError)
_, err := confx.Initialize(def, confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"))
require.Error(t, err)
assert.Contains(t, err.Error(), `unsupported squash type: "int"`)
})
}
type Stringer string
func (s Stringer) String() string {
return string(s)
}
func TestUnsupportType(t *testing.T) {
viper.Reset()
loader, err := confx.Initialize[fmt.Stringer](Stringer("a stringer"))
require.ErrorContains(t, err, `unsupported type "confx_test.Stringer" (string)`)
require.Nil(t, loader)
tests := []struct {
name string
def any
wantErr string
}{
{
name: "unsupported chan type",
def: struct {
Chan chan int `confx:"chan"`
}{
Chan: make(chan int),
},
wantErr: `unsupported field type "chan int" (chan) for key "chan"`,
},
{
name: "unsupported func type",
def: struct {
Func func() `confx:"func"`
}{
Func: func() {},
},
wantErr: `unsupported field type "func()" (func) for key "func"`,
},
{
name: "unsupported complex64 type",
def: struct {
Complex complex64 `confx:"complex"`
}{
Complex: complex(1, 2),
},
wantErr: `unsupported field type "complex64" (complex64) for key "complex"`,
},
{
name: "unsupported interface type",
def: struct {
Stringer fmt.Stringer `confx:"stringer"`
}{
Stringer: Stringer("a stringer"),
},
wantErr: `unsupported field type "fmt.Stringer" (interface) for key "stringer"`,
},
{
name: "unsupported slice of func type",
def: struct {
Funcs []func() `confx:"funcs"`
}{
Funcs: []func(){func() {}},
},
wantErr: `flag key "funcs": unsupported slice element type: "func()"`,
},
{
name: "unsupported map key of int type",
def: struct {
IntToString map[int]string `confx:"intToString"`
}{
IntToString: map[int]string{1: "one"},
},
wantErr: `flag key "int-to-string": only string keys are supported for map type, but got "int"`,
},
{
name: "unsupported map value of func type",
def: struct {
StringToFunc map[string]func() `confx:"stringToFunc"`
}{
StringToFunc: map[string]func(){"key": func() {}},
},
wantErr: `flag key "string-to-func": unsupported map value type "func()"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
viper.Reset()
loader, err := confx.Initialize(tt.def)
require.ErrorContains(t, err, tt.wantErr)
require.Nil(t, loader)
})
}
}
func TestUint8Slice(t *testing.T) {
viper.Reset()
def := struct {
Bytes []uint8 `confx:"bytes"` // []uint8 == []byte
}{
Bytes: []uint8("abc"),
}
flagSet := pflag.NewFlagSet("test_uint8_slice", pflag.ContinueOnError)
loader, err := confx.Initialize(def, confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"))
require.NoError(t, err)
{
args := []string{
"--bytes=x,y,z",
}
err = flagSet.Parse(args)
require.ErrorContains(t, err, "illegal base64 data at input byte")
conf, err := loader(context.Background(), "")
require.NoError(t, err)
assert.Equal(t, struct {
Bytes []uint8 `confx:"bytes"`
}{
Bytes: []uint8("abc"),
}, conf)
}
{
args := []string{
"--bytes=" + base64.StdEncoding.EncodeToString([]byte("xyz")),
}
err = flagSet.Parse(args)
require.NoError(t, err)
conf, err := loader(context.Background(), "")
require.NoError(t, err)
assert.Equal(t, struct {
Bytes []uint8 `confx:"bytes"`
}{
Bytes: []uint8("xyz"),
}, conf)
}
}
func TestWithFieldHook(t *testing.T) {
viper.Reset()
def := &TestConfig{
Port: 8080,
LogFiles: []string{"/var/log/app1.log", "/var/log/app2.log"},
RetryCounts: []int{3, 5},
Verbose: true,
MaxIdleConns: 1,
Timeout: 30 * time.Second,
Database: DatabaseConfig{
Host: "localhost",
Port: 5432,
},
Extra: ExtraConfig{
BoolSlice: []bool{true, false},
Time: time.Now(),
},
}
require.Panics(t, func() {
_, _ = confx.Initialize(def, confx.WithFieldHook(nil))
})
{
flagSet := pflag.NewFlagSet("test_field_hook", pflag.ContinueOnError)
loader, err := confx.Initialize(
def,
confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"),
confx.WithFieldHook(func(_ *confx.Field) (*confx.Field, error) {
return nil, fmt.Errorf("hook error")
}),
)
require.ErrorContains(t, err, "hook error")
require.Nil(t, loader)
}
flagSet := pflag.NewFlagSet("test_field_hook", pflag.ContinueOnError)
loader, err := confx.Initialize(
def,
confx.WithFlagSet(flagSet), confx.WithEnvPrefix("APP_"),
confx.WithFieldHook(func(f *confx.Field) (*confx.Field, error) {
if f.ViperKey == "port" {
f.EnvKey = "APP_HTTP_PORT"
}
if f.ViperKey == "MaxIdleConns" {
assert.Equal(t, "max-idle-conns", f.FlagKey)
assert.Equal(t, "APP_MAX_IDLE_CONNS", f.EnvKey)
assert.Equal(t, "Maximum number of idle connections", f.Usage)
// t.Logf("viperKey: %s, flagKey: %s, envKey: %s, usage: %s", f.ViperKey, f.FlagKey, f.EnvKey, f.Usage)
}
return f, nil
}),
)
require.NoError(t, err)
t.Setenv("APP_PORT", "9090")
t.Setenv("APP_HTTP_PORT", "7070")
config, err := loader(context.Background(), "")
require.NoError(t, err)
assert.Equal(t, 7070, config.Port)
}
func TestWithViper(t *testing.T) {
viper.Reset()
type Config struct {
Port int `confx:"port"`
}
def := Config{
Port: 8080,
}
require.Panics(t, func() {
_, _ = confx.Initialize(def, confx.WithViper(nil))
})
v := viper.New()
flagSet := pflag.NewFlagSet("test_with_viper", pflag.ContinueOnError)
loader, err := confx.Initialize(
def,
confx.WithFlagSet(flagSet),
confx.WithEnvPrefix("APP_"),
confx.WithViper(v),
)
require.NoError(t, err)
conf, err := loader(context.Background(), "")
require.NoError(t, err)
require.Equal(t, def, conf)
require.Equal(t, 8080, v.GetInt("port"))
require.NotEqual(t, v, viper.GetInt("port"))
}
func TestWithTagName(t *testing.T) {
viper.Reset()
type Config struct {
Port int `confz:"port"`
}
def := Config{
Port: 8080,
}
require.Panics(t, func() {
_, _ = confx.Initialize(def, confx.WithTagName(""))
})
flagSet := pflag.NewFlagSet("test_with_tag_name", pflag.ContinueOnError)
loader, err := confx.Initialize(
def,
confx.WithFlagSet(flagSet),
confx.WithEnvPrefix("APP_"),
confx.WithTagName("confz"),
)
require.NoError(t, err)
conf, err := loader(context.Background(), "")
require.NoError(t, err)
require.Equal(t, def, conf)
}