MCPcopy Create free account
hub / github.com/devfeel/mapper / MapToSlice

Method MapToSlice

mapper_object.go:233–270  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

231// MapToSlice mapper from map[string]interface{} to a slice of any type's ptr
232// toSlice must be a slice of any type.
233func (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.

Callers

nothing calls this directly

Calls 3

MapperMapMethod · 0.95
SetMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected