Feature/generic type (#19)

Supports generics and any
This commit is contained in:
jeffery
2023-04-07 19:21:05 +08:00
committed by GitHub
parent ce429aed1f
commit 9a8c60b30a
51 changed files with 348 additions and 1127 deletions

View File

@@ -1,7 +1,6 @@
package sliceUtil
import (
"errors"
"reflect"
"testing"
)
@@ -18,11 +17,7 @@ import (
// }
//
// // 获取年龄列
// ages, err := ColumnSlice(people, "Age")
// if err != nil {
// fmt.Println(err)
// return
// }
// ages := ColumnSlice(people, "Age")
// fmt.Println(ages) // 输出:[18 20 22]
//}
@@ -34,43 +29,30 @@ func TestColumnSlice(t *testing.T) {
testCases := []struct {
name string
input interface{}
input []Person
column string
output []interface{}
err error
output []any
}{
{
name: "success",
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
column: "Age",
output: []interface{}{18, 20, 22},
err: nil,
},
{
name: "not a slice",
input: Person{"Alice", 18},
column: "Age",
output: nil,
err: errors.New("not a slice"),
output: []any{18, 20, 22},
},
{
name: "column not found",
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
column: "Gender",
output: nil,
err: errors.New("column not found"),
output: []any{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output, err := ColumnSlice(tc.input, tc.column)
output := ColumnSlice(tc.input, tc.column)
if !reflect.DeepEqual(output, tc.output) {
t.Errorf("expected %v, but got %v", tc.output, output)
}
if !reflect.DeepEqual(err, tc.err) {
t.Errorf("expected %v, but got %v", tc.err, err)
}
})
}
}