diff --git a/map.go b/map.go index 3240435..42da414 100644 --- a/map.go +++ b/map.go @@ -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 } diff --git a/map_test.go b/map_test.go index 8085b7e..3eb75c0 100644 --- a/map_test.go +++ b/map_test.go @@ -28,6 +28,7 @@ type Example struct { ObjThree ObjTwo `custom:"-"` // explicitly ignored Id int `custom:"id"` Call int `custom:"data.call"` + Array []string `custom:"array"` ArrayObj []ObjThree `custom:"list"` //three int `custom:"three"` // unexported, TODO: handle panic } @@ -49,8 +50,9 @@ func TestFullStructToMap(t *testing.T) { Hello: "1", Text: "2", }, - Id: 01, - Call: 02, + Id: 01, + Call: 02, + Array: []string{"1", "2"}, ArrayObj: []ObjThree{ {"hi", 1}, {"world", 2}, @@ -74,6 +76,7 @@ func TestFullStructToMap(t *testing.T) { "call": 2 }, "id": 1, + "array": ["1", "2"], "list": [ { "name": "hi",