Skip to content

Commit dc779c1

Browse files
authored
Merge pull request #9 from dumim/develop
Panics on nil interfaces fixed
2 parents 59c9118 + 62923b1 commit dc779c1

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
4+
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
9+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
13+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

map.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,29 @@ func getMapOfAllKeyValues(s interface{}) *map[string]interface{} {
9090
var finalMap = make(map[string]interface{})
9191
// iterate through the map
9292
for k, v := range vars {
93-
switch reflect.TypeOf(v).Kind() {
94-
// if any of them is a slice
95-
case reflect.Slice:
96-
if reflect.TypeOf(v).Elem().Kind() == reflect.Struct{
97-
var sliceOfMap []map[string]interface{}
98-
s := reflect.ValueOf(v)
99-
// iterate through the slice
100-
for i := 0; i < s.Len(); i++ {
101-
if s.Index(i).CanInterface() {
102-
m := getMapOfAllKeyValues(s.Index(i).Interface()) // get the map value of the object, recursively
103-
if m != nil {
104-
sliceOfMap = append(sliceOfMap, *m) // append to the slice
93+
if reflect.TypeOf(v) != nil {
94+
switch reflect.TypeOf(v).Kind() {
95+
// if any of them is a slice
96+
case reflect.Slice:
97+
if reflect.TypeOf(v).Elem().Kind() == reflect.Struct {
98+
var sliceOfMap []map[string]interface{}
99+
s := reflect.ValueOf(v)
100+
// iterate through the slice
101+
for i := 0; i < s.Len(); i++ {
102+
if s.Index(i).CanInterface() {
103+
m := getMapOfAllKeyValues(s.Index(i).Interface()) // get the map value of the object, recursively
104+
if m != nil {
105+
sliceOfMap = append(sliceOfMap, *m) // append to the slice
106+
}
105107
}
106108
}
109+
finalMap[k] = sliceOfMap
110+
} else {
111+
finalMap[k] = v
107112
}
108-
finalMap[k] = sliceOfMap
109-
} else {
113+
default:
110114
finalMap[k] = v
111115
}
112-
default:
113-
finalMap[k] = v
114116
}
115117
}
116118

@@ -134,6 +136,10 @@ func buildMap(s []string, value interface{}, parent *map[string]interface{}) err
134136
// these values can be written in dot notation to create complex nested maps
135137
// for a more comprehensive example, please see the
136138
func ToMap(obj interface{}, tag string) (*map[string]interface{}, error) {
139+
if obj == nil {
140+
return nil, fmt.Errorf("nil object passed")
141+
}
142+
137143
tagName = tag
138144
s := getMapOfAllKeyValues(obj)
139145

map_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,22 @@ func TestNilAndUnexportedFields(t *testing.T) {
172172
F2 struct {
173173
F21 string `custom:"f21"`
174174
} `custom:"age"`
175-
F3 *string `custom:"f3"`
176-
F4 int `custom:"f4"`
175+
F3 *string `custom:"f3"`
176+
F4 int `custom:"f4"`
177+
F5 interface{} `custom:"f5"`
178+
F6 interface{} `custom:"f6"`
177179
}
178180

179181
obj := MyStruct{
180182
F4: 666,
183+
F6: "666",
181184
}
182185

183186
// expected response
184187
expectedJSON := `{
185188
"f3": null,
186-
"f4": 666
189+
"f4": 666,
190+
"f6": "666"
187191
}
188192
`
189193

0 commit comments

Comments
 (0)