1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 07:02:29 +08:00

refactor: interface{} -> any

This commit is contained in:
dudaodong
2022-03-16 18:41:40 +08:00
parent af480efa8c
commit c939b26cb8
34 changed files with 194 additions and 195 deletions

View File

@@ -21,7 +21,7 @@ func ToBool(s string) (bool, error) {
}
// ToBytes convert interface to bytes
func ToBytes(data interface{}) ([]byte, error) {
func ToBytes(data any) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(data)
@@ -44,7 +44,7 @@ func ToChar(s string) []string {
}
// ToString convert value to string
func ToString(value interface{}) string {
func ToString(value any) string {
res := ""
if value == nil {
return res
@@ -76,7 +76,7 @@ func ToString(value interface{}) string {
}
// ToJson convert value to a valid json string
func ToJson(value interface{}) (string, error) {
func ToJson(value any) (string, error) {
res, err := json.Marshal(value)
if err != nil {
return "", err
@@ -86,7 +86,7 @@ func ToJson(value interface{}) (string, error) {
}
// ToFloat convert value to a float64, if input is not a float return 0.0 and error
func ToFloat(value interface{}) (float64, error) {
func ToFloat(value any) (float64, error) {
v := reflect.ValueOf(value)
res := 0.0
@@ -113,7 +113,7 @@ func ToFloat(value interface{}) (float64, error) {
}
// ToInt convert value to a int64, if input is not a numeric format return 0 and error
func ToInt(value interface{}) (int64, error) {
func ToInt(value any) (int64, error) {
v := reflect.ValueOf(value)
var res int64
@@ -141,7 +141,7 @@ func ToInt(value interface{}) (int64, error) {
// StructToMap convert struct to map, only convert exported struct field
// map key is specified same as struct field tag `json` value
func StructToMap(value interface{}) (map[string]interface{}, error) {
func StructToMap(value any) (map[string]any, error) {
v := reflect.ValueOf(value)
t := reflect.TypeOf(value)
@@ -152,7 +152,7 @@ func StructToMap(value interface{}) (map[string]interface{}, error) {
return nil, fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", value)
}
res := make(map[string]interface{})
res := make(map[string]any)
fieldNum := t.NumField()
pattern := `^[A-Z]`

View File

@@ -36,7 +36,7 @@ func TestToBool(t *testing.T) {
func TestToBytes(t *testing.T) {
assert := internal.NewAssert(t, "TestToBytes")
cases := []interface{}{
cases := []any{
0,
false,
"1",
@@ -55,7 +55,7 @@ func TestToBytes(t *testing.T) {
func TestToInt(t *testing.T) {
assert := internal.NewAssert(t, "TestToInt")
cases := []interface{}{"123", "-123", 123,
cases := []any{"123", "-123", 123,
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
float32(12.3), float64(12.3),
"abc", false, "111111111111111111111111111111111111111"}
@@ -71,7 +71,7 @@ func TestToInt(t *testing.T) {
func TestToFloat(t *testing.T) {
assert := internal.NewAssert(t, "TestToFloat")
cases := []interface{}{
cases := []any{
"", "-1", "-.11", "1.23e3", ".123e10", "abc",
int(0), int8(1), int16(-1), int32(123), int64(123),
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
@@ -99,7 +99,7 @@ func TestToString(t *testing.T) {
}
aStruct := TestStruct{Name: "TestStruct"}
cases := []interface{}{
cases := []any{
"", nil,
int(0), int8(1), int16(-1), int32(123), int64(123),
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
@@ -147,7 +147,7 @@ func TestStructToMap(t *testing.T) {
100,
}
pm, _ := StructToMap(p)
var expected = map[string]interface{}{"name": "test"}
var expected = map[string]any{"name": "test"}
assert.Equal(expected, pm)
}