-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunmarshal_test.go
779 lines (631 loc) · 18.8 KB
/
unmarshal_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
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
package env
import (
"os"
"reflect"
"strings"
"testing"
)
type Config struct {
StringField string `env:"STRING_FIELD,default=default_value"`
IntField int `env:"INT_FIELD,default=123"`
BoolField bool `env:"BOOL_FIELD,default=true"`
FloatField float64 `env:"FLOAT_FIELD,default=1.23"`
StringSliceField []string `env:"SLICE_FIELD,default=item1"`
StringSliceFieldMultiple []string `env:"SLICE_FIELD_MULTI,default=[item1,item2,item3]"`
}
type NestedConfig struct {
NestedField string `env:"NESTED_FIELD,default=nested"`
}
type ParentConfig struct {
Config
Nested NestedConfig `env:"NESTED"`
}
func TestUnmarshal(t *testing.T) {
setEnvForTest(t, "STRING_FIELD", "string_value")
setEnvForTest(t, "INT_FIELD", "456")
setEnvForTest(t, "BOOL_FIELD", "false")
setEnvForTest(t, "FLOAT_FIELD", "4.56")
setEnvForTest(t, "SLICE_FIELD", "item1,item2")
setEnvForTest(t, "SLICE_FIELD_MULTI", "item1,item2,item3,item4")
var cfg Config
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal")
expected := Config{
StringField: "string_value",
IntField: 456,
BoolField: false,
FloatField: 4.56,
StringSliceField: []string{"item1", "item2"},
StringSliceFieldMultiple: []string{"item1", "item2", "item3", "item4"},
}
assertEqual(t, expected, cfg, "Unmarshal")
}
func TestUnmarshalWithDefaults(t *testing.T) {
var cfg Config
err := Unmarshal(&cfg)
assertNoError(t, err)
assertEqual(t, "default_value", cfg.StringField)
assertEqual(t, 123, cfg.IntField)
assertEqual(t, true, cfg.BoolField)
assertEqual(t, 1.23, cfg.FloatField)
assertEqual(t, []string{"item1"}, cfg.StringSliceField)
assertEqual(t, []string{"item1", "item2", "item3"}, cfg.StringSliceFieldMultiple)
}
func TestUnmarshalDefaultsFromCode(t *testing.T) {
type Config struct {
Host string `env:"HOST,default=localhost"`
Port int `env:"PORT"`
}
setEnvForTest(t, "HOST", "envhost")
setEnvForTest(t, "PORT", "8080")
cfg := &Config{
Host: "syntaqx.com",
Port: 3306,
}
err := Unmarshal(cfg)
assertNoError(t, err, "Unmarshal")
expected := Config{
Host: "envhost",
Port: 8080,
}
assertEqual(t, expected, *cfg, "UnmarshalDefaultsFromCode")
}
func TestUnmarshalDefaultsFromTags(t *testing.T) {
type Config struct {
Host string `env:"HOST,default=localhost"`
Port int `env:"PORT"`
}
cfg := &Config{}
err := Unmarshal(cfg)
assertNoError(t, err, "Unmarshal")
expected := Config{
Host: "localhost",
Port: 0,
}
assertEqual(t, expected, *cfg, "UnmarshalDefaultsFromTags")
}
func TestUnmarshalDefaultsFromCodeAndTags(t *testing.T) {
type Config struct {
Host string `env:"HOST,default=localhost"`
Port int `env:"PORT"`
}
cfg := &Config{
Host: "syntaqx.com",
Port: 3306,
}
err := Unmarshal(cfg)
assertNoError(t, err, "Unmarshal")
expected := Config{
Host: "localhost",
Port: 3306,
}
assertEqual(t, expected, *cfg, "UnmarshalDefaultsFromCodeAndTags")
}
func TestUnmarshalNested(t *testing.T) {
setEnvForTest(t, "STRING_FIELD", "string_value")
setEnvForTest(t, "INT_FIELD", "456")
setEnvForTest(t, "BOOL_FIELD", "false")
setEnvForTest(t, "FLOAT_FIELD", "4.56")
setEnvForTest(t, "SLICE_FIELD", "item1,item2")
setEnvForTest(t, "SLICE_FIELD_MULTI", "item1,item2,item3,item4")
setEnvForTest(t, "NESTED_NESTED_FIELD", "nested_value")
var cfg ParentConfig
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal")
expected := ParentConfig{
Config: Config{
StringField: "string_value",
IntField: 456,
BoolField: false,
FloatField: 4.56,
StringSliceField: []string{"item1", "item2"},
StringSliceFieldMultiple: []string{"item1", "item2", "item3", "item4"},
},
Nested: NestedConfig{
NestedField: "nested_value",
},
}
assertEqual(t, expected, cfg, "UnmarshalNested")
}
func TestUnmarshalUntaggedFields(t *testing.T) {
type UntaggedFieldsConfig struct {
UntaggedField1 string
UntaggedField2 int
TaggedField string `env:"TAGGED_FIELD,default=default_value"`
}
setEnvForTest(t, "TAGGED_FIELD", "value")
var cfg UntaggedFieldsConfig
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal")
expected := UntaggedFieldsConfig{
UntaggedField1: "",
UntaggedField2: 0,
TaggedField: "value",
}
assertEqual(t, expected, cfg, "UnmarshalUntaggedFields")
}
func TestUnmarshalFloat(t *testing.T) {
setEnvForTest(t, "FLOAT32_VALUE", "3.14")
setEnvForTest(t, "FLOAT64_VALUE", "3.14159")
var cfg struct {
Float32Value float32 `env:"FLOAT32_VALUE"`
Float64Value float64 `env:"FLOAT64_VALUE"`
}
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal FloatConfig")
expected := struct {
Float32Value float32
Float64Value float64
}{
Float32Value: 3.14,
Float64Value: 3.14159,
}
assertEqual(t, expected.Float32Value, cfg.Float32Value, "Float32Value")
assertEqual(t, expected.Float64Value, cfg.Float64Value, "Float64Value")
}
func TestUnmarshalUnsupportedKind(t *testing.T) {
setEnvForTest(t, "UNSUPPORTED", "invalid")
var cfg struct {
Unsupported complex64 `env:"UNSUPPORTED"`
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal Unsupported kind")
}
func TestUnmarshalSetFieldIntError(t *testing.T) {
setEnvForTest(t, "INVALID_INT", "invalid")
var cfg struct {
InvalidInt int `env:"INVALID_INT"`
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal InvalidInt")
}
func TestUnmarshalSetFieldFloatError(t *testing.T) {
setEnvForTest(t, "INVALID_FLOAT", "invalid")
var cfg struct {
InvalidFloat float64 `env:"INVALID_FLOAT"`
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal InvalidFloat")
}
func TestUnmarshalSetFieldNestedError(t *testing.T) {
type NestedConfig struct {
NestedField string `env:"NESTED_FIELD,required"`
}
setEnvForTest(t, "NESTED_FIELD", "") // Setting an empty value to trigger required error
var cfg struct {
Nested NestedConfig
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal NestedConfig")
}
func TestUnmarshalRequired(t *testing.T) {
type RequiredConfig struct {
RequiredVar string `env:"REQUIRED_VAR,required"`
}
var cfg RequiredConfig
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal Required")
expectedErr := "required environment variable REQUIRED_VAR is not set"
if err != nil && err.Error() != expectedErr {
t.Errorf("expected error %s, got %s", expectedErr, err.Error())
}
setEnvForTest(t, "REQUIRED_VAR", "value")
err = Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal Required")
assertEqual(t, "value", cfg.RequiredVar, "Unmarshal Required")
}
func TestUnmarshalSetFieldErrors(t *testing.T) {
tests := []struct {
envKey string
envValue string
fieldType string
}{
{"INVALID_UINT", "invalid", "uint"},
{"INVALID_FLOAT", "invalid", "float32"},
{"UNSUPPORTED", "invalid", "complex64"},
}
for _, tt := range tests {
t.Run(tt.fieldType, func(t *testing.T) {
setEnvForTest(t, tt.envKey, tt.envValue)
var cfg struct {
InvalidUint uint `env:"INVALID_UINT"`
InvalidFloat float32 `env:"INVALID_FLOAT"`
Unsupported complex64 `env:"UNSUPPORTED"`
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal "+tt.fieldType)
})
}
}
func TestSetFieldUint(t *testing.T) {
type Config struct {
UIntField uint `env:"UINT_FIELD"`
}
var cfg Config
err := Set("UINT_FIELD", "42")
assertNoError(t, err, "Set UINT_FIELD")
field := reflect.ValueOf(&cfg).Elem().FieldByName("UIntField")
err = setField(field, "42")
assertNoError(t, err, "setField Uint")
assertEqual(t, uint(42), cfg.UIntField, "UintField value")
}
func TestSetFieldEmptyValue(t *testing.T) {
type Config struct {
StringField string `env:"STRING_FIELD"`
IntField int `env:"INT_FIELD"`
BoolField bool `env:"BOOL_FIELD"`
}
var cfg Config
field := reflect.ValueOf(&cfg).Elem().FieldByName("StringField")
err := setField(field, "")
assertNoError(t, err, "setField empty string")
assertEqual(t, "", cfg.StringField, "StringField")
field = reflect.ValueOf(&cfg).Elem().FieldByName("IntField")
err = setField(field, "")
assertNoError(t, err, "setField empty int")
assertEqual(t, 0, cfg.IntField, "IntField")
field = reflect.ValueOf(&cfg).Elem().FieldByName("BoolField")
err = setField(field, "")
assertNoError(t, err, "setField empty bool")
assertEqual(t, false, cfg.BoolField, "BoolField")
}
func TestSetFieldBoolError(t *testing.T) {
type Config struct {
BoolField bool `env:"BOOL_FIELD"`
}
var cfg Config
err := Set("BOOL_FIELD", "invalid")
assertNoError(t, err, "Set BOOL_FIELD")
field := reflect.ValueOf(&cfg).Elem().FieldByName("BoolField")
err = setField(field, "invalid")
assertError(t, err, "setField Bool")
}
func TestUnsupportedSliceElementKind(t *testing.T) {
type Config struct {
Unsupported []complex64 `env:"UNSUPPORTED"`
}
var cfg Config
err := Set("UNSUPPORTED", "invalid")
assertNoError(t, err, "Set UNSUPPORTED")
field := reflect.ValueOf(&cfg).Elem().FieldByName("Unsupported")
err = setField(field, "invalid")
assertError(t, err, "setField Unsupported")
}
func TestUnmarshalFallback(t *testing.T) {
type FallbackConfig struct {
FallbackField string `env:"FALLBACK_FIELD,fallback=fallback_value"`
}
var cfg FallbackConfig
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal Fallback")
expected := FallbackConfig{
FallbackField: "fallback_value",
}
assertEqual(t, expected, cfg, "UnmarshalFallback")
}
func TestUnmarshalSliceBool(t *testing.T) {
setEnvForTest(t, "SLICE_BOOL", "true,false,true")
var cfg struct {
SliceBool []bool `env:"SLICE_BOOL"`
}
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal SliceBool")
expected := struct {
SliceBool []bool
}{
SliceBool: []bool{true, false, true},
}
assertEqual(t, expected.SliceBool, cfg.SliceBool, "SliceBool")
}
func TestUnmarshalSliceInt(t *testing.T) {
setEnvForTest(t, "SLICE_INT", "1,2,3")
var cfg struct {
SliceInt []int `env:"SLICE_INT"`
}
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal SliceInt")
expected := struct {
SliceInt []int
}{
SliceInt: []int{1, 2, 3},
}
assertEqual(t, expected.SliceInt, cfg.SliceInt, "SliceInt")
}
func TestUnmarshalSliceUint(t *testing.T) {
setEnvForTest(t, "SLICE_UINT", "1,2,3")
var cfg struct {
SliceUint []uint `env:"SLICE_UINT"`
}
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal SliceUint")
expected := struct {
SliceUint []uint
}{
SliceUint: []uint{1, 2, 3},
}
assertEqual(t, expected.SliceUint, cfg.SliceUint, "SliceUint")
}
func TestUnmarshalSliceFloat(t *testing.T) {
setEnvForTest(t, "SLICE_FLOAT", "1.1,2.2,3.3")
var cfg struct {
SliceFloat []float64 `env:"SLICE_FLOAT"`
}
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal SliceFloat")
expected := struct {
SliceFloat []float64
}{
SliceFloat: []float64{1.1, 2.2, 3.3},
}
assertEqual(t, expected.SliceFloat, cfg.SliceFloat, "SliceFloat")
}
func TestUnmarshalSliceBoolError(t *testing.T) {
setEnvForTest(t, "SLICE_BOOL_ERROR", "true,notabool,true")
var cfg struct {
SliceBool []bool `env:"SLICE_BOOL_ERROR"`
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal SliceBoolError")
}
func TestUnmarshalSliceIntError(t *testing.T) {
setEnvForTest(t, "SLICE_INT_ERROR", "1,two,3")
var cfg struct {
SliceInt []int `env:"SLICE_INT_ERROR"`
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal SliceIntError")
}
func TestUnmarshalSliceUintError(t *testing.T) {
setEnvForTest(t, "SLICE_UINT_ERROR", "1,-2,3")
var cfg struct {
SliceUint []uint `env:"SLICE_UINT_ERROR"`
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal SliceUintError")
}
func TestUnmarshalSliceFloatError(t *testing.T) {
setEnvForTest(t, "SLICE_FLOAT_ERROR", "1.1,two.two,3.3")
var cfg struct {
SliceFloat []float64 `env:"SLICE_FLOAT_ERROR"`
}
err := Unmarshal(&cfg)
assertError(t, err, "Unmarshal SliceFloatError")
}
func TestParseTag(t *testing.T) {
type TestCase struct {
Tag string
ExpectedOpts tagOptions
}
testCases := []TestCase{
{
Tag: "NOT_REQUIRED,default=required",
ExpectedOpts: tagOptions{
keys: []string{"NOT_REQUIRED"},
fallback: "required",
required: false,
},
},
{
Tag: "REQUIRED,required",
ExpectedOpts: tagOptions{
keys: []string{"REQUIRED"},
fallback: "",
required: true,
},
},
{
Tag: "REQUIRED_WITH_DEFAULT,default=default,required",
ExpectedOpts: tagOptions{
keys: []string{"REQUIRED_WITH_DEFAULT"},
fallback: "default",
required: true,
},
},
{
Tag: "SINGLE_KEY,required,default=default",
ExpectedOpts: tagOptions{
keys: []string{"SINGLE_KEY"},
fallback: "default",
required: true,
},
},
{
Tag: "MULTI_KEY1|MULTI_KEY2|MULTI_KEY3,required,default=default",
ExpectedOpts: tagOptions{
keys: []string{"MULTI_KEY1", "MULTI_KEY2", "MULTI_KEY3"},
fallback: "default",
required: true,
},
},
{
Tag: "SQUARE_BRACKETS,default=[item1,item2,item3]",
ExpectedOpts: tagOptions{
keys: []string{"SQUARE_BRACKETS"},
fallback: "item1,item2,item3",
required: false,
},
},
{
Tag: "SQUARE_BRACKETS,default=[item1,item2,item3],required",
ExpectedOpts: tagOptions{
keys: []string{"SQUARE_BRACKETS"},
fallback: "item1,item2,item3",
required: true,
},
},
}
for _, tc := range testCases {
t.Run(tc.Tag, func(t *testing.T) {
opts := parseTag(tc.Tag)
if !reflect.DeepEqual(opts, tc.ExpectedOpts) {
t.Errorf("parseTag(%s) returned %+v, expected %+v", tc.Tag, opts, tc.ExpectedOpts)
}
})
}
}
func TestIsZeroValue(t *testing.T) {
tests := []struct {
name string
field reflect.Value
want bool
}{
{"ZeroString", reflect.ValueOf(""), true},
{"NonZeroString", reflect.ValueOf("non-zero"), false},
{"ZeroInt", reflect.ValueOf(0), true},
{"NonZeroInt", reflect.ValueOf(123), false},
{"ZeroBool", reflect.ValueOf(false), true},
{"NonZeroBool", reflect.ValueOf(true), false},
{"ZeroFloat", reflect.ValueOf(0.0), true},
{"NonZeroFloat", reflect.ValueOf(1.23), false},
{"ZeroSlice", reflect.ValueOf([]string(nil)), true},
{"NonZeroSlice", reflect.ValueOf([]string{"item1"}), false},
{"InvalidValue", reflect.Value{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isZeroValue(tt.field); got != tt.want {
t.Errorf("isZeroValue() = %v, want %v", got, tt.want)
}
})
}
}
func TestUnmarshalFileOption(t *testing.T) {
type Config struct {
Host string `env:"HOST,default=localhost"`
Port int `env:"PORT"`
Key string `env:"KEY,file"`
}
// Create a temporary file with the content
fileContent := "file_content"
tmpFile, err := os.CreateTemp("", "example")
assertNoError(t, err, "CreateTemp")
defer os.Remove(tmpFile.Name())
_, err = tmpFile.WriteString(fileContent)
assertNoError(t, err, "WriteString")
err = tmpFile.Close()
assertNoError(t, err, "Close")
setEnvForTest(t, "HOST", "envhost")
setEnvForTest(t, "PORT", "8080")
setEnvForTest(t, "KEY", tmpFile.Name())
cfg := &Config{
Host: "syntaqx.com",
Port: 3306,
}
err = Unmarshal(cfg)
assertNoError(t, err, "Unmarshal")
expected := Config{
Host: "envhost",
Port: 8080,
Key: fileContent,
}
assertEqual(t, expected, *cfg, "UnmarshalFileOption")
}
func TestUnmarshalFieldFileError(t *testing.T) {
type Config struct {
Key string `env:"KEY,file"`
}
// Set an invalid file path
setEnvForTest(t, "KEY", "/invalid/path/to/file")
var cfg Config
err := Unmarshal(&cfg)
assertError(t, err, "UnmarshalFieldFileError")
expectedErrPrefix := "open /invalid/path/to/file"
if err != nil && !strings.HasPrefix(err.Error(), expectedErrPrefix) {
t.Errorf("expected error to start with %s, got %s", expectedErrPrefix, err.Error())
}
}
func TestReadFileContentError(t *testing.T) {
_, err := readFileContent("/invalid/path/to/file")
assertError(t, err, "readFileContentError")
expectedErrPrefix := "open /invalid/path/to/file"
if err != nil && !strings.HasPrefix(err.Error(), expectedErrPrefix) {
t.Errorf("expected error to start with %s, got %s", expectedErrPrefix, err.Error())
}
}
func TestUnmarshalExpand(t *testing.T) {
setEnvForTest(t, "HOST", "localhost")
setEnvForTest(t, "PORT", "8080")
setEnvForTest(t, "BASE_URL", "http://${HOST}:${PORT}")
type Config struct {
BaseURL string `env:"BASE_URL,expand"`
}
var cfg Config
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal with expand")
expected := Config{
BaseURL: "http://localhost:8080",
}
assertEqual(t, expected, cfg, "UnmarshalExpand")
}
func TestUnmarshalExpandWithDefault(t *testing.T) {
setEnvForTest(t, "HOST", "localhost")
setEnvForTest(t, "PORT", "8080")
type Config struct {
BaseURL string `env:"BASE_URL,default=http://${HOST}:${PORT}/api,expand"`
}
var cfg Config
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal with expand and default")
expected := Config{
BaseURL: "http://localhost:8080/api",
}
assertEqual(t, expected, cfg, "UnmarshalExpandWithDefault")
}
func TestUnmarshalExpandWithMissingEnv(t *testing.T) {
type Config struct {
BaseURL string `env:"BASE_URL,default=http://${HOST}:${PORT}/api,expand"`
}
var cfg Config
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal with expand and missing env variables")
expected := Config{
BaseURL: "http://:/api", // Expanded with empty strings for missing HOST and PORT
}
assertEqual(t, expected, cfg, "UnmarshalExpandWithMissingEnv")
}
func TestGetDefaultFromStructWithFallback(t *testing.T) {
type Config struct {
Host string `env:"HOST,default=localhost"`
Port string `env:"PORT,default=8080"`
Address string `env:"ADDRESS,default=${HOST}:${PORT},expand"`
}
var cfg Config
defaultHost := getDefaultFromStruct("HOST", &cfg)
defaultPort := getDefaultFromStruct("PORT", &cfg)
if defaultHost != "localhost" {
t.Errorf("expected default host to be 'localhost', got '%s'", defaultHost)
}
if defaultPort != "8080" {
t.Errorf("expected default port to be '8080', got '%s'", defaultPort)
}
}
func TestGetDefaultFromStructWithNestedStruct(t *testing.T) {
type NestedConfig struct {
NestedField string `env:"NESTED_FIELD,default=nested_default"`
}
type Config struct {
Host string `env:"HOST,default=localhost"`
Nested NestedConfig `env:"NESTED"`
}
var cfg Config
defaultNestedField := getDefaultFromStruct("NESTED_FIELD", &cfg)
if defaultNestedField != "nested_default" {
t.Errorf("expected default nested field to be 'nested_default', got '%s'", defaultNestedField)
}
}
func TestExpandVariables(t *testing.T) {
setEnvForTest(t, "HOST", "localhost")
setEnvForTest(t, "PORT", "8080")
type Config struct {
BaseURL1 string `env:"BASE_URL1,default=http://${HOST}:${PORT}/api,expand"`
BaseURL2 string `env:"BASE_URL2,default=http://$HOST:$PORT/api,expand"`
}
var cfg Config
err := Unmarshal(&cfg)
assertNoError(t, err, "Unmarshal with expand and default")
expected := Config{
BaseURL1: "http://localhost:8080/api",
BaseURL2: "http://localhost:8080/api",
}
assertEqual(t, expected, cfg, "ExpandVariables")
}