Skip to content

Commit

Permalink
Fixed panics for non-struct slices
Browse files Browse the repository at this point in the history
  • Loading branch information
dumim committed Jul 6, 2021
1 parent 1838bce commit 7232726
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,22 @@ func getMapOfAllKeyValues(s interface{}) *map[string]interface{} {
switch reflect.TypeOf(v).Kind() {
// if any of them is a slice
case reflect.Slice:
var sliceOfMap []map[string]interface{}
s := reflect.ValueOf(v)
// iterate through the slice
for i := 0; i < s.Len(); i++ {
if s.Index(i).CanInterface() {
m := getMapOfAllKeyValues(s.Index(i).Interface()) // get the map value of the object, recursively
if m != nil {
sliceOfMap = append(sliceOfMap, *m) // append to the slice
if reflect.TypeOf(v).Elem().Kind() == reflect.Struct{
var sliceOfMap []map[string]interface{}
s := reflect.ValueOf(v)
// iterate through the slice
for i := 0; i < s.Len(); i++ {
if s.Index(i).CanInterface() {
m := getMapOfAllKeyValues(s.Index(i).Interface()) // get the map value of the object, recursively
if m != nil {
sliceOfMap = append(sliceOfMap, *m) // append to the slice
}
}
}
finalMap[k] = sliceOfMap
} else {
finalMap[k] = v
}
finalMap[k] = sliceOfMap
default:
finalMap[k] = v
}
Expand Down

0 comments on commit 7232726

Please # to comment.