diff --git a/distinct.go b/distinct.go index f17d317..4957339 100644 --- a/distinct.go +++ b/distinct.go @@ -34,7 +34,7 @@ func (g *gollection) Distinct() *gollection { } } -func (g *gollection) DistinctBy(f func(v interface{}) interface{}) *gollection { +func (g *gollection) DistinctBy(f interface{}) *gollection { if g.err != nil { return &gollection{err: g.err} } @@ -47,12 +47,22 @@ func (g *gollection) DistinctBy(f func(v interface{}) interface{}) *gollection { } } - ret := reflect.MakeSlice(sv.Type(), 0, sv.Len()) + funcValue := reflect.ValueOf(f) + funcType := funcValue.Type() + if funcType.Kind() != reflect.Func || funcType.NumIn() != 1 || funcType.NumOut() != 1 { + return &gollection{ + slice: nil, + err: fmt.Errorf("gollection.DistinctBy called with invalid func. required func(in ) out but supplied %v", g.slice), + } + } + + resultSliceType := reflect.SliceOf(funcType.In(0)) + ret := reflect.MakeSlice(resultSliceType, 0, sv.Len()) m := make(map[interface{}]bool) for i := 0; i < sv.Len(); i++ { v := sv.Index(i) - id := f(v.Interface()) + id := funcValue.Call([]reflect.Value{v})[0].Interface() if _, ok := m[id]; !ok { ret = reflect.Append(ret, v) m[id] = true diff --git a/distinct_test.go b/distinct_test.go index 9a12009..b3ad694 100644 --- a/distinct_test.go +++ b/distinct_test.go @@ -34,13 +34,12 @@ func TestDistinctBy(t *testing.T) { arr := []string{"aaa", "bb", "c", "ddd", "ee", "f"} expect := []string{"aaa", "bb", "c"} - res, err := gollection.New(arr).DistinctBy(func(v interface{}) interface{} { - return len(v.(string)) + res, err := gollection.New(arr).DistinctBy(func(v string) int { + return len(v) }).Result() assert.NoError(err) assert.Equal(expect, res) } - func TestDistinctBy_NotSlice(t *testing.T) { assert := assert.New(t) _, err := gollection.New("not slice value").DistinctBy(func(v interface{}) interface{} { @@ -48,6 +47,11 @@ func TestDistinctBy_NotSlice(t *testing.T) { }).Result() assert.Error(err) } +func TestDistinctBy_NotFunc(t *testing.T) { + assert := assert.New(t) + _, err := gollection.New([]int{}).DistinctBy(0).Result() + assert.Error(err) +} func TestDistinctBy_HavingError(t *testing.T) { assert := assert.New(t) diff --git a/examples/distinct/main.go b/examples/distinct/main.go index 12c7a0a..5877c6b 100644 --- a/examples/distinct/main.go +++ b/examples/distinct/main.go @@ -26,8 +26,8 @@ func main() { {ID: 6, Name: "Charles"}, } - res, _ = gollection.New(users).DistinctBy(func(v interface{}) interface{} { - return v.(user).Name + res, _ = gollection.New(users).DistinctBy(func(v user) string { + return v.Name }).Result() fmt.Println("origin : ", users) fmt.Println("ret : ", res) diff --git a/examples/filter/main.go b/examples/filter/main.go index efb89a9..c8ed268 100644 --- a/examples/filter/main.go +++ b/examples/filter/main.go @@ -9,11 +9,8 @@ import ( func main() { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} - res, _ := gollection.New(arr).Filter(func(v interface{}) bool { - if n, ok := v.(int); ok && n > 5 { - return true - } - return false + res, _ := gollection.New(arr).Filter(func(v int) bool { + return v > 5 }).Result() fmt.Println("origin : ", arr) fmt.Println("ret : ", res) diff --git a/examples/flatMap/main.go b/examples/flatMap/main.go index b4bf4b2..2d55f8a 100644 --- a/examples/flatMap/main.go +++ b/examples/flatMap/main.go @@ -13,11 +13,8 @@ func main() { {6, 7, 8, 9, 10}, } - res, _ := gollection.New(arr).FlatMap(func(v interface{}) interface{} { - if n, ok := v.(int); ok { - return n * 2 - } - return 0 + res, _ := gollection.New(arr).FlatMap(func(v int) int { + return v * 2 }).Result() fmt.Println("origin : ", arr) fmt.Println("ret : ", res) diff --git a/examples/fold/main.go b/examples/fold/main.go index 8828b23..b4e32ce 100644 --- a/examples/fold/main.go +++ b/examples/fold/main.go @@ -9,13 +9,8 @@ import ( func main() { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} - res, _ := gollection.New(arr).Fold(100, func(v1, v2 interface{}) interface{} { - n1, ok1 := v1.(int) - n2, ok2 := v2.(int) - if ok1 && ok2 { - return n1 + n2 - } - return "" + res, _ := gollection.New(arr).Fold(100, func(v1, v2 int) int { + return v1 + v2 }).Result() fmt.Println("origin : ", arr) fmt.Println("ret : ", res) diff --git a/examples/map/main.go b/examples/map/main.go index cfe73d2..cedc01a 100644 --- a/examples/map/main.go +++ b/examples/map/main.go @@ -9,11 +9,8 @@ import ( func main() { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} - res, _ := gollection.New(arr).Map(func(v interface{}) interface{} { - if n, ok := v.(int); ok { - return n * 2 - } - return 0 + res, _ := gollection.New(arr).Map(func(v int) int { + return v * 2 }).Result() fmt.Println("origin : ", arr) fmt.Println("ret : ", res) diff --git a/examples/reduce/main.go b/examples/reduce/main.go index f59bef5..bc2191b 100644 --- a/examples/reduce/main.go +++ b/examples/reduce/main.go @@ -9,13 +9,8 @@ import ( func main() { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} - res, _ := gollection.New(arr).Reduce(func(v1, v2 interface{}) interface{} { - n1, ok1 := v1.(int) - n2, ok2 := v2.(int) - if ok1 && ok2 { - return n1 + n2 - } - return "" + res, _ := gollection.New(arr).Reduce(func(v1, v2 int) int { + return v1 + v2 }).Result() fmt.Println("origin : ", arr) fmt.Println("ret : ", res) diff --git a/examples/sort/main.go b/examples/sort/main.go index 8bea13f..7284ac4 100644 --- a/examples/sort/main.go +++ b/examples/sort/main.go @@ -9,8 +9,8 @@ import ( func main() { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1} - res, _ := gollection.New(arr).SortBy(func(v1, v2 interface{}) bool { - return v1.(int) < v2.(int) + res, _ := gollection.New(arr).SortBy(func(v1, v2 int) bool { + return v1 < v2 }).Result() fmt.Println("origin : ", arr) fmt.Println("ret : ", res) diff --git a/filter.go b/filter.go index d9559c0..53b412b 100644 --- a/filter.go +++ b/filter.go @@ -5,7 +5,7 @@ import ( "reflect" ) -func (g *gollection) Filter(f func(v interface{}) bool) *gollection { +func (g *gollection) Filter(f interface{}) *gollection { if g.err != nil { return &gollection{err: g.err} } @@ -18,11 +18,21 @@ func (g *gollection) Filter(f func(v interface{}) bool) *gollection { } } - ret := reflect.MakeSlice(sv.Type(), 0, sv.Len()) + funcValue := reflect.ValueOf(f) + funcType := funcValue.Type() + if funcType.Kind() != reflect.Func || funcType.NumIn() != 1 || funcType.NumOut() != 1 || funcType.Out(0).Kind() != reflect.Bool { + return &gollection{ + slice: nil, + err: fmt.Errorf("gollection.Filter called with invalid func. required func(in ) bool but supplied %v", g.slice), + } + } + + resultSliceType := reflect.SliceOf(funcType.In(0)) + ret := reflect.MakeSlice(resultSliceType, 0, sv.Len()) for i := 0; i < sv.Len(); i++ { v := sv.Index(i) - if f(v.Interface()) { + if funcValue.Call([]reflect.Value{v})[0].Interface().(bool) { ret = reflect.Append(ret, sv.Index(i)) } } diff --git a/filter_test.go b/filter_test.go index 399689b..a21c155 100644 --- a/filter_test.go +++ b/filter_test.go @@ -12,16 +12,12 @@ func TestFilter(t *testing.T) { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} expect := []int{6, 7, 8, 9, 10} - res, err := gollection.New(arr).Filter(func(v interface{}) bool { - if n, ok := v.(int); ok && n > 5 { - return true - } - return false + res, err := gollection.New(arr).Filter(func(v int) bool { + return v > 5 }).Result() assert.NoError(err) assert.Equal(expect, res) } - func TestFilter_NotSlice(t *testing.T) { assert := assert.New(t) _, err := gollection.New("not slice value").Filter(func(v interface{}) bool { @@ -29,6 +25,11 @@ func TestFilter_NotSlice(t *testing.T) { }).Result() assert.Error(err) } +func TestFilter_NotFunc(t *testing.T) { + assert := assert.New(t) + _, err := gollection.New([]int{0}).Filter(0).Result() + assert.Error(err) +} func TestFilter_HavingError(t *testing.T) { assert := assert.New(t) diff --git a/flat_map.go b/flat_map.go index 4ac252d..81e93b1 100644 --- a/flat_map.go +++ b/flat_map.go @@ -5,7 +5,7 @@ import ( "reflect" ) -func (g *gollection) FlatMap(f func(v interface{}) interface{}) *gollection { +func (g *gollection) FlatMap(f interface{}) *gollection { if g.err != nil { return &gollection{err: g.err} } @@ -18,9 +18,25 @@ func (g *gollection) FlatMap(f func(v interface{}) interface{}) *gollection { } } - // init - retType := reflect.ValueOf(f(nil)).Type() - ret := reflect.MakeSlice(reflect.SliceOf(retType), 0, sv.Len()) + currentType := reflect.TypeOf(g.slice).Elem() + if currentType.Kind() != reflect.Slice { + return &gollection{ + slice: nil, + err: fmt.Errorf("gollection.FlatMap called with non-slice-of-slice value of type %T", g.slice), + } + } + + funcValue := reflect.ValueOf(f) + funcType := funcValue.Type() + if funcType.Kind() != reflect.Func || funcType.NumIn() != 1 || funcType.NumOut() != 1 { + return &gollection{ + slice: nil, + err: fmt.Errorf("gollection.FlatMap called with invalid func. required func(in ) out but supplied %v", g.slice), + } + } + + resultSliceType := reflect.SliceOf(funcType.Out(0)) + ret := reflect.MakeSlice(resultSliceType, 0, sv.Len()) // avoid "panic: reflect: call of reflect.Value.Interface on zero Value" // see https://github.com/azihsoyn/gollection/issues/7 @@ -33,11 +49,8 @@ func (g *gollection) FlatMap(f func(v interface{}) interface{}) *gollection { for i := 0; i < sv.Len(); i++ { v := sv.Index(i).Interface() svv := reflect.ValueOf(v) - if svv.Kind() != reflect.Slice { - continue - } for j := 0; j < svv.Len(); j++ { - v := reflect.ValueOf(f(svv.Index(j).Interface())) + v := funcValue.Call([]reflect.Value{svv.Index(j)})[0] ret = reflect.Append(ret, v) } } diff --git a/flat_map_test.go b/flat_map_test.go index 1bfee8d..75e4837 100644 --- a/flat_map_test.go +++ b/flat_map_test.go @@ -17,11 +17,8 @@ func TestFlatMap(t *testing.T) { } expect := []int{2, 4, 6, 8, 10, 12, 14, 16, 18, 20} - res, err := gollection.New(arr).FlatMap(func(v interface{}) interface{} { - if n, ok := v.(int); ok { - return n * 2 - } - return 0 + res, err := gollection.New(arr).FlatMap(func(v int) int { + return v * 2 }).Result() assert.NoError(err) assert.Equal(expect, res) @@ -29,18 +26,18 @@ func TestFlatMap(t *testing.T) { func TestFlatMap_InterfaceSlice(t *testing.T) { assert := assert.New(t) - arr := []interface{}{ - []int{1, 2, 3}, - "a", "b", + arr := [][]interface{}{ + []interface{}{1, 2, 3}, + []interface{}{"a", "b"}, nil, } - expect := []int{2, 4, 6} + expect := []interface{}{2, 4, 6, "a", "b"} res, err := gollection.New(arr).FlatMap(func(v interface{}) interface{} { if n, ok := v.(int); ok { return n * 2 } - return 0 + return v }).Result() assert.NoError(err) assert.Equal(expect, res) @@ -49,13 +46,27 @@ func TestFlatMap_InterfaceSlice(t *testing.T) { func TestFlatMap_EmptySlice(t *testing.T) { assert := assert.New(t) expect := []string{} - res, err := gollection.New([][]int{}).FlatMap(func(v interface{}) interface{} { + res, err := gollection.New([][]int{}).FlatMap(func(v int) string { return "" }).Result() assert.NoError(err) assert.Equal(expect, res) } +func TestFlatMap_NotFunc(t *testing.T) { + assert := assert.New(t) + _, err := gollection.New([][]int{}).FlatMap(0).Result() + assert.Error(err) +} + +func TestFlatMap_NonSliceOfSlice(t *testing.T) { + assert := assert.New(t) + _, err := gollection.New([]int{}).FlatMap(func(v int) string { + return "" + }).Result() + assert.Error(err) +} + func TestFlatMap_NotSlice(t *testing.T) { assert := assert.New(t) _, err := gollection.New("not slice value").FlatMap(func(v interface{}) interface{} { diff --git a/fold.go b/fold.go index 5810094..d44be45 100644 --- a/fold.go +++ b/fold.go @@ -5,7 +5,7 @@ import ( "reflect" ) -func (g *gollection) Fold(v0 interface{}, f func(v1, v2 interface{}) interface{}) *gollection { +func (g *gollection) Fold(v0 interface{}, f interface{}) *gollection { if g.err != nil { return &gollection{err: g.err} } @@ -24,13 +24,23 @@ func (g *gollection) Fold(v0 interface{}, f func(v1, v2 interface{}) interface{} } } - v1 := v0 + funcValue := reflect.ValueOf(f) + funcType := funcValue.Type() + if funcType.Kind() != reflect.Func || funcType.NumIn() != 2 || funcType.NumOut() != 1 { + return &gollection{ + slice: nil, + err: fmt.Errorf("gollection.Fold called with invalid func. required func(in1, in2 ) out but supplied %v", g.slice), + } + } + + ret := v0 for i := 0; i < sv.Len(); i++ { - v2 := sv.Index(i).Interface() - v1 = f(v1, v2) + v1 := reflect.ValueOf(ret) + v2 := sv.Index(i) + ret = funcValue.Call([]reflect.Value{v1, v2})[0].Interface() } return &gollection{ - val: v1, + val: ret, } } diff --git a/fold_test.go b/fold_test.go index cf13c5b..cafb6a9 100644 --- a/fold_test.go +++ b/fold_test.go @@ -12,13 +12,8 @@ func TestFold(t *testing.T) { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} expect := 155 - res, err := gollection.New(arr).Fold(100, func(v1, v2 interface{}) interface{} { - n1, ok1 := v1.(int) - n2, ok2 := v2.(int) - if ok1 && ok2 { - return n1 + n2 - } - return "" + res, err := gollection.New(arr).Fold(100, func(v1, v2 int) int { + return v1 + v2 }).Result() assert.NoError(err) assert.Equal(expect, res) @@ -26,18 +21,12 @@ func TestFold(t *testing.T) { arr = []int{} expect = 100 - res, err = gollection.New(arr).Fold(100, func(v1, v2 interface{}) interface{} { - n1, ok1 := v1.(int) - n2, ok2 := v2.(int) - if ok1 && ok2 { - return n1 + n2 - } - return "" + res, err = gollection.New(arr).Fold(100, func(v1, v2 int) int { + return v1 + v2 }).Result() assert.NoError(err) assert.Equal(expect, res) } - func TestFold_NotSlice(t *testing.T) { assert := assert.New(t) _, err := gollection.New("not slice value").Fold(100, func(v1, v2 interface{}) interface{} { @@ -45,6 +34,11 @@ func TestFold_NotSlice(t *testing.T) { }).Result() assert.Error(err) } +func TestFold_NotFunc(t *testing.T) { + assert := assert.New(t) + _, err := gollection.New([]int{0}).Fold(100, 0).Result() + assert.Error(err) +} func TestFold_HavingError(t *testing.T) { assert := assert.New(t) diff --git a/map.go b/map.go index 33a6f49..266d98f 100644 --- a/map.go +++ b/map.go @@ -5,7 +5,7 @@ import ( "reflect" ) -func (g *gollection) Map(f func(v interface{}) interface{}) *gollection { +func (g *gollection) Map(f interface{}) *gollection { if g.err != nil { return &gollection{err: g.err} } @@ -18,9 +18,16 @@ func (g *gollection) Map(f func(v interface{}) interface{}) *gollection { } } - // init - retType := reflect.ValueOf(f(nil)).Type() - ret := reflect.MakeSlice(reflect.SliceOf(retType), 0, sv.Len()) + funcValue := reflect.ValueOf(f) + funcType := funcValue.Type() + if funcType.Kind() != reflect.Func || funcType.NumIn() != 1 || funcType.NumOut() != 1 { + return &gollection{ + slice: nil, + err: fmt.Errorf("gollection.Map called with invalid func. required func(in ) out but supplied %v", g.slice), + } + } + resultSliceType := reflect.SliceOf(funcType.Out(0)) + ret := reflect.MakeSlice(resultSliceType, 0, sv.Len()) // avoid "panic: reflect: call of reflect.Value.Interface on zero Value" // see https://github.com/azihsoyn/gollection/issues/7 @@ -31,7 +38,7 @@ func (g *gollection) Map(f func(v interface{}) interface{}) *gollection { } for i := 0; i < sv.Len(); i++ { - v := reflect.ValueOf(f(sv.Index(i).Interface())) + v := funcValue.Call([]reflect.Value{sv.Index(i)})[0] ret = reflect.Append(ret, v) } diff --git a/map_test.go b/map_test.go index afd06a7..5f64e9a 100644 --- a/map_test.go +++ b/map_test.go @@ -13,11 +13,8 @@ func TestMap(t *testing.T) { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} expect := []int{2, 4, 6, 8, 10, 12, 14, 16, 18, 20} - res, err := gollection.New(arr).Map(func(v interface{}) interface{} { - if n, ok := v.(int); ok { - return n * 2 - } - return 0 + res, err := gollection.New(arr).Map(func(v int) int { + return v * 2 }).Result() assert.NoError(err) assert.Equal(expect, res) @@ -28,11 +25,8 @@ func TestMap_WithCast(t *testing.T) { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} expect := []string{"2", "4", "6", "8", "10", "12", "14", "16", "18", "20"} - res, err := gollection.New(arr).Map(func(v interface{}) interface{} { - if n, ok := v.(int); ok { - return fmt.Sprintf("%d", n*2) - } - return "" + res, err := gollection.New(arr).Map(func(v int) string { + return fmt.Sprintf("%d", v*2) }).Result() assert.NoError(err) assert.Equal(expect, res) @@ -45,7 +39,6 @@ func TestMap_EmptySlice(t *testing.T) { }).Result() assert.NoError(err) } - func TestMap_NotSlice(t *testing.T) { assert := assert.New(t) _, err := gollection.New("not slice value").Map(func(v interface{}) interface{} { @@ -53,6 +46,11 @@ func TestMap_NotSlice(t *testing.T) { }).Result() assert.Error(err) } +func TestMap_NotFunc(t *testing.T) { + assert := assert.New(t) + _, err := gollection.New([]int{}).Map(0).Result() + assert.Error(err) +} func TestMap_HavingError(t *testing.T) { assert := assert.New(t) diff --git a/reduce.go b/reduce.go index 5feaa4a..f8782c2 100644 --- a/reduce.go +++ b/reduce.go @@ -5,7 +5,7 @@ import ( "reflect" ) -func (g *gollection) Reduce(f func(v1, v2 interface{}) interface{}) *gollection { +func (g *gollection) Reduce(f interface{}) *gollection { if g.err != nil { return &gollection{err: g.err} } @@ -29,13 +29,23 @@ func (g *gollection) Reduce(f func(v1, v2 interface{}) interface{}) *gollection } } - v1 := sv.Index(0).Interface() + funcValue := reflect.ValueOf(f) + funcType := funcValue.Type() + if funcType.Kind() != reflect.Func || funcType.NumIn() != 2 || funcType.NumOut() != 1 { + return &gollection{ + slice: nil, + err: fmt.Errorf("gollection.Reduce called with invalid func. required func(in1, in2 ) out but supplied %v", g.slice), + } + } + + ret := sv.Index(0).Interface() for i := 1; i < sv.Len(); i++ { - v2 := sv.Index(i).Interface() - v1 = f(v1, v2) + v1 := reflect.ValueOf(ret) + v2 := sv.Index(i) + ret = funcValue.Call([]reflect.Value{v1, v2})[0].Interface() } return &gollection{ - val: v1, + val: ret, } } diff --git a/reduce_test.go b/reduce_test.go index 94c37c6..fd11740 100644 --- a/reduce_test.go +++ b/reduce_test.go @@ -13,13 +13,8 @@ func TestReduce(t *testing.T) { arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} expect := 55 - res, err := gollection.New(arr).Reduce(func(v1, v2 interface{}) interface{} { - n1, ok1 := v1.(int) - n2, ok2 := v2.(int) - if ok1 && ok2 { - return n1 + n2 - } - return "" + res, err := gollection.New(arr).Reduce(func(v1, v2 int) int { + return v1 + v2 }).Result() assert.NoError(err) assert.Equal(expect, res) @@ -27,18 +22,12 @@ func TestReduce(t *testing.T) { arr = []int{1} expect = 1 - res, err = gollection.New(arr).Reduce(func(v1, v2 interface{}) interface{} { - n1, ok1 := v1.(int) - n2, ok2 := v2.(int) - if ok1 && ok2 { - return n1 + n2 - } - return "" + res, err = gollection.New(arr).Reduce(func(v1, v2 int) int { + return v1 + v2 }).Result() assert.NoError(err) assert.Equal(expect, res) } - func TestReduce_NotSlice(t *testing.T) { assert := assert.New(t) _, err := gollection.New("not slice value").Reduce(func(v1, v2 interface{}) interface{} { @@ -46,6 +35,11 @@ func TestReduce_NotSlice(t *testing.T) { }).Result() assert.Error(err) } +func TestReduce_NotFunc(t *testing.T) { + assert := assert.New(t) + _, err := gollection.New([]int{0, 0, 0}).Reduce(0).Result() + assert.Error(err) +} func TestReduce_EmptySlice(t *testing.T) { assert := assert.New(t) diff --git a/sort.go b/sort.go index f53da4c..33a4a0f 100644 --- a/sort.go +++ b/sort.go @@ -8,7 +8,7 @@ import ( "go4.org/reflectutil" ) -func (g *gollection) SortBy(f func(v1, v2 interface{}) bool) *gollection { +func (g *gollection) SortBy(f interface{}) *gollection { if g.err != nil { return &gollection{err: g.err} } @@ -24,8 +24,17 @@ func (g *gollection) SortBy(f func(v1, v2 interface{}) bool) *gollection { ret := reflect.MakeSlice(sv.Type(), sv.Len(), sv.Cap()) reflect.Copy(orig, sv) + funcValue := reflect.ValueOf(f) + funcType := funcValue.Type() + if funcType.Kind() != reflect.Func || funcType.NumIn() != 2 || funcType.NumOut() != 1 || funcType.Out(0).Kind() != reflect.Bool { + return &gollection{ + slice: nil, + err: fmt.Errorf("gollection.SortBy called with invalid func. required func(in1, in2 ) bool but supplied %v", g.slice), + } + } + less := func(i, j int) bool { - return f(sv.Index(i).Interface(), sv.Index(j).Interface()) + return funcValue.Call([]reflect.Value{sv.Index(i), sv.Index(j)})[0].Interface().(bool) } sort.Sort(&funcs{ diff --git a/sort_test.go b/sort_test.go index 45a88a2..4730139 100644 --- a/sort_test.go +++ b/sort_test.go @@ -14,8 +14,8 @@ func TestSortBy(t *testing.T) { original := make([]int, len(arr)) copy(original, arr) - res, err := gollection.New(arr).SortBy(func(v1, v2 interface{}) bool { - return v1.(int) < v2.(int) + res, err := gollection.New(arr).SortBy(func(v1, v2 int) bool { + return v1 < v2 }).Result() assert.NoError(err) @@ -23,7 +23,6 @@ func TestSortBy(t *testing.T) { // check not changed assert.Equal(original, arr) } - func TestSort_NotSlice(t *testing.T) { assert := assert.New(t) _, err := gollection.New("not slice value").SortBy(func(v1, v2 interface{}) bool { @@ -31,6 +30,11 @@ func TestSort_NotSlice(t *testing.T) { }).Result() assert.Error(err) } +func TestSort_NotFunc(t *testing.T) { + assert := assert.New(t) + _, err := gollection.New([]int{0, 0, 0}).SortBy(0).Result() + assert.Error(err) +} func TestSort_HavingError(t *testing.T) { assert := assert.New(t)