@@ -10,9 +10,10 @@ type testOptionsJSONSerialization struct {
1010 StringPointer * string `json:"stringPointer"`
1111 NormalString string `json:"normalString"`
1212 WithoutJSONTag string
13- WithJSONTag string `json:"withJSONTag"`
14- SkipNilPtrs * string `json:"skipNilPtrs"`
15- SkipMe * int `json:"skipMe"`
13+ WithJSONTag string `json:"withJSONTag"`
14+ OverrideMe []string `json:"overrideMe"`
15+ SkipNilPtrs * string `json:"skipNilPtrs"`
16+ SkipMe * int `json:"skipMe"`
1617}
1718
1819func TestTransformOptions (t * testing.T ) {
@@ -22,17 +23,18 @@ func TestTransformOptions(t *testing.T) {
2223 NormalString : "2" ,
2324 WithoutJSONTag : "3" ,
2425 WithJSONTag : "4" ,
26+ OverrideMe : []string {"5" },
2527 }
2628 var nilStrPtr * string
2729 testCases := []struct {
28- name string
29- baseMap map [ string ] interface {}
30- optionalStruct interface {}
31- expected interface {}
30+ name string
31+ firstOption interface {}
32+ secondOption interface {}
33+ expected interface {}
3234 }{
3335 {
3436 name : "No options supplied" ,
35- baseMap : map [string ]interface {}{
37+ firstOption : map [string ]interface {}{
3638 "1234" : nilStrPtr ,
3739 "foo" : "bar" ,
3840 },
@@ -42,44 +44,60 @@ func TestTransformOptions(t *testing.T) {
4244 },
4345 {
4446 name : "Options are nil" ,
45- baseMap : map [string ]interface {}{
47+ firstOption : map [string ]interface {}{
4648 "foo" : "bar" ,
4749 },
48- optionalStruct : nil ,
50+ secondOption : nil ,
4951 expected : map [string ]interface {}{
5052 "foo" : "bar" ,
5153 },
5254 },
5355 {
5456 name : "JSON serialization works" ,
55- baseMap : map [string ]interface {}{
56- "foo" : "bar" ,
57+ firstOption : map [string ]interface {}{
58+ "foo" : "bar" ,
59+ "stringPointer" : 1 ,
5760 },
58- optionalStruct : structVar ,
61+ secondOption : structVar ,
5962 expected : map [string ]interface {}{
6063 "foo" : "bar" ,
6164 "stringPointer" : String ("1" ),
6265 "normalString" : "2" ,
6366 "WithoutJSONTag" : "3" ,
6467 "withJSONTag" : "4" ,
68+ "overrideMe" : []interface {}{"5" },
6569 },
6670 },
6771 {
6872 name : "Second overwrites the first one" ,
69- baseMap : map [string ]interface {}{
73+ firstOption : map [string ]interface {}{
7074 "foo" : "1" ,
7175 },
72- optionalStruct : map [string ]interface {}{
76+ secondOption : map [string ]interface {}{
7377 "foo" : "2" ,
7478 },
7579 expected : map [string ]interface {}{
7680 "foo" : "2" ,
7781 },
7882 },
83+ {
84+ name : "Second overwrites the first one's value in different type" ,
85+ firstOption : structVar ,
86+ secondOption : map [string ]interface {}{
87+ "overrideMe" : "5" ,
88+ },
89+ expected : map [string ]interface {}{
90+ "stringPointer" : String ("1" ),
91+ "normalString" : "2" ,
92+ "WithoutJSONTag" : "3" ,
93+ "withJSONTag" : "4" ,
94+ "overrideMe" : "5" ,
95+ },
96+ },
7997 }
8098 for _ , tc := range testCases {
8199 t .Run (tc .name , func (t * testing.T ) {
82- require .Equal (t , tc .expected , transformOptions (tc .baseMap , tc .optionalStruct ))
100+ require .Equal (t , tc .expected , transformOptions (tc .firstOption , tc .secondOption ))
83101 })
84102 }
85103}
0 commit comments