Skip to content

Commit 92fc97c

Browse files
committed
fix: 修复筛选没有结果时调用其他方法报错的BUG
1 parent 2827f6b commit 92fc97c

File tree

6 files changed

+76
-38
lines changed

6 files changed

+76
-38
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
\_/__/
1010
```
1111

12-
# 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)
12+
# 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)
1313
like <a href="http://underscorejs.org/">underscore.js</a> and C# LINQ, but for Go
1414

1515
## Installation

Diff for: any_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ func Benchmark_Any(b *testing.B) {
1414
}
1515
}
1616

17+
func Test_Any(t *testing.T) {
18+
t.Run("source is empty slice", func(t *testing.T) {
19+
arr := make([]testModel, 0)
20+
res := Chain(arr).Any(func(r testModel, _ int) bool {
21+
return r.ID == 0
22+
})
23+
assert.False(t, res)
24+
})
25+
26+
t.Run("source is nil", func(t *testing.T) {
27+
var arr []testModel
28+
assert.Nil(t, arr)
29+
30+
res := Chain(arr).Any(func(r testModel, _ int) bool {
31+
return r.ID == 0
32+
})
33+
assert.False(t, res)
34+
})
35+
}
36+
1737
func Test_Any_False(t *testing.T) {
1838
ok := Chain([]testModel{
1939
{ID: 1, Name: "one"},

Diff for: chain.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package underscore
22

3-
import "reflect"
3+
import (
4+
"reflect"
5+
)
46

57
// Chain is 创建枚举器
68
func Chain(src interface{}) IEnumerable {
@@ -63,18 +65,20 @@ func chainFromValue(value reflect.Value) IEnumerable {
6365
value.Len(),
6466
)
6567
default:
66-
if iterator, ok := value.Interface().(IEnumerator); ok {
67-
return enumerable{
68-
Enumerator: func() IEnumerator {
69-
return iterator
70-
},
68+
if value.IsValid() {
69+
if iterator, ok := value.Interface().(IEnumerator); ok {
70+
return enumerable{
71+
Enumerator: func() IEnumerator {
72+
return iterator
73+
},
74+
}
7175
}
72-
}
7376

74-
if value.Kind() == reflect.Ptr {
75-
return chainFromValue(
76-
value.Elem(),
77-
)
77+
if value.Kind() == reflect.Ptr {
78+
return chainFromValue(
79+
value.Elem(),
80+
)
81+
}
7882
}
7983

8084
return enumerable{

Diff for: group_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Test_Group(t *testing.T) {
5353
}).Value(resValue)
5454
assert.EqualValues(
5555
t,
56-
resValue.Interface(),
56+
resValue.Elem().Interface(),
5757
map[string][]int{
5858
"odd": {1, 3, 5},
5959
"even": {2, 4},

Diff for: map_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package underscore
22

33
import (
4-
"fmt"
54
"reflect"
65
"strconv"
76
"testing"
@@ -51,9 +50,6 @@ func Test_Map(t *testing.T) {
5150
resValue.Elem().Interface(),
5251
[]int{0, 1},
5352
)
54-
fmt.Println(resValue)
55-
56-
assert.True(t, false)
5753
})
5854
}
5955

Diff for: order_test.go

+39-21
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,45 @@ import (
77
)
88

99
func Test_Order(t *testing.T) {
10-
arr := []testModel{
11-
{ID: 2, Name: "two"},
12-
{ID: 1, Name: "one"},
13-
{ID: 3, Name: "three"},
14-
}
15-
var res []int
16-
Chain(arr).Order(func(n testModel, _ int) int {
17-
return n.ID
18-
}).Map(func(r testModel, _ int) int {
19-
return r.ID
20-
}).Value(&res)
21-
assert.Len(
22-
t,
23-
res,
24-
len(arr),
25-
)
26-
assert.EqualValues(
27-
t,
28-
res,
29-
[]int{1, 2, 3},
30-
)
10+
t.Run("ok", func(t *testing.T) {
11+
arr := []testModel{
12+
{ID: 2, Name: "two"},
13+
{ID: 1, Name: "one"},
14+
{ID: 3, Name: "three"},
15+
}
16+
var res []int
17+
Chain(arr).Order(func(n testModel, _ int) int {
18+
return n.ID
19+
}).Map(func(r testModel, _ int) int {
20+
return r.ID
21+
}).Value(&res)
22+
assert.Len(
23+
t,
24+
res,
25+
len(arr),
26+
)
27+
assert.EqualValues(
28+
t,
29+
res,
30+
[]int{1, 2, 3},
31+
)
32+
})
33+
34+
t.Run("chain", func(t *testing.T) {
35+
arr := []testModel{
36+
{ID: 2, Name: "two"},
37+
{ID: 1, Name: "one"},
38+
{ID: 3, Name: "three"},
39+
}
40+
var res []int
41+
Chain(arr).Where(func(r testModel, _ int) bool {
42+
return r.ID > 5
43+
}).Order(func(r testModel, _ int) int {
44+
return r.ID
45+
}).Map(func(r testModel, _ int) int {
46+
return r.ID
47+
}).Value(&res)
48+
})
3149
}
3250

3351
func Test_OrderBy(t *testing.T) {

0 commit comments

Comments
 (0)