11 Commits

Author SHA1 Message Date
Yun 15e1ed41f4 优化any转string 2025-09-02 15:43:26 +08:00
yun ab8524e5ef 优化64进制转换 2024-10-28 21:28:58 +08:00
yun 7a1b81ebf2 优化int64的转换 2024-10-28 21:07:49 +08:00
yun 1be7f79ad3 提交 2024-10-28 20:33:26 +08:00
yun 1e41151c1d 优化类型的定义 2024-10-28 20:02:37 +08:00
Yun 594aae76c6 转int64和转string优化 2024-10-27 22:19:20 +08:00
yun 0baef759a0 优化ToString 2024-08-15 02:43:22 +00:00
yun 832e70ba48 添加部分测试 2024-01-11 21:48:44 +08:00
yun 97f9a0cb86 提交 2024-01-11 21:40:17 +08:00
yun c0cd260a86 添加进制转换 2024-01-11 21:36:23 +08:00
yun 659840adca 提交 2023-12-03 21:22:05 +08:00
13 changed files with 627 additions and 152 deletions
+52
View File
@@ -0,0 +1,52 @@
package convx
import (
"errors"
"fmt"
"strings"
)
// 说明
// 10进制的数字转换为64进制的字符串,用于生成短链接
// 64进制的字符集,可以自定义
const base64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#*"
// DecToBase64 将10进制的数字转换为64进制的字符串
func Encode10To64(n int64) (string, error) {
if n < 0 {
return "", fmt.Errorf("n must be positive")
}
if n == 0 {
return "0", nil
}
s := ""
for n > 0 {
// 取余数,作为64进制的一位
r := n % 64
// 从字符集中找到对应的字符
c := string(base64[r])
// 拼接到结果字符串的前面
s = c + s
// 除以64,继续下一轮循环
n = n / 64
}
return s, nil
}
// Base64ToDec 将64进制的字符串转换为10进制的数字
func Decode64To10(s string) (int64, error) {
n := int64(0)
// 遍历字符串的每个字符
for _, c := range s {
// 在字符集中找到字符的索引,作为64进制的一位
i := strings.Index(base64, string(c))
if i == -1 {
return 0, errors.New("invalid character: " + string(c))
}
// 累加到结果数字中,每次乘以64
n = n*64 + int64(i)
}
return n, nil
}
+31
View File
@@ -0,0 +1,31 @@
package convx_test
import (
"fmt"
"math"
"testing"
"code.yun.ink/pkg/convx"
)
func Test10to64(t *testing.T) {
var tests = []struct {
in int64
}{
{math.MaxInt64},
{-987654321},
// {-1234567890},
// {-987654321},
{0},
}
for _, test := range tests {
s, err := convx.Encode10To64(test.in)
fmt.Println(test.in, s, err)
n, err := convx.Decode64To10(s)
fmt.Println(n, err)
if n != test.in {
t.Errorf("10to64: %d != %d", n, test.in)
}
}
}
+1 -1
View File
@@ -20,7 +20,7 @@ func AutoConv(toType string, target interface{}) (interface{}, error) {
case "int32":
v, err = ToInt32(target)
case "string":
v, err = ToString(target)
v = ToString(target)
}
return v, err
}
+196
View File
@@ -0,0 +1,196 @@
package main
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
)
// package main
// import (
// "testing"
// "time"
// )
// func TestInterfaceToString(t *testing.T) {
// tests := []struct {
// name string
// input interface{}
// want string
// }{
// {"nil", nil, ""},
// {"string", "hello", "hello"},
// {"bytes", []byte("world"), "world"},
// {"custom stringer", customStringer{}, "custom stringer implementation"},
// {"error", fmt.Errorf("test error"), "test error"},
// {"bool true", true, "true"},
// {"bool false", false, "false"},
// {"int", 42, "42"},
// {"int8", int8(8), "8"},
// {"int16", int16(16), "16"},
// {"int32", int32(32), "32"},
// {"int64", int64(64), "64"},
// {"uint", uint(42), "42"},
// {"uint8", uint8(8), "8"},
// {"uint16", uint16(16), "16"},
// {"uint32", uint32(32), "32"},
// {"uint64", uint64(64), "64"},
// {"float32", float32(3.14), "3.14"},
// {"float64", 3.1415, "3.1415"},
// {"complex64", complex64(1 + 2i), "(1+2i)"},
// {"complex128", 1 + 3i, "(1+3i)"},
// {"time", time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), "2020-01-01T00:00:00Z"},
// {"duration", time.Hour, "1h0m0s"},
// {"slice", []int{1, 2, 3}, "[1,2,3]"},
// {"array", [2]string{"a", "b"}, `["a","b"]`},
// {"map", map[string]int{"a": 1}, `{"a":1}`},
// {"struct", struct{ A int }{42}, `{"A":42}`},
// {"pointer to int", intPtr(42), "42"},
// {"pointer to nil", (*int)(nil), ""},
// {"custom type", TestStr("custom"), "custom"},
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// if got := InterfaceToString(tt.input); got != tt.want {
// t.Errorf("InterfaceToString() = %v, want %v", got, tt.want)
// }
// })
// }
// }
// func intPtr(i int) *int {
// return &i
// }
// InterfaceToString 将任意interface转换为string
func InterfaceToString(v interface{}) string {
if v == nil {
return ""
}
// 获取值的反射对象
val := reflect.ValueOf(v)
// 处理指针类型:解引用直到获取到非指针值
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return ""
}
val = val.Elem()
}
// 获取解引用后的实际值
actualValue := val.Interface()
// 根据具体类型进行处理
switch actual := actualValue.(type) {
case string:
return actual
case []byte:
return string(actual)
case fmt.Stringer:
return actual.String()
case error:
return actual.Error()
case bool:
return strconv.FormatBool(actual)
case int:
return strconv.Itoa(actual)
case int8:
return strconv.FormatInt(int64(actual), 10)
case int16:
return strconv.FormatInt(int64(actual), 10)
case int32:
return strconv.FormatInt(int64(actual), 10)
case int64:
return strconv.FormatInt(actual, 10)
case uint:
return strconv.FormatUint(uint64(actual), 10)
case uint8:
return strconv.FormatUint(uint64(actual), 10)
case uint16:
return strconv.FormatUint(uint64(actual), 10)
case uint32:
return strconv.FormatUint(uint64(actual), 10)
case uint64:
return strconv.FormatUint(actual, 10)
case float32:
return strconv.FormatFloat(float64(actual), 'f', -1, 32)
case float64:
return strconv.FormatFloat(actual, 'f', -1, 64)
case complex64:
return fmt.Sprint(actual)
case complex128:
return fmt.Sprint(actual)
case time.Time:
return actual.Format(time.RFC3339)
case time.Duration:
return actual.String()
}
// 处理切片、数组、map、结构体等复杂类型 - 使用JSON序列化
if val.IsValid() {
switch val.Kind() {
case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct:
jsonBytes, err := json.Marshal(actualValue)
if err == nil {
return string(jsonBytes)
}
}
}
// 默认处理:使用fmt.Sprint
return fmt.Sprint(actualValue)
}
// 示例用法
func main() {
// 测试各种类型
testCases := []interface{}{
nil,
"hello",
42,
3.14,
true,
[]byte("world"),
[]int{1, 2, 3},
map[string]interface{}{"name": "John", "age": 30},
struct {
Name string `json:"name"`
Age int `json:"age"`
}{"Alice", 25},
time.Now(),
time.Hour * 2,
// 指针类型
&[]string{"a", "b", "c"},
// 自定义Stringer
customStringer{},
// 错误类型
fmt.Errorf("test error"),
TestStrOne,
2454528425152485425,
}
for i, tc := range testCases {
result := InterfaceToString(tc)
fmt.Printf("Case %d: %T -> %s\n", i+1, tc, result)
}
}
// 自定义Stringer实现
type customStringer struct{}
func (c customStringer) String() string {
return "custom stringer implementation"
}
type TestStr string
const (
TestStrOne TestStr = "1"
)
+31 -31
View File
@@ -3,20 +3,35 @@ package convx_test
import (
"testing"
"code.yun.ink/open/utils/convx"
"code.yun.ink/pkg/convx"
)
func Test64(t *testing.T) {
// 测试10进制转64进制
var v_int int64 = 123456789
str,_ := convx.Encode10To64(v_int)
if str != "7MyqL" {
t.Fail()
t.Log(str)
}
// 测试64进制转10进制
var v_string string = "7MyqL"
i,_ := convx.Decode64To10(v_string)
if i != 123456789 {
t.Fail()
t.Log(i)
}
}
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)
}
str := convx.ToString(v_int)
if str != "123456789" {
t.Fail()
t.Log(str)
@@ -24,11 +39,8 @@ func TestConvToString(t *testing.T) {
// 测试int32
var v_int32 int32 = 123456789
str, err = convx.ToString(v_int32)
if err != nil {
t.Fail()
t.Log(err)
}
str = convx.ToString(v_int32)
if str != "123456789" {
t.Fail()
t.Log(str)
@@ -36,11 +48,8 @@ func TestConvToString(t *testing.T) {
// 测试int64
var v_int64 int64 = 123456789
str, err = convx.ToString(v_int64)
if err != nil {
t.Fail()
t.Log(err)
}
str = convx.ToString(v_int64)
if str != "123456789" {
t.Fail()
t.Log(str)
@@ -48,11 +57,8 @@ func TestConvToString(t *testing.T) {
// 测试float32
var v_float32 float32 = 123.12345
str, err = convx.ToString(v_float32)
if err != nil {
t.Fail()
t.Log(err)
}
str = convx.ToString(v_float32)
if str != "123.12345" {
t.Fail()
t.Log(str)
@@ -60,11 +66,8 @@ func TestConvToString(t *testing.T) {
// 测试float64
var v_float64 float64 = 123456789.12345
str, err = convx.ToString(v_float64)
if err != nil {
t.Fail()
t.Log(err)
}
str = convx.ToString(v_float64)
if str != "123456789.12345" {
t.Fail()
t.Log(str)
@@ -72,11 +75,8 @@ func TestConvToString(t *testing.T) {
// 测试string
var v_string string = "123456789.12345"
str, err = convx.ToString(v_string)
if err != nil {
t.Fail()
t.Log(err)
}
str = convx.ToString(v_string)
if str != "123456789.12345" {
t.Fail()
t.Log(str)
+1 -1
View File
@@ -1,3 +1,3 @@
module code.yun.ink/pkg/convx
go 1.19
go 1.20
+28
View File
@@ -0,0 +1,28 @@
package convx
import (
"fmt"
"strconv"
)
// ToBool converts v to bool.
func ToBool(v interface{}) (bool, error) {
switch v := v.(type) {
case bool:
return v, nil
case string:
if v, err := strconv.ParseBool(v); err == nil {
return v, nil
} else {
return false, err
}
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
i, err := ToInt(v)
if err != nil {
return false, err
}
return i > 0, nil
default:
return false, fmt.Errorf("unsupported type %T", v)
}
}
+24 -19
View File
@@ -8,40 +8,45 @@ import (
// 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 {
switch v := val.(type) {
case nil:
return 0, errors.New("val is nil")
case float32:
return float64(v), nil
case float64:
return v, nil
} else if v, ok := val.(float32); ok {
case int:
return float64(v), nil
} else if v, ok := val.(int); ok {
case int8:
return float64(v), nil
} else if v, ok := val.(int8); ok {
case int16:
return float64(v), nil
} else if v, ok := val.(int16); ok {
case int32:
return float64(v), nil
} else if v, ok := val.(int32); ok {
case int64:
return float64(v), nil
} else if v, ok := val.(int64); ok {
case uint:
return float64(v), nil
} else if v, ok := val.(uint); ok {
case uint8:
return float64(v), nil
} else if v, ok := val.(uint8); ok {
case uint16:
return float64(v), nil
} else if v, ok := val.(uint16); ok {
case uint32:
return float64(v), nil
} else if v, ok := val.(uint32); ok {
case uint64:
return float64(v), nil
} else if v, ok := val.(uint64); ok {
return float64(v), nil
} else if v, ok := val.(bool); ok {
case string:
return strconv.ParseFloat(v, 64)
case bool:
if v {
return 1, nil
} else {
return 0, nil
}
} else {
return 0, errors.New("类型转换失败")
case []byte:
return strconv.ParseFloat(string(v), 64)
}
return
return 0, errors.New("convx.ToFloat64: unknown type")
}
+32 -25
View File
@@ -8,33 +8,40 @@ import (
// 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 {
switch v := val.(type) {
case nil:
return 0, nil
case string:
return strconv.Atoi(v)
case float32:
return int(v), nil
case float64:
return int(v), nil
case int:
return v, nil
case int8:
return int(v), nil
case int16:
return int(v), nil
case int32:
return int(v), nil
case int64:
return int(v), nil
case uint:
return int(v), nil
case uint8:
return int(v), nil
case uint32:
return int(v), nil
case uint64:
return int(v), nil
case bool:
if v {
i = 1
return 1, nil
} else {
i = 0
return 0, nil
}
} else {
return 0, errors.New("类型转换失败")
}
return
return 0, errors.New("can not convert to int")
}
+47 -31
View File
@@ -2,40 +2,56 @@ package convx
import (
"errors"
"strconv"
"math/big"
)
// 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("不支持的参数类型")
if val == nil {
return 0, errors.New("value is nil")
}
return
switch v := val.(type) {
case int:
return int64(v), nil
case int8:
return int64(v), nil
case int16:
return int64(v), nil
case int32:
return int64(v), nil
case int64:
return v, nil
case uint:
return int64(v), nil
case uint8:
return int64(v), nil
case uint16:
return int64(v), nil
case uint32:
return int64(v), nil
case uint64:
return int64(v), nil
case float32:
return int64(v), nil
case float64:
return int64(v), nil
case string:
num := new(big.Float)
_, ok := num.SetString(v)
if !ok {
return 0, errors.New("convx.ToInt64: out of range")
}
i, _ := num.Int64()
// fmt.Println(val, i, b)
return i, nil
case bool:
if v {
return 1, nil
} else {
return 0, nil
}
}
return 0, errors.New("convx.ToInt64: unknown type")
}
+48
View File
@@ -0,0 +1,48 @@
package convx_test
import (
"testing"
"code.yun.ink/pkg/convx"
)
func TestToInt64(t *testing.T) {
var tests = []struct {
input interface{}
output int64
isError bool
}{
{"123", 123, false}, // string
{123.456, 123, false}, // float64
{true, 1, false}, // bool
{"abc", 0, true}, // string
{nil, 0, true}, // nil
{123, 123, false}, // int
{int32(123), 123, false}, // int32
{uint64(123), 123, false}, // uint64
{int64(123), 123, false}, // int64
{float32(123.456), 123, false}, // float32
{float64(123.456), 123, false}, // float64
{"-123", -123, false}, // string
{"123.456", 123, false}, // string
{"170141183460469231731687303715884105727", 9223372036854775807, false}, // max int64
{"-170141183460469231731687303715884105728", -9223372036854775808, false}, // min int64
{"9223372036854775807", 9223372036854775807, false}, // max int64
{"-9223372036854775808", -9223372036854775808, false}, // min int64
{"12345678901234567890", 9223372036854775807, false}, // too large
}
for idx, test := range tests {
output, err := convx.ToInt64(test.input)
if test.isError && err == nil {
t.Error(idx, ": ToInt64(", test.input, ") should return error")
continue
} else if !test.isError && err != nil {
t.Error(idx, ": ToInt64(", test.input, ") should not return error", err.Error())
continue
}
if output != test.output {
t.Error("ToInt64(", test.input, ") should be", test.output, "but was", output)
}
}
}
+91 -44
View File
@@ -1,44 +1,91 @@
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
}
package convx
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
)
// interface 转 string
// InterfaceToString 将任意interface转换为string
func ToString(v interface{}) string {
if v == nil {
return ""
}
// 获取值的反射对象
val := reflect.ValueOf(v)
// 处理指针类型:解引用直到获取到非指针值
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return ""
}
val = val.Elem()
}
// 获取解引用后的实际值
actualValue := val.Interface()
// 根据具体类型进行处理
switch actual := actualValue.(type) {
case string:
return actual
case []byte:
return string(actual)
case error:
return actual.Error()
case bool:
return strconv.FormatBool(actual)
case int:
return strconv.Itoa(actual)
case int8:
return strconv.FormatInt(int64(actual), 10)
case int16:
return strconv.FormatInt(int64(actual), 10)
case int32:
return strconv.FormatInt(int64(actual), 10)
case int64:
return strconv.FormatInt(actual, 10)
case uint:
return strconv.FormatUint(uint64(actual), 10)
case uint8:
return strconv.FormatUint(uint64(actual), 10)
case uint16:
return strconv.FormatUint(uint64(actual), 10)
case uint32:
return strconv.FormatUint(uint64(actual), 10)
case uint64:
return strconv.FormatUint(actual, 10)
case float32:
return strconv.FormatFloat(float64(actual), 'f', -1, 32)
case float64:
return strconv.FormatFloat(actual, 'f', -1, 64)
case complex64:
return fmt.Sprint(actual)
case complex128:
return fmt.Sprint(actual)
case time.Time:
return actual.Format(time.RFC3339)
case time.Duration:
return actual.String()
case fmt.Stringer:
return actual.String()
}
// 处理切片、数组、map、结构体等复杂类型 - 使用JSON序列化
if val.IsValid() {
switch val.Kind() {
case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct:
jsonBytes, err := json.Marshal(actualValue)
if err == nil {
return string(jsonBytes)
}
}
}
// 默认处理:使用fmt.Sprint
return fmt.Sprint(actualValue)
}
+45
View File
@@ -0,0 +1,45 @@
package convx_test
import (
"testing"
"code.yun.ink/pkg/convx"
)
type TestStr string
const (
TestStrOne TestStr = "1"
)
func TestToString(t *testing.T) {
var tests = []struct {
input interface{}
expect string
}{
{"1", "1"}, //string
{1, "1"}, //int
{int8(1), "1"}, //int8
{int16(1), "1"}, //int16
{-1, "-1"}, //int32
{int64(1), "1"}, //int64
{float32(1.1), "1.1"}, //float32
{float64(1.1), "1.1"}, //float64
{true, "true"}, //bool
{uint(1), "1"}, //uint
{uint8(1), "1"}, //uint8
{uint16(1), "1"}, //uint16
{uint32(1), "1"}, //uint32
{uint64(1), "1"}, //uint64
{TestStrOne, "1"}, //custom type
}
for _, test := range tests {
str := convx.ToString(test.input)
if str != test.expect {
t.Errorf("convx.ToString(%v) = %v, want %v", test.input, str, test.expect)
}
}
}