优化类型的定义
This commit is contained in:
+28
@@ -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
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
+33
-26
@@ -8,34 +8,41 @@ import (
|
||||
// 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 {
|
||||
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 nil:
|
||||
return 0, nil
|
||||
case string:
|
||||
return strconv.ParseInt(v, 10, 64)
|
||||
case bool:
|
||||
if v {
|
||||
i = 1
|
||||
return 1, nil
|
||||
} else {
|
||||
i = 0
|
||||
return 0, nil
|
||||
}
|
||||
} else {
|
||||
err = errors.New("不支持的参数类型")
|
||||
}
|
||||
return
|
||||
return 0, errors.New("convx.ToInt64: unknown type")
|
||||
}
|
||||
|
||||
+34
-32
@@ -2,43 +2,45 @@ package convx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"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("不支持的参数类型")
|
||||
switch v := val.(type) {
|
||||
case nil:
|
||||
return "", fmt.Errorf("can not convert to string")
|
||||
case string:
|
||||
return v, nil
|
||||
case int:
|
||||
return strconv.Itoa(v), nil
|
||||
case int8:
|
||||
return strconv.FormatInt(int64(v), 10), nil
|
||||
case int16:
|
||||
return strconv.FormatInt(int64(v), 10), nil
|
||||
case int32:
|
||||
return strconv.FormatInt(int64(v), 10), nil
|
||||
case int64:
|
||||
return strconv.FormatInt(v, 10), nil
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'f', -1, 64), nil
|
||||
case bool:
|
||||
return strconv.FormatBool(v), nil
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(v), 10), nil
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(v), 10), nil
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(v), 10), nil
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(v), 10), nil
|
||||
case uint64:
|
||||
return strconv.FormatUint(v, 10), nil
|
||||
}
|
||||
return s, nil
|
||||
|
||||
return "", errors.New("can not convert to string")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user