Skip to content

Commit

Permalink
fix: 修复筛选没有结果时调用其他方法报错的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
ahl5esoft committed Aug 20, 2021
1 parent 2827f6b commit 92fc97c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
\_/__/
```

# Underscore.go [![GoDoc](https://godoc.org/github.com/ahl5esoft/golang-underscore?status.svg)](https://godoc.org/github.com/ahl5esoft/golang-underscore) [![Go Report Card](https://goreportcard.com/badge/github.com/ahl5esoft/golang-underscore)](https://goreportcard.com/report/github.com/ahl5esoft/golang-underscore) ![Version](https://img.shields.io/badge/version-2.4.0-green.svg)
# Underscore.go [![GoDoc](https://godoc.org/github.com/ahl5esoft/golang-underscore?status.svg)](https://godoc.org/github.com/ahl5esoft/golang-underscore) [![Go Report Card](https://goreportcard.com/badge/github.com/ahl5esoft/golang-underscore)](https://goreportcard.com/report/github.com/ahl5esoft/golang-underscore) ![Version](https://img.shields.io/badge/version-2.4.1-green.svg)
like <a href="http://underscorejs.org/">underscore.js</a> and C# LINQ, but for Go

## Installation
Expand Down
20 changes: 20 additions & 0 deletions any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ func Benchmark_Any(b *testing.B) {
}
}

func Test_Any(t *testing.T) {
t.Run("source is empty slice", func(t *testing.T) {
arr := make([]testModel, 0)
res := Chain(arr).Any(func(r testModel, _ int) bool {
return r.ID == 0
})
assert.False(t, res)
})

t.Run("source is nil", func(t *testing.T) {
var arr []testModel
assert.Nil(t, arr)

res := Chain(arr).Any(func(r testModel, _ int) bool {
return r.ID == 0
})
assert.False(t, res)
})
}

func Test_Any_False(t *testing.T) {
ok := Chain([]testModel{
{ID: 1, Name: "one"},
Expand Down
26 changes: 15 additions & 11 deletions chain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package underscore

import "reflect"
import (
"reflect"
)

// Chain is 创建枚举器
func Chain(src interface{}) IEnumerable {
Expand Down Expand Up @@ -63,18 +65,20 @@ func chainFromValue(value reflect.Value) IEnumerable {
value.Len(),
)
default:
if iterator, ok := value.Interface().(IEnumerator); ok {
return enumerable{
Enumerator: func() IEnumerator {
return iterator
},
if value.IsValid() {
if iterator, ok := value.Interface().(IEnumerator); ok {
return enumerable{
Enumerator: func() IEnumerator {
return iterator
},
}
}
}

if value.Kind() == reflect.Ptr {
return chainFromValue(
value.Elem(),
)
if value.Kind() == reflect.Ptr {
return chainFromValue(
value.Elem(),
)
}
}

return enumerable{
Expand Down
2 changes: 1 addition & 1 deletion group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_Group(t *testing.T) {
}).Value(resValue)
assert.EqualValues(
t,
resValue.Interface(),
resValue.Elem().Interface(),
map[string][]int{
"odd": {1, 3, 5},
"even": {2, 4},
Expand Down
4 changes: 0 additions & 4 deletions map_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package underscore

import (
"fmt"
"reflect"
"strconv"
"testing"
Expand Down Expand Up @@ -51,9 +50,6 @@ func Test_Map(t *testing.T) {
resValue.Elem().Interface(),
[]int{0, 1},
)
fmt.Println(resValue)

assert.True(t, false)
})
}

Expand Down
60 changes: 39 additions & 21 deletions order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,45 @@ import (
)

func Test_Order(t *testing.T) {
arr := []testModel{
{ID: 2, Name: "two"},
{ID: 1, Name: "one"},
{ID: 3, Name: "three"},
}
var res []int
Chain(arr).Order(func(n testModel, _ int) int {
return n.ID
}).Map(func(r testModel, _ int) int {
return r.ID
}).Value(&res)
assert.Len(
t,
res,
len(arr),
)
assert.EqualValues(
t,
res,
[]int{1, 2, 3},
)
t.Run("ok", func(t *testing.T) {
arr := []testModel{
{ID: 2, Name: "two"},
{ID: 1, Name: "one"},
{ID: 3, Name: "three"},
}
var res []int
Chain(arr).Order(func(n testModel, _ int) int {
return n.ID
}).Map(func(r testModel, _ int) int {
return r.ID
}).Value(&res)
assert.Len(
t,
res,
len(arr),
)
assert.EqualValues(
t,
res,
[]int{1, 2, 3},
)
})

t.Run("chain", func(t *testing.T) {
arr := []testModel{
{ID: 2, Name: "two"},
{ID: 1, Name: "one"},
{ID: 3, Name: "three"},
}
var res []int
Chain(arr).Where(func(r testModel, _ int) bool {
return r.ID > 5
}).Order(func(r testModel, _ int) int {
return r.ID
}).Map(func(r testModel, _ int) int {
return r.ID
}).Value(&res)
})
}

func Test_OrderBy(t *testing.T) {
Expand Down

0 comments on commit 92fc97c

Please sign in to comment.