123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- package reflectutils
- import (
- "errors"
- "fmt"
- "reflect"
- "strconv"
- )
- // GroupValueKind 将反射的Kind值进行聚集
- func GroupValueKind(val reflect.Value) reflect.Kind {
- kind := val.Kind()
- switch {
- case kind >= reflect.Int && kind <= reflect.Int64:
- return reflect.Int64
- case kind >= reflect.Uint && kind <= reflect.Uint64:
- return reflect.Uint64
- case kind >= reflect.Float32 && kind <= reflect.Float64:
- return reflect.Float64
- default:
- return kind
- }
- }
- // AssignStringValue 将any类型的值进行转化,赋值给string的reflect.Value
- func AssignStringValue(data any, val reflect.Value) error {
- if data == nil {
- return nil
- }
- if val.Kind() != reflect.String {
- return errors.New("val应当为string类型的Value")
- }
- dataVal := reflect.Indirect(reflect.ValueOf(data))
- dataKind := GroupValueKind(dataVal)
- switch dataKind {
- case reflect.String:
- val.SetString(dataVal.String())
- case reflect.Bool:
- if dataVal.Bool() {
- val.SetString("1")
- } else {
- val.SetString("0")
- }
- case reflect.Int64:
- val.SetString(strconv.FormatInt(dataVal.Int(), 10))
- case reflect.Uint64:
- val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
- case reflect.Float64:
- val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
- case reflect.Slice, reflect.Array:
- elemKind := dataVal.Type().Elem().Kind()
- if elemKind != reflect.Uint8 {
- return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
- }
- var uints []uint8
- if dataKind == reflect.Array {
- uints = make([]uint8, dataVal.Len(), dataVal.Len())
- for i := range uints {
- uints[i] = dataVal.Index(i).Interface().(uint8)
- }
- } else {
- uints = dataVal.Interface().([]uint8)
- }
- val.SetString(string(uints))
- default:
- return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
- }
- return nil
- }
- // AssignIntValue 将any类型的值进行转化,赋值给int类型的reflect.Value
- func AssignIntValue(data any, val reflect.Value) error {
- if data == nil {
- return nil
- }
- if val.Kind() < reflect.Int && val.Kind() > reflect.Int64 {
- return errors.New("val应当为int类型的Value")
- }
- dataVal := reflect.Indirect(reflect.ValueOf(data))
- dataKind := GroupValueKind(dataVal)
- switch dataKind {
- case reflect.Int64:
- val.SetInt(dataVal.Int())
- case reflect.Uint64:
- val.SetInt(int64(dataVal.Uint()))
- case reflect.Float64:
- val.SetInt(int64(dataVal.Float()))
- case reflect.Bool:
- if dataVal.Bool() {
- val.SetInt(1)
- } else {
- val.SetInt(0)
- }
- case reflect.String:
- str := dataVal.String()
- if str == "" {
- str = "0"
- }
- i, err := strconv.ParseInt(str, 0, val.Type().Bits())
- if err != nil {
- return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
- }
- val.SetInt(i)
- default:
- return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
- }
- return nil
- }
- // AssignUintValue 将any类型的值进行转化,赋值给uint类型的reflect.Value
- func AssignUintValue(data any, val reflect.Value) error {
- if data == nil {
- return nil
- }
- if val.Kind() < reflect.Uint && val.Kind() > reflect.Uint64 {
- return errors.New("val应当为uint类型的Value")
- }
- dataVal := reflect.Indirect(reflect.ValueOf(data))
- dataKind := GroupValueKind(dataVal)
- switch dataKind {
- case reflect.Int64:
- i := dataVal.Int()
- if i < 0 {
- return fmt.Errorf("数值溢出: %d", i)
- }
- val.SetUint(uint64(i))
- case reflect.Uint64:
- val.SetUint(dataVal.Uint())
- case reflect.Float64:
- f := dataVal.Float()
- if f < 0 {
- return fmt.Errorf("数值溢出: %f", f)
- }
- val.SetUint(uint64(f))
- case reflect.Bool:
- if dataVal.Bool() {
- val.SetUint(1)
- } else {
- val.SetUint(0)
- }
- case reflect.String:
- str := dataVal.String()
- if str == "" {
- str = "0"
- }
- i, err := strconv.ParseUint(str, 0, val.Type().Bits())
- if err != nil {
- return err
- }
- val.SetUint(i)
- default:
- return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
- }
- return nil
- }
- // AssignBoolValue 将any类型的值进行转化,赋值给bool类型的reflect.Value
- func AssignBoolValue(data any, val reflect.Value) error {
- if data == nil {
- return nil
- }
- if val.Kind() != reflect.Bool {
- return errors.New("val应当为bool类型的Value")
- }
- dataVal := reflect.Indirect(reflect.ValueOf(data))
- dataKind := GroupValueKind(dataVal)
- switch dataKind {
- case reflect.Bool:
- val.SetBool(dataVal.Bool())
- case reflect.Int64:
- val.SetBool(dataVal.Int() != 0)
- case reflect.Uint64:
- val.SetBool(dataVal.Uint() != 0)
- case reflect.Float64:
- val.SetBool(dataVal.Float() != 0)
- case reflect.String:
- if dataVal.String() == "" {
- val.SetBool(false)
- } else {
- b, err := strconv.ParseBool(dataVal.String())
- if err != nil {
- return err
- }
- val.SetBool(b)
- }
- default:
- return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
- }
- return nil
- }
- // AssignFloatValue 将any类型的值进行转化,赋值给float类型的reflect.Value
- func AssignFloatValue(data any, val reflect.Value) error {
- if data == nil {
- return nil
- }
- if val.Kind() != reflect.Float32 && val.Kind() != reflect.Float64 {
- return errors.New("val应当为float类型的Value")
- }
- dataVal := reflect.Indirect(reflect.ValueOf(data))
- dataKind := GroupValueKind(dataVal)
- switch dataKind {
- case reflect.Int64:
- val.SetFloat(float64(dataVal.Int()))
- case reflect.Uint64:
- val.SetFloat(float64(dataVal.Uint()))
- case reflect.Float64:
- val.SetFloat(dataVal.Float())
- case reflect.Bool:
- if dataVal.Bool() {
- val.SetFloat(1)
- } else {
- val.SetFloat(0)
- }
- case reflect.String:
- str := dataVal.String()
- if str == "" {
- str = "0"
- }
- f, err := strconv.ParseFloat(str, val.Type().Bits())
- if err == nil {
- return err
- }
- val.SetFloat(f)
- default:
- return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
- }
- return nil
- }
|