@@ -35,20 +35,25 @@ import (
3535)
3636
3737type testPlainObj struct {
38- AaAa string `m :"aaAa"`
38+ AaAa string `mapstructure :"aaAa"`
3939 BaBa string
4040 CaCa struct {
4141 AaAa string
42- BaBa string `m :"baBa"`
42+ BaBa string `mapstructure :"baBa"`
4343 XxYy struct {
44- xxXx string `m :"xxXx"`
45- Xx string `m :"xx"`
46- } `m :"xxYy"`
47- } `m :"caCa"`
44+ xxXx string `mapstructure :"xxXx"`
45+ Xx string `mapstructure :"xx"`
46+ } `mapstructure :"xxYy"`
47+ } `mapstructure :"caCa"`
4848 DaDa time.Time
4949 EeEe int
5050}
5151
52+ type testMapstructureTagObj struct {
53+ UserID string `mapstructure:"user_id"`
54+ Name string
55+ }
56+
5257func TestObjToMap (t * testing.T ) {
5358 obj := & testPlainObj {}
5459 obj .AaAa = "1"
@@ -71,20 +76,37 @@ func TestObjToMap(t *testing.T) {
7176 assert .Equal (t , 100 , m ["eeEe" ].(int ))
7277}
7378
79+ func TestMapstructureTagRoundTrip (t * testing.T ) {
80+ original := testMapstructureTagObj {
81+ UserID : "42" ,
82+ Name : "alice" ,
83+ }
84+
85+ generalized , err := mockMapGeneralizer .Generalize (original )
86+ require .NoError (t , err )
87+ generalizedMap , ok := generalized .(map [string ]any )
88+ require .True (t , ok )
89+ assert .Equal (t , "42" , generalizedMap ["user_id" ])
90+
91+ realized , err := mockMapGeneralizer .Realize (generalized , reflect .TypeOf (original ))
92+ require .NoError (t , err )
93+ assert .Equal (t , original , realized )
94+ }
95+
7496type testStruct struct {
7597 AaAa string
76- BaBa string `m :"baBa"`
98+ BaBa string `mapstructure :"baBa"`
7799 XxYy struct {
78- xxXx string `m :"xxXx"`
79- Xx string `m :"xx"`
80- } `m :"xxYy"`
100+ xxXx string `mapstructure :"xxXx"`
101+ Xx string `mapstructure :"xx"`
102+ } `mapstructure :"xxYy"`
81103}
82104
83105func TestObjToMap_Slice (t * testing.T ) {
84106 var testData struct {
85- AaAa string `m :"aaAa"`
107+ AaAa string `mapstructure :"aaAa"`
86108 BaBa string
87- CaCa []testStruct `m :"caCa"`
109+ CaCa []testStruct `mapstructure :"caCa"`
88110 }
89111 testData .AaAa = "1"
90112 testData .BaBa = "1"
0 commit comments