提交
This commit is contained in:
+4
-2
@@ -7,6 +7,9 @@ import (
|
||||
|
||||
// interface 转 int64
|
||||
func ToInt64(val interface{}) (i int64, err error) {
|
||||
if val == nil {
|
||||
return 0, errors.New("value is nil")
|
||||
}
|
||||
|
||||
switch v := val.(type) {
|
||||
case int:
|
||||
@@ -33,8 +36,6 @@ func ToInt64(val interface{}) (i int64, err error) {
|
||||
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:
|
||||
@@ -46,3 +47,4 @@ func ToInt64(val interface{}) (i int64, err error) {
|
||||
}
|
||||
return 0, errors.New("convx.ToInt64: unknown type")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
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},
|
||||
{123.456, 123, false},
|
||||
{true, 1, false},
|
||||
{"abc", 0, true}, //
|
||||
{nil, 0, true},
|
||||
{123, 123, false},
|
||||
{int32(123), 123, false},
|
||||
{uint64(123), 123, false},
|
||||
{int64(123), 123, false},
|
||||
{float32(123.456), 123, false},
|
||||
{float64(123.456), 123, false},
|
||||
{"-123", -123, false},
|
||||
{"123.456", 123, false}, // TODO: should be 123
|
||||
{"170141183460469231731687303715884105727", 9223372036854775807, false}, // max int64
|
||||
{"-170141183460469231731687303715884105728", -9223372036854775808, false}, // min int64
|
||||
{"9223372036854775807", 9223372036854775807, false}, // max int64
|
||||
{"-9223372036854775808", -9223372036854775808, false}, // min int64
|
||||
{"12345678901234567890", 0, false}, // too large
|
||||
|
||||
}
|
||||
for _, test := range tests {
|
||||
output, err := convx.ToInt64(test.input)
|
||||
if err != nil {
|
||||
t.Error("ToInt64(", test.input, ") should not return error")
|
||||
}
|
||||
if output != test.output {
|
||||
t.Error("ToInt64(", test.input, ") should be", test.output, "but was", output)
|
||||
}
|
||||
}
|
||||
}
|
||||
+46
-46
@@ -1,46 +1,46 @@
|
||||
package convx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// interface 转 string
|
||||
func ToString(val interface{}) (str string, err error) {
|
||||
|
||||
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 "", errors.New("can not convert to string")
|
||||
}
|
||||
package convx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// interface 转 string
|
||||
func ToString(val interface{}) (str string, err error) {
|
||||
|
||||
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 "", errors.New("can not convert to string")
|
||||
}
|
||||
|
||||
+28
-68
@@ -7,75 +7,35 @@ import (
|
||||
)
|
||||
|
||||
func TestToString(t *testing.T) {
|
||||
a := string("a")
|
||||
str, err := convx.ToString(a)
|
||||
if str != a {
|
||||
t.Error("convx.ToString(a) error", err)
|
||||
|
||||
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
|
||||
}
|
||||
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)
|
||||
|
||||
for _, test := range tests {
|
||||
str, err := convx.ToString(test.input)
|
||||
if err != nil {
|
||||
t.Errorf("convx.ToString(%v) failed with %v", test.input, err)
|
||||
}
|
||||
if str != test.expect {
|
||||
t.Errorf("convx.ToString(%v) = %v, want %v", test.input, str, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user