[golang] want to use reflection to do a map array to transform the structure array, the code is as follows, how to add the reflection generated structure to the array?

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
}
Mar.24,2021

idebar" rel=" nofollow noreferrer "> someone solved my problem in the go community

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37a7a-2c0b5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37a7a-2c0b5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?