File tree Expand file tree Collapse file tree 2 files changed +45
-4
lines changed Expand file tree Collapse file tree 2 files changed +45
-4
lines changed Original file line number Diff line number Diff line change @@ -55,8 +55,10 @@ func getMapOfAllKeyValues(s interface{}) *map[string]interface{} {
55
55
// only check if the value can be obtained without panicking (eg: for unexported fields)
56
56
if t .Field (i ).CanInterface () {
57
57
qVars := getMapOfAllKeyValues (t .Field (i ).Interface ()) //recursive call
58
- for k , v := range * qVars {
59
- vars [k ] = v
58
+ if qVars != nil {
59
+ for k , v := range * qVars {
60
+ vars [k ] = v
61
+ }
60
62
}
61
63
}
62
64
} else {
@@ -68,8 +70,10 @@ func getMapOfAllKeyValues(s interface{}) *map[string]interface{} {
68
70
// only check if the value can be obtained without panicking (eg: for unexported fields)
69
71
if t .Field (i ).CanInterface () {
70
72
qVars := getMapOfAllKeyValues (t .Field (i ).Interface ()) //recursive call
71
- for k , v := range * qVars {
72
- vars [fmt .Sprintf ("%s.%s" , tag , k )] = v // prepend the parent tag name
73
+ if qVars != nil {
74
+ for k , v := range * qVars {
75
+ vars [fmt .Sprintf ("%s.%s" , tag , k )] = v // prepend the parent tag name
76
+ }
73
77
}
74
78
}
75
79
} else {
Original file line number Diff line number Diff line change @@ -160,3 +160,40 @@ func TestMultipleTagsStructToMap(t *testing.T) {
160
160
require .JSONEqf (t , expectedJSONOne , string (actualJSONOne ), "JSON mismatch for foo tags" )
161
161
require .JSONEqf (t , expectedJSONTwo , string (actualJSONTwo ), "JSON mismatch for bar tags" )
162
162
}
163
+
164
+ // TestNilAndUnexportedFields calls ToMap function and checks against the expected result
165
+ // The struct used tries to use nil/empty fields and unexported fields which should not cause the test to panic
166
+ func TestNilAndUnexportedFields (t * testing.T ) {
167
+ type MyStruct struct {
168
+ f1 string `custom:"f1"`
169
+ F2 struct {
170
+ F21 string `custom:"f21"`
171
+ } `custom:"age"`
172
+ F3 * string `custom:"f3"`
173
+ F4 int `custom:"f4"`
174
+ }
175
+
176
+ obj := MyStruct {
177
+ F4 : 666 ,
178
+ }
179
+
180
+ // expected response
181
+ expectedJSON := `{
182
+ "f3": null,
183
+ "f4": 666
184
+ }
185
+ `
186
+
187
+ // get the map from custom tags
188
+ actual , err := ToMap (obj , "custom" )
189
+ if err != nil {
190
+ t .Fail ()
191
+ }
192
+ actualJSON , err := json .Marshal (actual )
193
+ if err != nil {
194
+ t .Fail ()
195
+ }
196
+
197
+ // compare
198
+ require .JSONEqf (t , expectedJSON , string (actualJSON ), "JSON mismatch" )
199
+ }
You can’t perform that action at this time.
0 commit comments