@@ -309,8 +309,78 @@ func newConfFromFile(t testing.TB, fileName string) map[string]any {
309
309
}
310
310
311
311
type testConfig struct {
312
- Next * nextConfig `mapstructure:"next"`
313
- Another string `mapstructure:"another"`
312
+ Next * nextConfig `mapstructure:"next"`
313
+ Another string `mapstructure:"another"`
314
+ EmbeddedConfig `mapstructure:",squash"`
315
+ EmbeddedConfig2 `mapstructure:",squash"`
316
+ }
317
+
318
+ type testConfigWithoutUnmarshaler struct {
319
+ Next * nextConfig `mapstructure:"next"`
320
+ Another string `mapstructure:"another"`
321
+ EmbeddedConfig `mapstructure:",squash"`
322
+ EmbeddedConfig2 `mapstructure:",squash"`
323
+ }
324
+
325
+ type testConfigWithEmbeddedError struct {
326
+ Next * nextConfig `mapstructure:"next"`
327
+ Another string `mapstructure:"another"`
328
+ EmbeddedConfigWithError `mapstructure:",squash"`
329
+ }
330
+
331
+ type testConfigWithMarshalError struct {
332
+ Next * nextConfig `mapstructure:"next"`
333
+ Another string `mapstructure:"another"`
334
+ EmbeddedConfigWithMarshalError `mapstructure:",squash"`
335
+ }
336
+
337
+ func (tc * testConfigWithEmbeddedError ) Unmarshal (component * Conf ) error {
338
+ if err := component .Unmarshal (tc , WithIgnoreUnused ()); err != nil {
339
+ return err
340
+ }
341
+ return nil
342
+ }
343
+
344
+ type EmbeddedConfig struct {
345
+ Some string `mapstructure:"some"`
346
+ }
347
+
348
+ func (ec * EmbeddedConfig ) Unmarshal (component * Conf ) error {
349
+ if err := component .Unmarshal (ec , WithIgnoreUnused ()); err != nil {
350
+ return err
351
+ }
352
+ ec .Some += " is also called"
353
+ return nil
354
+ }
355
+
356
+ type EmbeddedConfig2 struct {
357
+ Some2 string `mapstructure:"some_2"`
358
+ }
359
+
360
+ func (ec * EmbeddedConfig2 ) Unmarshal (component * Conf ) error {
361
+ if err := component .Unmarshal (ec , WithIgnoreUnused ()); err != nil {
362
+ return err
363
+ }
364
+ ec .Some2 += " also called2"
365
+ return nil
366
+ }
367
+
368
+ type EmbeddedConfigWithError struct {
369
+ }
370
+
371
+ func (ecwe * EmbeddedConfigWithError ) Unmarshal (_ * Conf ) error {
372
+ return errors .New ("embedded error" )
373
+ }
374
+
375
+ type EmbeddedConfigWithMarshalError struct {
376
+ }
377
+
378
+ func (ecwe EmbeddedConfigWithMarshalError ) Marshal (_ * Conf ) error {
379
+ return errors .New ("marshaling error" )
380
+ }
381
+
382
+ func (ecwe EmbeddedConfigWithMarshalError ) Unmarshal (_ * Conf ) error {
383
+ return nil
314
384
}
315
385
316
386
func (tc * testConfig ) Unmarshal (component * Conf ) error {
@@ -340,12 +410,59 @@ func TestUnmarshaler(t *testing.T) {
340
410
"string" : "make sure this" ,
341
411
},
342
412
"another" : "make sure this" ,
413
+ "some" : "make sure this" ,
414
+ "some_2" : "this better be" ,
343
415
})
344
416
345
417
tc := & testConfig {}
346
418
assert .NoError (t , cfgMap .Unmarshal (tc ))
347
419
assert .Equal (t , "make sure this" , tc .Another )
348
420
assert .Equal (t , "make sure this is called" , tc .Next .String )
421
+ assert .Equal (t , "make sure this is also called" , tc .EmbeddedConfig .Some )
422
+ assert .Equal (t , "this better be also called2" , tc .EmbeddedConfig2 .Some2 )
423
+ }
424
+
425
+ func TestEmbeddedUnmarshaler (t * testing.T ) {
426
+ cfgMap := NewFromStringMap (map [string ]any {
427
+ "next" : map [string ]any {
428
+ "string" : "make sure this" ,
429
+ },
430
+ "another" : "make sure this" ,
431
+ "some" : "make sure this" ,
432
+ "some_2" : "this better be" ,
433
+ })
434
+
435
+ tc := & testConfigWithoutUnmarshaler {}
436
+ assert .NoError (t , cfgMap .Unmarshal (tc ))
437
+ assert .Equal (t , "make sure this" , tc .Another )
438
+ assert .Equal (t , "make sure this is called" , tc .Next .String )
439
+ assert .Equal (t , "make sure this is also called" , tc .EmbeddedConfig .Some )
440
+ assert .Equal (t , "this better be also called2" , tc .EmbeddedConfig2 .Some2 )
441
+ }
442
+
443
+ func TestEmbeddedUnmarshalerError (t * testing.T ) {
444
+ cfgMap := NewFromStringMap (map [string ]any {
445
+ "next" : map [string ]any {
446
+ "string" : "make sure this" ,
447
+ },
448
+ "another" : "make sure this" ,
449
+ "some" : "make sure this" ,
450
+ })
451
+
452
+ tc := & testConfigWithEmbeddedError {}
453
+ assert .EqualError (t , cfgMap .Unmarshal (tc ), "error decoding '': embedded error" )
454
+ }
455
+
456
+ func TestEmbeddedMarshalerError (t * testing.T ) {
457
+ cfgMap := NewFromStringMap (map [string ]any {
458
+ "next" : map [string ]any {
459
+ "string" : "make sure this" ,
460
+ },
461
+ "another" : "make sure this" ,
462
+ })
463
+
464
+ tc := & testConfigWithMarshalError {}
465
+ assert .EqualError (t , cfgMap .Unmarshal (tc ), "error decoding '': error running encode hook: marshaling error" )
349
466
}
350
467
351
468
func TestUnmarshalerKeepAlreadyInitialized (t * testing.T ) {
0 commit comments