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

Method MapperSlice

mapper_object.go:308–342  ·  view source on GitHub ↗

MapperSlice mapper from slice of struct to a slice of any type fromSlice and toSlice must be a slice of any type.

(fromSlice, toSlice interface{})

Source from the content-addressed store, hash-verified

306// MapperSlice mapper from slice of struct to a slice of any type
307// fromSlice and toSlice must be a slice of any type.
308func (dm *mapperObject) MapperSlice(fromSlice, toSlice interface{}) error {
309 var err error
310 toValue := reflect.ValueOf(toSlice)
311 if toValue.Kind() != reflect.Ptr {
312 return errors.New("toSlice must be a pointer to a slice")
313 }
314 if toValue.IsNil() {
315 return errors.New("toSlice must not be a nil pointer")
316 }
317
318 elemType := reflect.TypeOf(toSlice).Elem().Elem()
319 realType := elemType.Kind()
320 direct := reflect.Indirect(toValue)
321 // 3 elem parse: 1.[]*type 2.*type 3.type
322 if realType == reflect.Ptr {
323 elemType = elemType.Elem()
324 }
325
326 fromElems := dm.convertToSlice(fromSlice)
327 for _, v := range fromElems {
328 elem := reflect.New(elemType).Elem()
329 if realType == reflect.Ptr {
330 elem = reflect.New(elemType)
331 }
332 if realType == reflect.Ptr {
333 err = dm.elemMapper(reflect.ValueOf(v).Elem(), elem.Elem())
334 } else {
335 err = dm.elemMapper(reflect.ValueOf(v), elem)
336 }
337 if err == nil {
338 direct.Set(reflect.Append(direct, elem))
339 }
340 }
341 return err
342}
343
344// MapToJson mapper from map[string]interface{} to json []byte
345func (dm *mapperObject) MapToJson(fromMap map[string]interface{}) ([]byte, error) {

Callers

nothing calls this directly

Calls 3

convertToSliceMethod · 0.95
elemMapperMethod · 0.95
SetMethod · 0.80

Tested by

no test coverage detected