package utils import ( "log" "reflect" "strings" ) func ReflectMakeNew(t reflect.Type) interface{} { retptr := reflect.New(t) sval := retptr.Elem().Interface() return sval } type TagMap map[string] string func ReflectTagMap(t reflect.Type) map[string] TagMap{ log.Print(t.Name()) ret := make(map[string] TagMap) num := t.NumField() for i := 0;i < num;i++{ sub := strings.Split(string(t.Field(i).Tag),"\"") for k,v := range(sub){ if len(v) > 0{ if k%2 == 0{ v = strings.Trim(v," ") } if v[len(v) - 1] == ':'{ if _,ok := ret[v[:len(v) - 1]];ok{ if ((k + 1) < len(sub) - 1) && ((sub[k + 1][len(sub[k + 1]) - 1] ) == ':'){ continue; }else{ ret[v[:len(v) - 1]][sub[k + 1]] = string(t.Field(i).Name) } }else{ ret[v[:len(v) - 1]] = make(TagMap) log.Print() if ((k + 1) < len(sub) - 1) && ((sub[k + 1][len(sub[k + 1]) - 1] ) == ':'){ continue; }else{ ret[v[:len(v) - 1]][sub[k + 1]] = string(t.Field(i).Name) } } }else{ continue } } } } return ret } func SameKind(typ1 reflect.Kind,typ2 reflect.Kind) bool{ switch typ1{ case reflect.Int: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Int8: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Int16: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Int32: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Uint: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Uint8: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Uint16: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Uint32: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Uint64: if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ return true } if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ return true } case reflect.Float32: if(typ2 == reflect.Float32 ||typ2 == reflect.Float64){ return true } case reflect.Float64: if(typ2 == reflect.Float32 ||typ2 == reflect.Float64){ return true } default: return (typ1 == typ2) } return false } func UnmarshalJson(value interface{}, maps map[string]interface{}){ valueof := reflect.ValueOf(value) if !valueof.Elem().CanAddr(){ log.Print("should be addr") return } remap := ReflectTagMap(valueof.Elem().Type()) _,ok := remap["json"] if !ok{ return } for k,v := range(maps){ log.Print(k) if filedName,ok := remap["json"][k];ok{ log.Print(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind().String()) if SameKind(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind()){ if(valueof.Elem().FieldByName(filedName).CanSet()){ valueof.Elem().FieldByName(filedName).Set(reflect.ValueOf(v).Convert(valueof.Elem().FieldByName(filedName).Type())) } } } } }