@@ -19,6 +19,8 @@ import (
1919 "time"
2020
2121 "github.com/stretchr/testify/assert"
22+ "github.com/stretchr/testify/require"
23+ "golang.org/x/exp/maps"
2224)
2325
2426func TestIsRawPayload (t * testing.T ) {
@@ -111,6 +113,9 @@ func TestMetadataDecode(t *testing.T) {
111113
112114 MyRegularDurationDefaultValueUnset time.Duration `mapstructure:"myregulardurationdefaultvalueunset"`
113115 MyRegularDurationDefaultValueEmpty time.Duration `mapstructure:"myregulardurationdefaultvalueempty"`
116+
117+ AliasedFieldA string `mapstructure:"aliasA1" mapstructurealiases:"aliasA2"`
118+ AliasedFieldB string `mapstructure:"aliasB1" mapstructurealiases:"aliasB2"`
114119 }
115120
116121 var m testMetadata
@@ -131,6 +136,9 @@ func TestMetadataDecode(t *testing.T) {
131136 "mydurationarray" : "1s,2s,3s,10" ,
132137 "mydurationarraypointer" : "1s,10,2s,20,3s,30" ,
133138 "mydurationarraypointerempty" : "," ,
139+ "aliasA2" : "hello" ,
140+ "aliasB1" : "ciao" ,
141+ "aliasB2" : "bonjour" ,
134142 }
135143
136144 err := DecodeMetadata (testData , & m )
@@ -149,6 +157,8 @@ func TestMetadataDecode(t *testing.T) {
149157 assert .Equal (t , []time.Duration {time .Second , time .Second * 2 , time .Second * 3 , time .Second * 10 }, m .MyDurationArray )
150158 assert .Equal (t , []time.Duration {time .Second , time .Second * 10 , time .Second * 2 , time .Second * 20 , time .Second * 3 , time .Second * 30 }, * m .MyDurationArrayPointer )
151159 assert .Equal (t , []time.Duration {}, * m .MyDurationArrayPointerEmpty )
160+ assert .Equal (t , "hello" , m .AliasedFieldA )
161+ assert .Equal (t , "ciao" , m .AliasedFieldB )
152162 })
153163
154164 t .Run ("Test metadata decode hook for truthy values" , func (t * testing.T ) {
@@ -303,3 +313,172 @@ func TestMetadataStructToStringMap(t *testing.T) {
303313 assert .Empty (t , metadatainfo ["ignored" ].Aliases )
304314 })
305315}
316+
317+ func TestResolveAliases (t * testing.T ) {
318+ tests := []struct {
319+ name string
320+ md map [string ]string
321+ result any
322+ wantErr bool
323+ wantMd map [string ]string
324+ }{
325+ {
326+ name : "no aliases" ,
327+ md : map [string ]string {
328+ "hello" : "world" ,
329+ "ciao" : "mondo" ,
330+ },
331+ result : & struct {
332+ Hello string `mapstructure:"hello"`
333+ Ciao string `mapstructure:"ciao"`
334+ Bonjour string `mapstructure:"bonjour"`
335+ }{},
336+ wantMd : map [string ]string {
337+ "hello" : "world" ,
338+ "ciao" : "mondo" ,
339+ },
340+ },
341+ {
342+ name : "set with aliased field" ,
343+ md : map [string ]string {
344+ "ciao" : "mondo" ,
345+ },
346+ result : & struct {
347+ Hello string `mapstructure:"hello" mapstructurealiases:"ciao"`
348+ Bonjour string `mapstructure:"bonjour"`
349+ }{},
350+ wantMd : map [string ]string {
351+ "hello" : "mondo" ,
352+ "ciao" : "mondo" ,
353+ },
354+ },
355+ {
356+ name : "do not overwrite existing fields with aliases" ,
357+ md : map [string ]string {
358+ "hello" : "world" ,
359+ "ciao" : "mondo" ,
360+ },
361+ result : & struct {
362+ Hello string `mapstructure:"hello" mapstructurealiases:"ciao"`
363+ Bonjour string `mapstructure:"bonjour"`
364+ }{},
365+ wantMd : map [string ]string {
366+ "hello" : "world" ,
367+ "ciao" : "mondo" ,
368+ },
369+ },
370+ {
371+ name : "no fields with aliased value" ,
372+ md : map [string ]string {
373+ "bonjour" : "monde" ,
374+ },
375+ result : & struct {
376+ Hello string `mapstructure:"hello" mapstructurealiases:"ciao"`
377+ Bonjour string `mapstructure:"bonjour"`
378+ }{},
379+ wantMd : map [string ]string {
380+ "bonjour" : "monde" ,
381+ },
382+ },
383+ {
384+ name : "multiple aliases" ,
385+ md : map [string ]string {
386+ "bonjour" : "monde" ,
387+ },
388+ result : & struct {
389+ Hello string `mapstructure:"hello" mapstructurealiases:"ciao,bonjour"`
390+ }{},
391+ wantMd : map [string ]string {
392+ "hello" : "monde" ,
393+ "bonjour" : "monde" ,
394+ },
395+ },
396+ {
397+ name : "first alias wins" ,
398+ md : map [string ]string {
399+ "ciao" : "mondo" ,
400+ "bonjour" : "monde" ,
401+ },
402+ result : & struct {
403+ Hello string `mapstructure:"hello" mapstructurealiases:"ciao,bonjour"`
404+ }{},
405+ wantMd : map [string ]string {
406+ "hello" : "mondo" ,
407+ "ciao" : "mondo" ,
408+ "bonjour" : "monde" ,
409+ },
410+ },
411+ {
412+ name : "no aliases with mixed case" ,
413+ md : map [string ]string {
414+ "hello" : "world" ,
415+ "CIAO" : "mondo" ,
416+ },
417+ result : & struct {
418+ Hello string `mapstructure:"Hello"`
419+ Ciao string `mapstructure:"ciao"`
420+ Bonjour string `mapstructure:"bonjour"`
421+ }{},
422+ wantMd : map [string ]string {
423+ "hello" : "world" ,
424+ "CIAO" : "mondo" ,
425+ },
426+ },
427+ {
428+ name : "set with aliased field with mixed case" ,
429+ md : map [string ]string {
430+ "ciao" : "mondo" ,
431+ },
432+ result : & struct {
433+ Hello string `mapstructure:"Hello" mapstructurealiases:"CIAO"`
434+ Bonjour string `mapstructure:"bonjour"`
435+ }{},
436+ wantMd : map [string ]string {
437+ "Hello" : "mondo" ,
438+ "ciao" : "mondo" ,
439+ },
440+ },
441+ {
442+ name : "do not overwrite existing fields with aliases with mixed cases" ,
443+ md : map [string ]string {
444+ "HELLO" : "world" ,
445+ "CIAO" : "mondo" ,
446+ },
447+ result : & struct {
448+ Hello string `mapstructure:"hELLo" mapstructurealiases:"cIAo"`
449+ Bonjour string `mapstructure:"bonjour"`
450+ }{},
451+ wantMd : map [string ]string {
452+ "HELLO" : "world" ,
453+ "CIAO" : "mondo" ,
454+ },
455+ },
456+ {
457+ name : "multiple aliases with mixed cases" ,
458+ md : map [string ]string {
459+ "bonjour" : "monde" ,
460+ },
461+ result : & struct {
462+ Hello string `mapstructure:"HELLO" mapstructurealiases:"CIAO,BONJOUR"`
463+ }{},
464+ wantMd : map [string ]string {
465+ "HELLO" : "monde" ,
466+ "bonjour" : "monde" ,
467+ },
468+ },
469+ }
470+ for _ , tt := range tests {
471+ t .Run (tt .name , func (t * testing.T ) {
472+ md := maps .Clone (tt .md )
473+ err := resolveAliases (md , tt .result )
474+
475+ if tt .wantErr {
476+ require .Error (t , err )
477+ return
478+ }
479+
480+ require .NoError (t , err )
481+ require .Equal (t , tt .wantMd , md )
482+ })
483+ }
484+ }
0 commit comments