From 3d048cb9b66eab17b3193150e7736aac3e7672c0 Mon Sep 17 00:00:00 2001 From: Yun Date: Sun, 3 Dec 2023 16:20:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auto_conv.go | 26 ++++++++ convx_test.go | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 + to_float32.go | 50 +++++++++++++++ to_float64.go | 47 ++++++++++++++ to_int.go | 40 ++++++++++++ to_int32.go | 7 ++ to_int64.go | 41 ++++++++++++ to_string.go | 44 +++++++++++++ 9 files changed, 430 insertions(+) create mode 100644 auto_conv.go create mode 100644 convx_test.go create mode 100644 go.mod create mode 100644 to_float32.go create mode 100644 to_float64.go create mode 100644 to_int.go create mode 100644 to_int32.go create mode 100644 to_int64.go create mode 100644 to_string.go diff --git a/auto_conv.go b/auto_conv.go new file mode 100644 index 0000000..10170c9 --- /dev/null +++ b/auto_conv.go @@ -0,0 +1,26 @@ +package convx + +import ( + "errors" +) + +func AutoConv(toType string, target interface{}) (interface{}, error) { + + var v interface{} + err := errors.New("not support type") + switch toType { + case "float64": + v, err = ToFloat64(target) + case "float32": + v, err = ToFloat32(target) + case "int": + v, err = ToInt(target) + case "int64": + v, err = ToInt64(target) + case "int32": + v, err = ToInt32(target) + case "string": + v, err = ToString(target) + } + return v, err +} diff --git a/convx_test.go b/convx_test.go new file mode 100644 index 0000000..28b7772 --- /dev/null +++ b/convx_test.go @@ -0,0 +1,172 @@ +package convx_test + +import ( + "testing" + + "code.yun.ink/open/utils/convx" +) + +func TestConvToString(t *testing.T) { + // 日志 + // t.Log("hello world") + + // 测试int + var v_int int = 123456789 + str, err := convx.ToString(v_int) + if err != nil { + t.Fail() + t.Log(err) + } + if str != "123456789" { + t.Fail() + t.Log(str) + } + + // 测试int32 + var v_int32 int32 = 123456789 + str, err = convx.ToString(v_int32) + if err != nil { + t.Fail() + t.Log(err) + } + if str != "123456789" { + t.Fail() + t.Log(str) + } + + // 测试int64 + var v_int64 int64 = 123456789 + str, err = convx.ToString(v_int64) + if err != nil { + t.Fail() + t.Log(err) + } + if str != "123456789" { + t.Fail() + t.Log(str) + } + + // 测试float32 + var v_float32 float32 = 123.12345 + str, err = convx.ToString(v_float32) + if err != nil { + t.Fail() + t.Log(err) + } + if str != "123.12345" { + t.Fail() + t.Log(str) + } + + // 测试float64 + var v_float64 float64 = 123456789.12345 + str, err = convx.ToString(v_float64) + if err != nil { + t.Fail() + t.Log(err) + } + if str != "123456789.12345" { + t.Fail() + t.Log(str) + } + + // 测试string + var v_string string = "123456789.12345" + str, err = convx.ToString(v_string) + if err != nil { + t.Fail() + t.Log(err) + } + if str != "123456789.12345" { + t.Fail() + t.Log(str) + } + + // 标记错误(继续运行) + // t.Fail() + + // 终止运行 + // t.FailNow() + +} + +func TestConvToInt(t *testing.T) { + // int + var v_int int = 123456789 + i, err := convx.ToInt(v_int) + if err != nil { + t.Fail() + t.Log(err) + } + if i != 123456789 { + t.Fail() + t.Log(i) + } + + // int32 + var v_int32 int32 = 123456789 + i, err = convx.ToInt(v_int32) + if err != nil { + t.Fail() + t.Log(err) + } + if i != 123456789 { + t.Fail() + t.Log(i) + } + + // int64 + var v_int64 int64 = 123456789 + i, err = convx.ToInt(v_int64) + if err != nil { + t.Fail() + t.Log(err) + } + if i != 123456789 { + t.Fail() + t.Log(i) + } + + // float32 + var v_float32 float32 = 1234.56789 + i, err = convx.ToInt(v_float32) + if err != nil { + t.Fail() + t.Log(err) + } + if i != 1234 { + t.Fail() + t.Log(i) + } + + // float64 + var v_float64 float64 = 1234.56789 + i, err = convx.ToInt(v_float64) + if err != nil { + t.Fail() + t.Log(err) + } + if i != 1234 { + t.Fail() + t.Log(i) + } + + // string + var v_string string = "1234" + i, err = convx.ToInt(v_string) + if err != nil { + t.Fail() + t.Log(err) + } + if i != 1234 { + t.Fail() + t.Log(i) + } + +} + +func TestConvToInt64(t *testing.T) {} + +func TestConvToFloat32(t *testing.T) {} + +func TestConvToFloat64(t *testing.T) {} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..970656c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module code.yun.ink/pkg/convx + +go 1.19 diff --git a/to_float32.go b/to_float32.go new file mode 100644 index 0000000..e9f80e3 --- /dev/null +++ b/to_float32.go @@ -0,0 +1,50 @@ +package convx + +import ( + "errors" + "strconv" +) + +// interface 转 float32 +func ToFloat32(val interface{}) (f float32, err error) { + if v, ok := val.(string); ok { + ff, err := strconv.ParseFloat(v, 32) + if err != nil { + return 0, err + } + f = float32(ff) + } else if v, ok := val.(float32); ok { + return v, nil + } else if v, ok := val.(float64); ok { + return float32(v), nil + } else if v, ok := val.(int); ok { + return float32(v), nil + } else if v, ok := val.(int8); ok { + return float32(v), nil + } else if v, ok := val.(int16); ok { + return float32(v), nil + } else if v, ok := val.(int32); ok { + return float32(v), nil + } else if v, ok := val.(int64); ok { + return float32(v), nil + } else if v, ok := val.(uint); ok { + return float32(v), nil + } else if v, ok := val.(uint8); ok { + return float32(v), nil + } else if v, ok := val.(uint16); ok { + return float32(v), nil + } else if v, ok := val.(uint32); ok { + return float32(v), nil + } else if v, ok := val.(uint64); ok { + return float32(v), nil + } else if v, ok := val.(bool); ok { + if v { + return 1, nil + } else { + return 0, nil + } + } else { + return 0, errors.New("类型转换失败") + } + return +} diff --git a/to_float64.go b/to_float64.go new file mode 100644 index 0000000..626d5ca --- /dev/null +++ b/to_float64.go @@ -0,0 +1,47 @@ +package convx + +import ( + "strconv" + + "errors" +) + +// interface 转 float64 +func ToFloat64(val interface{}) (f float64, err error) { + if v, ok := val.(string); ok { + f, err = strconv.ParseFloat(v, 64) + } else if v, ok := val.(float64); ok { + return v, nil + } else if v, ok := val.(float32); ok { + return float64(v), nil + } else if v, ok := val.(int); ok { + return float64(v), nil + } else if v, ok := val.(int8); ok { + return float64(v), nil + } else if v, ok := val.(int16); ok { + return float64(v), nil + } else if v, ok := val.(int32); ok { + return float64(v), nil + } else if v, ok := val.(int64); ok { + return float64(v), nil + } else if v, ok := val.(uint); ok { + return float64(v), nil + } else if v, ok := val.(uint8); ok { + return float64(v), nil + } else if v, ok := val.(uint16); ok { + return float64(v), nil + } else if v, ok := val.(uint32); ok { + return float64(v), nil + } else if v, ok := val.(uint64); ok { + return float64(v), nil + } else if v, ok := val.(bool); ok { + if v { + return 1, nil + } else { + return 0, nil + } + } else { + return 0, errors.New("类型转换失败") + } + return +} diff --git a/to_int.go b/to_int.go new file mode 100644 index 0000000..98a247d --- /dev/null +++ b/to_int.go @@ -0,0 +1,40 @@ +package convx + +import ( + "errors" + "strconv" +) + +// interface 转 int +func ToInt(val interface{}) (i int, err error) { + + if v, ok := val.(string); ok { + // 不支持小数转换 + i, err = strconv.Atoi(v) + } else if v, ok := val.(float32); ok { + i = int(v) + } else if v, ok := val.(float64); ok { + i = int(v) + } else if v, ok := val.(int); ok { + i = v + } else if v, ok := val.(int32); ok { + i = int(v) + } else if v, ok := val.(int64); ok { + i = int(v) + } else if v, ok := val.(uint); ok { + i = int(v) + } else if v, ok := val.(uint32); ok { + i = int(v) + } else if v, ok := val.(uint64); ok { + i = int(v) + } else if v, ok := val.(bool); ok { + if v { + i = 1 + } else { + i = 0 + } + } else { + return 0, errors.New("类型转换失败") + } + return +} diff --git a/to_int32.go b/to_int32.go new file mode 100644 index 0000000..71e0094 --- /dev/null +++ b/to_int32.go @@ -0,0 +1,7 @@ +package convx + +func ToInt32(val interface{}) (i int32, err error) { + ii, err := ToInt(val) + i = int32(ii) + return +} diff --git a/to_int64.go b/to_int64.go new file mode 100644 index 0000000..e81c88d --- /dev/null +++ b/to_int64.go @@ -0,0 +1,41 @@ +package convx + +import ( + "errors" + "strconv" +) + +// interface 转 int64 +func ToInt64(val interface{}) (i int64, err error) { + + if v, ok := val.(int64); ok { + i = v + } else if v, ok := val.(int32); ok { + i = int64(v) + } else if v, ok := val.(string); ok { + // string 转 int64 + // 第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64 + i, err = strconv.ParseInt(v, 10, 64) + } else if v, ok := val.(float64); ok { + i = int64(v) + } else if v, ok := val.(float32); ok { + i = int64(v) + } else if v, ok := val.(int); ok { + i = int64(v) + } else if v, ok := val.(uint); ok { + i = int64(v) + } else if v, ok := val.(uint32); ok { + i = int64(v) + } else if v, ok := val.(uint64); ok { + i = int64(v) + } else if v, ok := val.(bool); ok { + if v { + i = 1 + } else { + i = 0 + } + } else { + err = errors.New("不支持的参数类型") + } + return +} diff --git a/to_string.go b/to_string.go new file mode 100644 index 0000000..a545eda --- /dev/null +++ b/to_string.go @@ -0,0 +1,44 @@ +package convx + +import ( + "errors" + "strconv" +) + +// interface 转 string +func ToString(val interface{}) (str string, err error) { + + var s string + if vv, ok := val.(float32); ok { + s = strconv.FormatFloat(float64(vv), 'f', -1, 32) + } else if vv, ok := val.(float64); ok { + s = strconv.FormatFloat(vv, 'f', -1, 64) + } else if vv, ok := val.(int); ok { + s = strconv.Itoa(vv) + } else if vv, ok := val.(int32); ok { + s = strconv.Itoa(int(vv)) + } else if vv, ok := val.(int64); ok { + s = strconv.FormatInt(vv, 10) + } else if vv, ok := val.(string); ok { + s = vv + } else if vv, ok := val.(bool); ok { + s = strconv.FormatBool(vv) + } else if vv, ok := val.(uint); ok { + s = strconv.FormatUint(uint64(vv), 10) + } else if vv, ok := val.(uint32); ok { + s = strconv.FormatUint(uint64(vv), 10) + } else if vv, ok := val.(uint64); ok { + s = strconv.FormatUint(vv, 10) + } else if vv, ok := val.(uint8); ok { + s = strconv.FormatUint(uint64(vv), 10) + } else if vv, ok := val.(uint16); ok { + s = strconv.FormatUint(uint64(vv), 10) + } else if vv, ok := val.(int8); ok { + s = strconv.FormatInt(int64(vv), 10) + } else if vv, ok := val.(int16); ok { + s = strconv.FormatInt(int64(vv), 10) + } else { + return s, errors.New("不支持的参数类型") + } + return s, nil +}