Ver código fonte

添加方法

yjp 6 meses atrás
pai
commit
549f66cbaf
1 arquivos alterados com 145 adições e 106 exclusões
  1. 145 106
      reflectutils/reflectutils.go

+ 145 - 106
reflectutils/reflectutils.go

@@ -2,7 +2,6 @@ package reflectutils
 
 import (
 	"errors"
-	"fmt"
 	"reflect"
 	"strconv"
 )
@@ -33,29 +32,125 @@ func AssignStringValue(data any, val reflect.Value) error {
 		return errors.New("val应当为string类型的Value")
 	}
 
+	v, err := ToString(data)
+	if err != nil {
+		return err
+	}
+
+	val.SetString(v)
+
+	return nil
+}
+
+// AssignInt64Value 将any类型的值进行转化,赋值给int64类型的reflect.Value
+func AssignInt64Value(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")
+	}
+
+	v, err := ToInt64(data)
+	if err != nil {
+		return err
+	}
+
+	val.SetInt(v)
+
+	return nil
+}
+
+// AssignUint64Value 将any类型的值进行转化,赋值给uint64类型的reflect.Value
+func AssignUint64Value(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")
+	}
+
+	v, err := ToUint64(data)
+	if err != nil {
+		return err
+	}
+
+	val.SetUint(v)
+
+	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")
+	}
+
+	v, err := ToBool(data)
+	if err != nil {
+		return err
+	}
+
+	val.SetBool(v)
+
+	return nil
+}
+
+// AssignFloat64Value 将any类型的值进行转化,赋值给float64类型的reflect.Value
+func AssignFloat64Value(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")
+	}
+
+	v, err := ToFloat64(data)
+	if err != nil {
+		return err
+	}
+
+	val.SetFloat(v)
+
+	return nil
+}
+
+// ToString 将any类型的值进行转化为string类型
+func ToString(data any) (string, error) {
+	if data == nil {
+		return "", nil
+	}
+
 	dataVal := reflect.Indirect(reflect.ValueOf(data))
 	dataKind := GroupValueKind(dataVal)
 
 	switch dataKind {
 	case reflect.String:
-		val.SetString(dataVal.String())
+		return dataVal.String(), nil
 	case reflect.Bool:
 		if dataVal.Bool() {
-			val.SetString("1")
+			return "1", nil
 		} else {
-			val.SetString("0")
+			return "0", nil
 		}
 	case reflect.Int64:
-		val.SetString(strconv.FormatInt(dataVal.Int(), 10))
+		return strconv.FormatInt(dataVal.Int(), 10), nil
 	case reflect.Uint64:
-		val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
+		return strconv.FormatUint(dataVal.Uint(), 10), nil
 	case reflect.Float64:
-		val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
+		return strconv.FormatFloat(dataVal.Float(), 'f', -1, 64), nil
 	case reflect.Slice, reflect.Array:
 		elemKind := dataVal.Type().Elem().Kind()
 
 		if elemKind != reflect.Uint8 {
-			return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
+			return "", errors.New("不支持的类型: " + dataVal.Type().Elem().String())
 		}
 
 		var uints []uint8
@@ -68,22 +163,16 @@ func AssignStringValue(data any, val reflect.Value) error {
 			uints = dataVal.Interface().([]uint8)
 		}
 
-		val.SetString(string(uints))
+		return string(uints), nil
 	default:
-		return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
+		return "", errors.New("不支持的类型: " + dataVal.Type().Elem().String())
 	}
-
-	return nil
 }
 
-// AssignIntValue 将any类型的值进行转化,赋值给int类型的reflect.Value
-func AssignIntValue(data any, val reflect.Value) error {
+// ToInt64 将any类型的值进行转化为int64类型
+func ToInt64(data any) (int64, error) {
 	if data == nil {
-		return nil
-	}
-
-	if val.Kind() < reflect.Int && val.Kind() > reflect.Int64 {
-		return errors.New("val应当为int类型的Value")
+		return 0, nil
 	}
 
 	dataVal := reflect.Indirect(reflect.ValueOf(data))
@@ -91,16 +180,16 @@ func AssignIntValue(data any, val reflect.Value) error {
 
 	switch dataKind {
 	case reflect.Int64:
-		val.SetInt(dataVal.Int())
+		return dataVal.Int(), nil
 	case reflect.Uint64:
-		val.SetInt(int64(dataVal.Uint()))
+		return int64(dataVal.Uint()), nil
 	case reflect.Float64:
-		val.SetInt(int64(dataVal.Float()))
+		return int64(dataVal.Float()), nil
 	case reflect.Bool:
 		if dataVal.Bool() {
-			val.SetInt(1)
+			return 1, nil
 		} else {
-			val.SetInt(0)
+			return 0, nil
 		}
 	case reflect.String:
 		str := dataVal.String()
@@ -108,27 +197,16 @@ func AssignIntValue(data any, val reflect.Value) error {
 			str = "0"
 		}
 
-		i, err := strconv.ParseInt(str, 0, val.Type().Bits())
-		if err != nil {
-			return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
-		}
-
-		val.SetInt(i)
+		return strconv.ParseInt(str, 0, 10)
 	default:
-		return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
+		return 0, errors.New("不支持的类型: " + dataVal.Type().Elem().String())
 	}
-
-	return nil
 }
 
-// AssignUintValue 将any类型的值进行转化,赋值给uint类型的reflect.Value
-func AssignUintValue(data any, val reflect.Value) error {
+// ToUint64 将any类型的值进行转化为uint64类型
+func ToUint64(data any) (uint64, error) {
 	if data == nil {
-		return nil
-	}
-
-	if val.Kind() < reflect.Uint && val.Kind() > reflect.Uint64 {
-		return errors.New("val应当为uint类型的Value")
+		return 0, nil
 	}
 
 	dataVal := reflect.Indirect(reflect.ValueOf(data))
@@ -136,26 +214,16 @@ func AssignUintValue(data any, val reflect.Value) error {
 
 	switch dataKind {
 	case reflect.Int64:
-		i := dataVal.Int()
-		if i < 0 {
-			return fmt.Errorf("数值溢出: %d", i)
-		}
-
-		val.SetUint(uint64(i))
+		return uint64(dataVal.Int()), nil
 	case reflect.Uint64:
-		val.SetUint(dataVal.Uint())
+		return dataVal.Uint(), nil
 	case reflect.Float64:
-		f := dataVal.Float()
-		if f < 0 {
-			return fmt.Errorf("数值溢出: %f", f)
-		}
-
-		val.SetUint(uint64(f))
+		return uint64(dataVal.Float()), nil
 	case reflect.Bool:
 		if dataVal.Bool() {
-			val.SetUint(1)
+			return 1, nil
 		} else {
-			val.SetUint(0)
+			return 0, nil
 		}
 	case reflect.String:
 		str := dataVal.String()
@@ -163,27 +231,16 @@ func AssignUintValue(data any, val reflect.Value) error {
 			str = "0"
 		}
 
-		i, err := strconv.ParseUint(str, 0, val.Type().Bits())
-		if err != nil {
-			return err
-		}
-
-		val.SetUint(i)
+		return strconv.ParseUint(str, 0, 10)
 	default:
-		return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
+		return 0, errors.New("不支持的类型: " + dataVal.Type().Elem().String())
 	}
-
-	return nil
 }
 
-// AssignBoolValue 将any类型的值进行转化,赋值给bool类型的reflect.Value
-func AssignBoolValue(data any, val reflect.Value) error {
+// ToBool 将any类型的值进行转化为bool类型
+func ToBool(data any) (bool, error) {
 	if data == nil {
-		return nil
-	}
-
-	if val.Kind() != reflect.Bool {
-		return errors.New("val应当为bool类型的Value")
+		return false, nil
 	}
 
 	dataVal := reflect.Indirect(reflect.ValueOf(data))
@@ -191,39 +248,28 @@ func AssignBoolValue(data any, val reflect.Value) error {
 
 	switch dataKind {
 	case reflect.Bool:
-		val.SetBool(dataVal.Bool())
+		return dataVal.Bool(), nil
 	case reflect.Int64:
-		val.SetBool(dataVal.Int() != 0)
+		return dataVal.Int() != 0, nil
 	case reflect.Uint64:
-		val.SetBool(dataVal.Uint() != 0)
+		return dataVal.Uint() != 0, nil
 	case reflect.Float64:
-		val.SetBool(dataVal.Float() != 0)
+		return dataVal.Float() != 0, nil
 	case reflect.String:
 		if dataVal.String() == "" {
-			val.SetBool(false)
+			return false, nil
 		} else {
-			b, err := strconv.ParseBool(dataVal.String())
-			if err != nil {
-				return err
-			}
-
-			val.SetBool(b)
+			return strconv.ParseBool(dataVal.String())
 		}
 	default:
-		return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
+		return false, errors.New("不支持的类型: " + dataVal.Type().Elem().String())
 	}
-
-	return nil
 }
 
-// AssignFloatValue 将any类型的值进行转化,赋值给float类型的reflect.Value
-func AssignFloatValue(data any, val reflect.Value) error {
+// ToFloat64 将any类型的值进行转化为float64类型
+func ToFloat64(data any) (float64, error) {
 	if data == nil {
-		return nil
-	}
-
-	if val.Kind() != reflect.Float32 && val.Kind() != reflect.Float64 {
-		return errors.New("val应当为float类型的Value")
+		return 0, nil
 	}
 
 	dataVal := reflect.Indirect(reflect.ValueOf(data))
@@ -231,16 +277,16 @@ func AssignFloatValue(data any, val reflect.Value) error {
 
 	switch dataKind {
 	case reflect.Int64:
-		val.SetFloat(float64(dataVal.Int()))
+		return float64(dataVal.Int()), nil
 	case reflect.Uint64:
-		val.SetFloat(float64(dataVal.Uint()))
+		return float64(dataVal.Uint()), nil
 	case reflect.Float64:
-		val.SetFloat(dataVal.Float())
+		return dataVal.Float(), nil
 	case reflect.Bool:
 		if dataVal.Bool() {
-			val.SetFloat(1)
+			return 1, nil
 		} else {
-			val.SetFloat(0)
+			return 0, nil
 		}
 	case reflect.String:
 		str := dataVal.String()
@@ -248,15 +294,8 @@ func AssignFloatValue(data any, val reflect.Value) error {
 			str = "0"
 		}
 
-		f, err := strconv.ParseFloat(str, val.Type().Bits())
-		if err == nil {
-			return err
-		}
-
-		val.SetFloat(f)
+		return strconv.ParseFloat(str, 10)
 	default:
-		return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
+		return 0, errors.New("不支持的类型: " + dataVal.Type().Elem().String())
 	}
-
-	return nil
 }