package main
import (
"reflect"
"fmt"
)
type ModelStruct struct {
Name string `json:"name"`
Id int64 `json:"id"`
Age int `json:"age"`
}
func SetField(obj interface{}, name string, value interface{}) error {
structValue := reflect.ValueOf(obj).Elem()
structFieldValue := structValue.FieldByName(name)
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)
if structFieldType != val.Type() {
return fmt.Errorf("Provided value type didn't match obj field type")
}
structFieldValue.Set(val)
return nil
}
func FormatModelStruct(m map[string]interface{}) (ModelStruct, error) {
var s ModelStruct
for k, v := range m {
err := SetField(&s, k, v)
if err != nil {
return s, err
}
}
return s, nil
}
func main() {
myData := make(map[string]interface{})
myData["Name"] = "Tony"
myData["Age"] = 23
result, err := FormatModelStruct(myData)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}