description
wants to be a data component similar to orm. If you encounter this problem, convert the response of a [] map [string] interface {} into a structure array of the corresponding type
.ideas
read the source code of orm and some articles on the Internet, and then use reflection to complete the assignment of a single structure, that is, err: = o.Read (& user)
in orm. When doing the array, refer to func (o * rawSet) QueryRows (containers. Interface {}) (int64, error)
related codes
package test
import (
"errors"
"fmt"
"reflect"
"strings"
)
func MapToStruct() {
mList := []map[string]interface{}{
{"Id": 213, "Name": "zhaoliu", "Sex": ""},
{"Id": 56, "Name": "zhangsan", "Sex": ""},
{"Id": 7, "Name": "lisi", "Sex": ""},
{"Id": 978, "Name": "wangwu", "Sex": ""},
}
type User struct {
Id int
Name string
Sex string
}
users := []*User{}
mapToStruct(mList, &users)
fmt.Printf("users: %+v\n", users)
}
func mapToStruct(mList []map[string]interface{}, model interface{}) (err error) {
val := reflect.Indirect(reflect.ValueOf(model))
typ := val.Type()
fmt.Printf("val: %v\n", val)
fmt.Printf("typ: %v\n", typ)
for _, r := range mList {
mVal := reflect.Indirect(reflect.New(typ.Elem().Elem())).Addr()
//fmt.Printf("mVal: %+v\n", mVal)
for key, val := range r {
err = setField(mVal.Interface(), key, val)
if err != nil {
return err
}
}
//fmt.Printf("mVal: %+v\n", mVal)
val = reflect.Append(val, mVal)
}
fmt.Printf("val: %+v\n", val.Interface())
//model = val.Interface()
return err
}
//map
func setField(obj interface{}, name string, value interface{}) error {
//
sl := strings.Split(name, "")
sl[0] = strings.ToUpper(sl[0])
name = strings.Join(sl, "")
structValue := reflect.ValueOf(obj).Elem() //
//fmt.Printf("structValue: %+v\n", structValue)
structFieldValue := structValue.FieldByName(name) //
//fmt.Printf("structFieldValue: %+v\n", structFieldValue)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := structFieldValue.Type() //
val := reflect.ValueOf(value) //map
if structFieldType != val.Type() {
return errors.New("type is err")
}
structFieldValue.Set(val)
return nil
}