|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- package utils
-
- import (
- "encoding/json"
- "fmt"
- "reflect"
- "strconv"
- "strings"
- )
-
- func Map2Struct(vals map[string]interface{}, dst interface{}) (err error) {
- return Map2StructByTag(vals, dst, "json")
- }
-
- func Map2StructByTag(vals map[string]interface{}, dst interface{}, structTag string) (err error) {
- defer func() {
- e := recover()
- if e != nil {
- if v, ok := e.(error); ok {
- err = fmt.Errorf("Panic: %v", v.Error())
- } else {
- err = fmt.Errorf("Panic: %v", e)
- }
- }
- }()
-
- pt := reflect.TypeOf(dst)
- pv := reflect.ValueOf(dst)
-
- if pv.Kind() != reflect.Ptr || pv.Elem().Kind() != reflect.Struct {
- return fmt.Errorf("not a pointer of struct")
- }
-
- var f reflect.StructField
- var ft reflect.Type
- var fv reflect.Value
-
- for i := 0; i < pt.Elem().NumField(); i++ {
- f = pt.Elem().Field(i)
- fv = pv.Elem().Field(i)
- ft = f.Type
-
- if f.Anonymous || !fv.CanSet() {
- continue
- }
-
- tag := f.Tag.Get(structTag)
-
- name, option := parseTag(tag)
-
- if name == "-" {
- continue
- }
-
- if name == "" {
- name = strings.ToLower(f.Name)
- }
- val, ok := vals[name]
-
- if !ok {
- if option == "required" {
- return fmt.Errorf("'%v' not found", name)
- }
- if len(option) != 0 {
- val = option
- } else {
-
- continue
- }
- }
-
-
- vv := reflect.ValueOf(val)
- vt := reflect.TypeOf(val)
-
- if vt.Kind() != reflect.String {
-
- if vt.AssignableTo(ft) {
- fv.Set(vv)
- continue
- }
-
- if vt.ConvertibleTo(ft) {
- fv.Set(vv.Convert(ft))
- continue
- }
-
- return fmt.Errorf("value type not match: field=%v(%v) value=%v(%v)", f.Name, ft.Kind(), val, vt.Kind())
- }
- s := strings.TrimSpace(vv.String())
- if len(s) == 0 && option == "required" {
- return fmt.Errorf("value of required argument can't not be empty")
- }
- fk := ft.Kind()
-
-
- if fk == reflect.Ptr && ft.Elem().Kind() == reflect.String {
- fv.Set(reflect.ValueOf(&s))
- continue
- }
- if fk == reflect.Ptr || fk == reflect.Struct {
- err = convertJsonValue(s, name, fv)
- } else if fk == reflect.Slice {
- err = convertSlice(s, f.Name, ft, fv)
- } else {
- err = convertValue(fk, s, f.Name, fv)
- }
-
- if err != nil {
- return err
- }
- continue
- }
-
- return nil
- }
-
- func Struct2Map(s interface{}) map[string]interface{} {
- return Struct2MapByTag(s, "json")
- }
- func Struct2MapByTag(s interface{}, tagName string) map[string]interface{} {
- t := reflect.TypeOf(s)
- v := reflect.ValueOf(s)
-
- if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
- t = t.Elem()
- v = v.Elem()
- }
-
- if v.Kind() != reflect.Struct {
- return nil
- }
-
- m := make(map[string]interface{})
-
- for i := 0; i < t.NumField(); i++ {
- fv := v.Field(i)
- ft := t.Field(i)
-
- if !fv.CanInterface() {
- continue
- }
-
- if ft.PkgPath != "" {
- continue
- }
-
- var name string
- var option string
- tag := ft.Tag.Get(tagName)
- if tag != "" {
- ts := strings.Split(tag, ",")
- if len(ts) == 1 {
- name = ts[0]
- } else if len(ts) > 1 {
- name = ts[0]
- option = ts[1]
- }
- if name == "-" {
- continue
- }
- if name == "" {
- name = strings.ToLower(ft.Name)
- }
- if option == "omitempty" {
- if isEmpty(&fv) {
- continue
- }
- }
- } else {
- name = strings.ToLower(ft.Name)
- }
-
- if ft.Anonymous && fv.Kind() == reflect.Ptr && fv.IsNil() {
- continue
- }
- if (ft.Anonymous && fv.Kind() == reflect.Struct) ||
- (ft.Anonymous && fv.Kind() == reflect.Ptr && fv.Elem().Kind() == reflect.Struct) {
-
-
- embedded := Struct2MapByTag(fv.Interface(), tagName)
- for embName, embValue := range embedded {
- m[embName] = embValue
- }
- } else if option == "string" {
- kind := fv.Kind()
- if kind == reflect.Int || kind == reflect.Int8 || kind == reflect.Int16 || kind == reflect.Int32 || kind == reflect.Int64 {
- m[name] = strconv.FormatInt(fv.Int(), 10)
- } else if kind == reflect.Uint || kind == reflect.Uint8 || kind == reflect.Uint16 || kind == reflect.Uint32 || kind == reflect.Uint64 {
- m[name] = strconv.FormatUint(fv.Uint(), 10)
- } else if kind == reflect.Float32 || kind == reflect.Float64 {
- m[name] = strconv.FormatFloat(fv.Float(), 'f', 2, 64)
- } else {
- m[name] = fv.Interface()
- }
- } else {
- m[name] = fv.Interface()
- }
- }
-
- return m
- }
-
- func isEmpty(v *reflect.Value) bool {
- k := v.Kind()
- if k == reflect.Bool {
- return v.Bool() == false
- } else if reflect.Int < k && k < reflect.Int64 {
- return v.Int() == 0
- } else if reflect.Uint < k && k < reflect.Uintptr {
- return v.Uint() == 0
- } else if k == reflect.Float32 || k == reflect.Float64 {
- return v.Float() == 0
- } else if k == reflect.Array || k == reflect.Map || k == reflect.Slice || k == reflect.String {
- return v.Len() == 0
- } else if k == reflect.Interface || k == reflect.Ptr {
- return v.IsNil()
- }
- return false
- }
-
- func convertSlice(s string, name string, ft reflect.Type, fv reflect.Value) error {
- var err error
- et := ft.Elem()
-
- if et.Kind() == reflect.Ptr || et.Kind() == reflect.Struct {
- return convertJsonValue(s, name, fv)
- }
-
- ss := strings.Split(s, ",")
-
- if len(s) == 0 || len(ss) == 0 {
- return nil
- }
-
- fs := reflect.MakeSlice(ft, 0, len(ss))
-
- for _, si := range ss {
- ev := reflect.New(et).Elem()
-
- err = convertValue(et.Kind(), si, name, ev)
- if err != nil {
- return err
- }
- fs = reflect.Append(fs, ev)
- }
-
- fv.Set(fs)
-
- return nil
- }
-
- func convertJsonValue(s string, name string, fv reflect.Value) error {
- var err error
- d := StringToSlice(s)
-
- if fv.Kind() == reflect.Ptr {
- if fv.IsNil() {
- fv.Set(reflect.New(fv.Type().Elem()))
- }
- } else {
- fv = fv.Addr()
- }
-
- err = json.Unmarshal(d, fv.Interface())
-
- if err != nil {
- return fmt.Errorf("invalid json '%v': %v, %v", name, err.Error(), s)
- }
-
- return nil
- }
-
- func convertValue(kind reflect.Kind, s string, name string, fv reflect.Value) error {
- if !fv.CanAddr() {
- return fmt.Errorf("can not addr: %v", name)
- }
-
- if kind == reflect.String {
- fv.SetString(s)
- return nil
- }
-
- if kind == reflect.Bool {
- switch s {
- case "true":
- fv.SetBool(true)
- case "false":
- fv.SetBool(false)
- case "1":
- fv.SetBool(true)
- case "0":
- fv.SetBool(false)
- default:
- return fmt.Errorf("invalid bool: %v value=%v", name, s)
- }
- return nil
- }
-
- if reflect.Int <= kind && kind <= reflect.Int64 {
- i, err := strconv.ParseInt(s, 10, 64)
- if err != nil {
- return fmt.Errorf("invalid int: %v value=%v", name, s)
- }
- fv.SetInt(i)
-
- } else if reflect.Uint <= kind && kind <= reflect.Uint64 {
- i, err := strconv.ParseUint(s, 10, 64)
- if err != nil {
- return fmt.Errorf("invalid int: %v value=%v", name, s)
- }
- fv.SetUint(i)
-
- } else if reflect.Float32 == kind || kind == reflect.Float64 {
- i, err := strconv.ParseFloat(s, 64)
-
- if err != nil {
- return fmt.Errorf("invalid float: %v value=%v", name, s)
- }
-
- fv.SetFloat(i)
- } else {
-
-
- }
- return nil
- }
-
- func parseTag(tag string) (string, string) {
- tags := strings.Split(tag, ",")
-
- if len(tags) <= 0 {
- return "", ""
- }
-
- if len(tags) == 1 {
- return tags[0], ""
- }
-
- return tags[0], tags[1]
- }
|