refactor: code review

This commit is contained in:
meilin.huang
2023-10-20 21:31:46 +08:00
parent 45d2449221
commit 10f6b03fb5
15 changed files with 270 additions and 209 deletions

View File

@@ -1,6 +1,7 @@
package anyx
import (
"encoding/json"
"reflect"
"strconv"
)
@@ -75,3 +76,47 @@ func IsBlank(value any) bool {
}
return reflect.DeepEqual(rValue.Interface(), reflect.Zero(rValue.Type()).Interface())
}
// any to string
func ToString(value any) string {
// interface 转 string
if value == nil {
return ""
}
switch it := value.(type) {
case string:
return it
case error:
return it.Error()
case float64:
return strconv.FormatFloat(it, 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(it), 'f', -1, 64)
case int:
return strconv.Itoa(it)
case uint:
return strconv.Itoa(int(it))
case int8:
return strconv.Itoa(int(it))
case uint8:
return strconv.Itoa(int(it))
case int16:
return strconv.Itoa(int(it))
case uint16:
return strconv.Itoa(int(it))
case int32:
return strconv.Itoa(int(it))
case uint32:
return strconv.Itoa(int(it))
case int64:
return strconv.FormatInt(it, 10)
case uint64:
return strconv.FormatUint(it, 10)
case []byte:
return string(value.([]byte))
default:
newValue, _ := json.Marshal(value)
return string(newValue)
}
}