1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 11:12:28 +08:00

refactor: reduce gocyclo of ToString func

This commit is contained in:
dudaodong
2022-01-03 15:15:22 +08:00
parent 7a9b0847f9
commit 56fc12c660
2 changed files with 26 additions and 31 deletions

View File

@@ -45,44 +45,34 @@ func ToChar(s string) []string {
// ToString convert value to string // ToString convert value to string
func ToString(value interface{}) string { func ToString(value interface{}) string {
var res string res := ""
if value == nil { if value == nil {
return res return res
} }
switch v := value.(type) {
case float64: v := reflect.ValueOf(value)
res = strconv.FormatFloat(v, 'f', -1, 64)
case float32: switch value.(type) {
res = strconv.FormatFloat(float64(v), 'f', -1, 64) case float32, float64:
case int: res = strconv.FormatFloat(v.Float(), 'f', -1, 64)
res = strconv.Itoa(v) return res
case uint: case int, int8, int16, int32, int64:
res = strconv.Itoa(int(v)) res = strconv.FormatInt(v.Int(), 10)
case int8: return res
res = strconv.Itoa(int(v)) case uint, uint8, uint16, uint32, uint64:
case uint8: res = strconv.FormatUint(v.Uint(), 10)
res = strconv.Itoa(int(v)) return res
case int16:
res = strconv.Itoa(int(v))
case uint16:
res = strconv.Itoa(int(v))
case int32:
res = strconv.Itoa(int(v))
case uint32:
res = strconv.Itoa(int(v))
case int64:
res = strconv.FormatInt(v, 10)
case uint64:
res = strconv.FormatUint(v, 10)
case string: case string:
res = value.(string) res = v.String()
return res
case []byte: case []byte:
res = string(value.([]byte)) res = string(v.Bytes())
return res
default: default:
newValue, _ := json.Marshal(value) newValue, _ := json.Marshal(value)
res = string(newValue) res = string(newValue)
return res
} }
return res
} }
// ToJson convert value to a valid json string // ToJson convert value to a valid json string

View File

@@ -108,14 +108,19 @@ func TestToString(t *testing.T) {
aStruct := TestStruct{Name: "TestStruct"} aStruct := TestStruct{Name: "TestStruct"}
cases := []interface{}{ cases := []interface{}{
"", nil,
int(0), int8(1), int16(-1), int32(123), int64(123), int(0), int8(1), int16(-1), int32(123), int64(123),
uint(123), uint8(123), uint16(123), uint32(123), uint64(123), uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
float64(12.3), float32(12.3), float64(12.3), float32(12.3),
true, false, true, false,
[]int{1, 2, 3}, aMap, aStruct, []byte{104, 101, 108, 108, 111}} []int{1, 2, 3}, aMap, aStruct, []byte{104, 101, 108, 108, 111}}
expected := []string{"0", "1", "-1", "123", "123", "123", "123", "123", expected := []string{
"123", "123", "12.3", "12.300000190734863", "true", "false", "", "",
"0", "1", "-1",
"123", "123", "123", "123", "123", "123", "123",
"12.3", "12.300000190734863",
"true", "false",
"[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"} "[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {