MapToSlice mapper from map[string]interface{} to a slice of any type's ptr toSlice must be a slice of any type.
(fromMap map[string]interface{}, toSlice interface{})
| 231 | // MapToSlice mapper from map[string]interface{} to a slice of any type's ptr |
| 232 | // toSlice must be a slice of any type. |
| 233 | func (dm *mapperObject) MapToSlice(fromMap map[string]interface{}, toSlice interface{}) error { |
| 234 | var err error |
| 235 | toValue := reflect.ValueOf(toSlice) |
| 236 | if toValue.Kind() != reflect.Ptr { |
| 237 | return errors.New("toSlice must be a pointer to a slice") |
| 238 | } |
| 239 | if toValue.IsNil() { |
| 240 | return errors.New("toSlice must not be a nil pointer") |
| 241 | } |
| 242 | |
| 243 | toElemType := reflect.TypeOf(toSlice).Elem().Elem() |
| 244 | realType := toElemType.Kind() |
| 245 | direct := reflect.Indirect(toValue) |
| 246 | if realType == reflect.Ptr { |
| 247 | toElemType = toElemType.Elem() |
| 248 | } |
| 249 | for _, v := range fromMap { |
| 250 | if reflect.TypeOf(v).Kind().String() == "map" { |
| 251 | elem := reflect.New(toElemType) |
| 252 | err = dm.MapperMap(v.(map[string]interface{}), elem.Interface()) |
| 253 | if err == nil { |
| 254 | if realType == reflect.Ptr { |
| 255 | direct.Set(reflect.Append(direct, elem)) |
| 256 | } else { |
| 257 | direct.Set(reflect.Append(direct, elem).Elem()) |
| 258 | } |
| 259 | } |
| 260 | } else { |
| 261 | if realType == reflect.Ptr { |
| 262 | direct.Set(reflect.Append(direct, reflect.ValueOf(v))) |
| 263 | } else { |
| 264 | direct.Set(reflect.Append(direct, reflect.ValueOf(v).Elem())) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | return err |
| 270 | } |
| 271 | |
| 272 | // MapperMapSlice mapper from map[string]map[string]interface{} to a slice of any type's ptr |
| 273 | // toSlice must be a slice of any type. |