@@ -66,6 +66,8 @@ func (i Mapper) mapAny(path string, v interface{}) (interface{}, error) {
6666 return i .mapAny (path , reflectValue .Elem ().Interface ())
6767 case reflectValue .Kind () == reflect .Struct :
6868 return i .mapStruct (path , reflectValue )
69+ case reflectValue .Kind () == reflect .Map && reflectValue .Type ().Key ().Kind () == reflect .String :
70+ return i .mapStringMap (path , reflectValue )
6971 case reflectValue .Kind () == reflect .Slice :
7072 return i .mapSlice (path , reflectValue )
7173 default :
@@ -106,15 +108,33 @@ func (i Mapper) mapStruct(path string, reflectValue reflect.Value) (map[string]i
106108 value := reflectValue .Field (j )
107109 element := Element {name : fieldName , Value : value , field : & field }
108110
109- if err := i .mapStructField (fieldPath , element , result ); err != nil {
111+ if err := i .mapElement (fieldPath , element , result ); err != nil {
110112 return nil , err
111113 }
112114 }
113115
114116 return result , nil
115117}
116118
117- func (i Mapper ) mapStructField (fieldPath string , element Element , result map [string ]interface {}) error {
119+ func (i Mapper ) mapStringMap (path string , reflectValue reflect.Value ) (map [string ]interface {}, error ) {
120+ result := map [string ]interface {}{}
121+
122+ keys := reflectValue .MapKeys ()
123+ for _ , key := range keys {
124+ fieldName := key .String ()
125+ fieldPath := path + "." + fieldName
126+ value := reflectValue .MapIndex (key )
127+ element := Element {name : fieldName , Value : value }
128+
129+ if err := i .mapElement (fieldPath , element , result ); err != nil {
130+ return nil , err
131+ }
132+ }
133+
134+ return result , nil
135+ }
136+
137+ func (i Mapper ) mapElement (fieldPath string , element Element , result map [string ]interface {}) error {
118138 accepted , filterErr := i .Filter (fieldPath , element )
119139 if filterErr != nil {
120140 return fmt .Errorf ("Filter failed: %w" , filterErr )
@@ -156,9 +176,28 @@ func (i Mapper) mapSlice(path string, reflectValue reflect.Value) (_ interface{}
156176 }
157177 }
158178
179+ return slice , nil
180+ case reflect .Map :
181+ if reflectValue .Type ().Elem ().Key ().Kind () != reflect .String {
182+ return reflectValue .Interface (), nil
183+ }
184+
185+ slice := make ([]map [string ]interface {}, reflectValue .Len ())
186+
187+ for j := 0 ; j < reflectValue .Len (); j ++ {
188+ slice [j ], err = i .mapStringMap (slicePath (path , j ), reflectValue .Index (j ))
189+ if err != nil {
190+ return nil , err
191+ }
192+ }
193+
159194 return slice , nil
160195 case reflect .Slice :
161- if reflectValue .Type ().Elem ().Elem ().Kind () == reflect .Struct {
196+ sliceElem := reflectValue .Type ().Elem ().Elem ()
197+
198+ if sliceElem .Kind () == reflect .Struct ||
199+ (sliceElem .Kind () == reflect .Map && sliceElem .Key ().Kind () == reflect .String ) {
200+
162201 var slice [][]map [string ]interface {}
163202
164203 for j := 0 ; j < reflectValue .Len (); j ++ {
0 commit comments