1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +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

@@ -306,7 +306,7 @@ func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool)
}
// FlattenDeep flattens slice recursive
func FlattenDeep(slice interface{}) interface{} {
func FlattenDeep(slice any) any {
sv := sliceValue(slice)
st := sliceElemType(sv.Type())
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
@@ -377,13 +377,13 @@ func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T
}
// InterfaceSlice convert param to slice of interface.
func InterfaceSlice(slice interface{}) []interface{} {
func InterfaceSlice(slice any) []any {
sv := sliceValue(slice)
if sv.IsNil() {
return nil
}
res := make([]interface{}, sv.Len())
res := make([]any, sv.Len())
for i := 0; i < sv.Len(); i++ {
res[i] = sv.Index(i).Interface()
}
@@ -392,7 +392,7 @@ func InterfaceSlice(slice interface{}) []interface{} {
}
// StringSlice convert param to slice of string.
func StringSlice(slice interface{}) []string {
func StringSlice(slice any) []string {
v := sliceValue(slice)
out := make([]string, v.Len())
@@ -408,7 +408,7 @@ func StringSlice(slice interface{}) []string {
}
// IntSlice convert param to slice of int.
func IntSlice(slice interface{}) []int {
func IntSlice(slice any) []int {
sv := sliceValue(slice)
out := make([]int, sv.Len())
@@ -473,7 +473,7 @@ func Drop[T any](slice []T, n int) []T {
}
// InsertAt insert the value or other slice into slice at index.
func InsertAt[T any](slice []T, index int, value interface{}) []T {
func InsertAt[T any](slice []T, index int, value any) []T {
size := len(slice)
if index < 0 || index > size {
@@ -608,7 +608,7 @@ func Shuffle[T any](slice []T) []T {
// SortByField return sorted slice by field
// Slice element should be struct, field type should be int, uint, string, or bool
// default sortType is ascending (asc), if descending order, set sortType to desc
func SortByField(slice interface{}, field string, sortType ...string) error {
func SortByField(slice any, field string, sortType ...string) error {
sv := sliceValue(slice)
t := sv.Type().Elem()

View File

@@ -6,7 +6,7 @@ import (
)
// sliceValue return the reflect value of a slice
func sliceValue(slice interface{}) reflect.Value {
func sliceValue(slice any) reflect.Value {
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
panic(fmt.Sprintf("Invalid slice type, value of type %T", slice))
@@ -15,7 +15,7 @@ func sliceValue(slice interface{}) reflect.Value {
}
// functionValue return the reflect value of a function
func functionValue(function interface{}) reflect.Value {
func functionValue(function any) reflect.Value {
v := reflect.ValueOf(function)
if v.Kind() != reflect.Func {
panic(fmt.Sprintf("Invalid function type, value of type %T", function))

View File

@@ -268,7 +268,7 @@ func TestReduce(t *testing.T) {
}
func TestIntSlice(t *testing.T) {
var nums []interface{}
var nums []any
nums = append(nums, 1, 2, 3)
assert := internal.NewAssert(t, "TestIntSlice")
@@ -276,7 +276,7 @@ func TestIntSlice(t *testing.T) {
}
func TestStringSlice(t *testing.T) {
var strs []interface{}
var strs []any
strs = append(strs, "a", "b", "c")
assert := internal.NewAssert(t, "TestStringSlice")
@@ -285,7 +285,7 @@ func TestStringSlice(t *testing.T) {
func TestInterfaceSlice(t *testing.T) {
strs := []string{"a", "b", "c"}
expect := []interface{}{"a", "b", "c"}
expect := []any{"a", "b", "c"}
assert := internal.NewAssert(t, "TestInterfaceSlice")
assert.Equal(expect, InterfaceSlice(strs))
@@ -380,7 +380,7 @@ func TestIntersection(t *testing.T) {
{1, 2, 3},
{},
}
res := []interface{}{
res := []any{
Intersection(s1, s2, s3),
Intersection(s1, s2),
Intersection(s1),