优化类型的定义

This commit is contained in:
Yun
2024-10-28 20:02:37 +08:00
parent 832e70ba48
commit 1e41151c1d
6 changed files with 232 additions and 102 deletions
+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 // interface 转 float64
func ToFloat64(val interface{}) (f float64, err error) { func ToFloat64(val interface{}) (f float64, err error) {
if v, ok := val.(string); ok {
f, err = strconv.ParseFloat(v, 64) switch v := val.(type) {
} else if v, ok := val.(float64); ok { case nil:
return 0, errors.New("val is nil")
case float32:
return float64(v), nil
case float64:
return v, nil return v, nil
} else if v, ok := val.(float32); ok { case int:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(int); ok { case int8:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(int8); ok { case int16:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(int16); ok { case int32:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(int32); ok { case int64:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(int64); ok { case uint:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(uint); ok { case uint8:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(uint8); ok { case uint16:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(uint16); ok { case uint32:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(uint32); ok { case uint64:
return float64(v), nil return float64(v), nil
} else if v, ok := val.(uint64); ok { case string:
return float64(v), nil return strconv.ParseFloat(v, 64)
} else if v, ok := val.(bool); ok { case bool:
if v { if v {
return 1, nil return 1, nil
} else { } else {
return 0, nil return 0, nil
} }
} else { case []byte:
return 0, errors.New("类型转换失败") 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 // interface 转 int
func ToInt(val interface{}) (i int, err error) { func ToInt(val interface{}) (i int, err error) {
if v, ok := val.(string); ok { switch v := val.(type) {
// 不支持小数转换 case nil:
i, err = strconv.Atoi(v) return 0, nil
} else if v, ok := val.(float32); ok { case string:
i = int(v) return strconv.Atoi(v)
} else if v, ok := val.(float64); ok { case float32:
i = int(v) return int(v), nil
} else if v, ok := val.(int); ok { case float64:
i = v return int(v), nil
} else if v, ok := val.(int32); ok { case int:
i = int(v) return v, nil
} else if v, ok := val.(int64); ok { case int8:
i = int(v) return int(v), nil
} else if v, ok := val.(uint); ok { case int16:
i = int(v) return int(v), nil
} else if v, ok := val.(uint32); ok { case int32:
i = int(v) return int(v), nil
} else if v, ok := val.(uint64); ok { case int64:
i = int(v) return int(v), nil
} else if v, ok := val.(bool); ok { 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 { if v {
i = 1 return 1, nil
} else { } else {
i = 0 return 0, nil
} }
} else {
return 0, errors.New("类型转换失败")
} }
return
return 0, errors.New("can not convert to int")
} }
+33 -26
View File
@@ -8,34 +8,41 @@ import (
// interface 转 int64 // interface 转 int64
func ToInt64(val interface{}) (i int64, err error) { func ToInt64(val interface{}) (i int64, err error) {
if v, ok := val.(int64); ok { switch v := val.(type) {
i = v case int:
} else if v, ok := val.(int32); ok { return int64(v), nil
i = int64(v) case int8:
} else if v, ok := val.(string); ok { return int64(v), nil
// string 转 int64 case int16:
// 第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64 return int64(v), nil
i, err = strconv.ParseInt(v, 10, 64) case int32:
} else if v, ok := val.(float64); ok { return int64(v), nil
i = int64(v) case int64:
} else if v, ok := val.(float32); ok { return v, nil
i = int64(v) case uint:
} else if v, ok := val.(int); ok { return int64(v), nil
i = int64(v) case uint8:
} else if v, ok := val.(uint); ok { return int64(v), nil
i = int64(v) case uint16:
} else if v, ok := val.(uint32); ok { return int64(v), nil
i = int64(v) case uint32:
} else if v, ok := val.(uint64); ok { return int64(v), nil
i = int64(v) case uint64:
} else if v, ok := val.(bool); ok { return int64(v), nil
case float32:
return int64(v), nil
case float64:
return int64(v), nil
case nil:
return 0, nil
case string:
return strconv.ParseInt(v, 10, 64)
case bool:
if v { if v {
i = 1 return 1, nil
} else { } else {
i = 0 return 0, nil
} }
} else {
err = errors.New("不支持的参数类型")
} }
return return 0, errors.New("convx.ToInt64: unknown type")
} }
+34 -32
View File
@@ -2,43 +2,45 @@ package convx
import ( import (
"errors" "errors"
"fmt"
"strconv" "strconv"
) )
// interface 转 string // interface 转 string
func ToString(val interface{}) (str string, err error) { func ToString(val interface{}) (str string, err error) {
var s string switch v := val.(type) {
if vv, ok := val.(float32); ok { case nil:
s = strconv.FormatFloat(float64(vv), 'f', -1, 32) return "", fmt.Errorf("can not convert to string")
} else if vv, ok := val.(float64); ok { case string:
s = strconv.FormatFloat(vv, 'f', -1, 64) return v, nil
} else if vv, ok := val.(int); ok { case int:
s = strconv.Itoa(vv) return strconv.Itoa(v), nil
} else if vv, ok := val.(int32); ok { case int8:
s = strconv.Itoa(int(vv)) return strconv.FormatInt(int64(v), 10), nil
} else if vv, ok := val.(int64); ok { case int16:
s = strconv.FormatInt(vv, 10) return strconv.FormatInt(int64(v), 10), nil
} else if vv, ok := val.(string); ok { case int32:
s = vv return strconv.FormatInt(int64(v), 10), nil
} else if vv, ok := val.(bool); ok { case int64:
s = strconv.FormatBool(vv) return strconv.FormatInt(v, 10), nil
} else if vv, ok := val.(uint); ok { case float32:
s = strconv.FormatUint(uint64(vv), 10) return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
} else if vv, ok := val.(uint32); ok { case float64:
s = strconv.FormatUint(uint64(vv), 10) return strconv.FormatFloat(v, 'f', -1, 64), nil
} else if vv, ok := val.(uint64); ok { case bool:
s = strconv.FormatUint(vv, 10) return strconv.FormatBool(v), nil
} else if vv, ok := val.(uint8); ok { case uint:
s = strconv.FormatUint(uint64(vv), 10) return strconv.FormatUint(uint64(v), 10), nil
} else if vv, ok := val.(uint16); ok { case uint8:
s = strconv.FormatUint(uint64(vv), 10) return strconv.FormatUint(uint64(v), 10), nil
} else if vv, ok := val.(int8); ok { case uint16:
s = strconv.FormatInt(int64(vv), 10) return strconv.FormatUint(uint64(v), 10), nil
} else if vv, ok := val.(int16); ok { case uint32:
s = strconv.FormatInt(int64(vv), 10) return strconv.FormatUint(uint64(v), 10), nil
} else { case uint64:
return s, errors.New("不支持的参数类型") return strconv.FormatUint(v, 10), nil
} }
return s, nil
return "", errors.New("can not convert to string")
} }
+81
View File
@@ -0,0 +1,81 @@
package convx_test
import (
"testing"
"code.yun.ink/pkg/convx"
)
func TestToString(t *testing.T) {
a := string("a")
str, err := convx.ToString(a)
if str != a {
t.Error("convx.ToString(a) error", err)
}
b := int(1)
str, err = convx.ToString(b)
if str != "1" {
t.Error("convx.ToString(b) error", err)
}
c := int8(1)
str, err = convx.ToString(c)
if str != "1" {
t.Error("convx.ToString(c) error", err)
}
d := int16(-1)
str, err = convx.ToString(d)
if str != "-1" {
t.Error("convx.ToString(d) error", err)
}
e := int32(1)
str, err = convx.ToString(e)
if str != "1" {
t.Error("convx.ToString(e) error", err)
}
f := int64(1)
str, err = convx.ToString(f)
if str != "1" {
t.Error("convx.ToString(f) error", err)
}
g := float32(1.1)
str, err = convx.ToString(g)
if str != "1.1" {
t.Error("convx.ToString(g) error", err)
}
h := float64(1.1)
str, err = convx.ToString(h)
if str != "1.1" {
t.Error("convx.ToString(h) error", err)
}
i := true
str, err = convx.ToString(i)
if str != "true" {
t.Error("convx.ToString(i) error", err)
}
j := uint(1)
str, err = convx.ToString(j)
if str != "1" {
t.Error("convx.ToString(j) error", err)
}
k := uint8(1)
str, err = convx.ToString(k)
if str != "1" {
t.Error("convx.ToString(k) error", err)
}
l := uint16(1)
str, err = convx.ToString(l)
if str != "1" {
t.Error("convx.ToString(l) error", err)
}
m := uint32(1)
str, err = convx.ToString(m)
if str != "1" {
t.Error("convx.ToString(m) error", err)
}
n := uint64(1)
str, err = convx.ToString(n)
if str != "1" {
t.Error("convx.ToString(n) error", err)
}
}