Skip to content

Commit 2d8e3d5

Browse files
author
ahl5esoft
committed
删除IQuery
IEnumerable增加MapMany、MapManyBy、SelectMany、SelectManyBy
1 parent 39ddf00 commit 2d8e3d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+494
-1671
lines changed

README.md

+109-224
Large diffs are not rendered by default.

aggregate_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
func Benchmark_Aggregate(b *testing.B) {
88
for n := 0; n < b.N; n++ {
99
total := 0
10-
Range2(1, 100, 1).Aggregate(make([]int, 0), func(memo []int, r, _ int) []int {
10+
Range(1, 100, 1).Aggregate(make([]int, 0), func(memo []int, r, _ int) []int {
1111
memo = append(memo, r)
1212
memo = append(memo, -r)
1313
return memo
@@ -17,7 +17,7 @@ func Benchmark_Aggregate(b *testing.B) {
1717

1818
func Benchmark_Aggregate_NoValue(b *testing.B) {
1919
for n := 0; n < b.N; n++ {
20-
Range2(1, 100, 1).Aggregate(make([]int, 0), func(memo []int, r, _ int) []int {
20+
Range(1, 100, 1).Aggregate(make([]int, 0), func(memo []int, r, _ int) []int {
2121
memo = append(memo, r)
2222
memo = append(memo, -r)
2323
return memo
@@ -27,7 +27,7 @@ func Benchmark_Aggregate_NoValue(b *testing.B) {
2727

2828
func Test_Aggregate(t *testing.T) {
2929
dst := make([]int, 0)
30-
Chain2([]int{1, 2}).Aggregate(make([]int, 0), func(memo []int, n, _ int) []int {
30+
Chain([]int{1, 2}).Aggregate(make([]int, 0), func(memo []int, n, _ int) []int {
3131
memo = append(memo, n)
3232
memo = append(memo, n+10)
3333
return memo

all.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
package underscore
22

3-
import (
4-
"reflect"
5-
)
6-
7-
func (m *query) All(predicate interface{}) bool {
8-
var ok bool
9-
each(m.Source, predicate, func(resRV, _, _ reflect.Value) bool {
10-
ok = resRV.Bool()
11-
return !ok
12-
})
13-
return ok
14-
}
15-
16-
func (m *query) AllBy(properties map[string]interface{}) bool {
17-
return m.All(func(value, _ interface{}) bool {
18-
return IsMatch(value, properties)
19-
})
20-
}
3+
import "reflect"
214

225
func (m enumerable) All(predicate interface{}) bool {
236
iterator := m.GetEnumerator()

all_test.go

+4-64
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@ func Benchmark_All(b *testing.B) {
1010
}
1111
}
1212

13-
func Benchmark_All_New(b *testing.B) {
14-
for n := 0; n < b.N; n++ {
15-
Range2(0, benchmarkSize, 1).All(func(r, _ int) bool {
16-
return r < 1000
17-
})
18-
}
19-
}
20-
2113
func Test_All_False(t *testing.T) {
22-
ok := Chain2([]testModel{
14+
ok := Chain([]testModel{
2315
{ID: 1, Name: "one"},
2416
{ID: 1, Name: "two"},
2517
{ID: 1, Name: "three"},
@@ -32,58 +24,6 @@ func Test_All_False(t *testing.T) {
3224
}
3325

3426
func Test_All_True(t *testing.T) {
35-
ok := Chain2([]testModel{
36-
{ID: 1, Name: "one"},
37-
{ID: 1, Name: "two"},
38-
{ID: 1, Name: "three"},
39-
}).All(func(r testModel, _ int) bool {
40-
return r.ID == 1
41-
})
42-
if !ok {
43-
t.Error("wrong")
44-
}
45-
}
46-
47-
func Test_AllBy_False(t *testing.T) {
48-
ok := Chain2([]testModel{
49-
{ID: 1, Name: "one"},
50-
{ID: 2, Name: "two"},
51-
{ID: 3, Name: "three"},
52-
}).AllBy(map[string]interface{}{
53-
"Name": "a",
54-
})
55-
if ok {
56-
t.Error("wrong")
57-
}
58-
}
59-
60-
func Test_AllBy_True(t *testing.T) {
61-
ok := Chain2([]testModel{
62-
{ID: 1, Name: "one"},
63-
{ID: 2, Name: "one"},
64-
{ID: 3, Name: "one"},
65-
}).AllBy(map[string]interface{}{
66-
"name": "one",
67-
})
68-
if !ok {
69-
t.Error("wrong")
70-
}
71-
}
72-
73-
func Test_Chain_All_False(t *testing.T) {
74-
ok := Chain([]testModel{
75-
{ID: 1, Name: "one"},
76-
{ID: 1, Name: "two"},
77-
{ID: 1, Name: "three"},
78-
}).All(func(r testModel, _ int) bool {
79-
return r.ID != 1
80-
})
81-
if ok {
82-
t.Error("wrong")
83-
}
84-
}
85-
86-
func Test_Chain_All_True(t *testing.T) {
8727
ok := Chain([]testModel{
8828
{ID: 1, Name: "one"},
8929
{ID: 1, Name: "two"},
@@ -96,7 +36,7 @@ func Test_Chain_All_True(t *testing.T) {
9636
}
9737
}
9838

99-
func Test_Chain_AllBy_False(t *testing.T) {
39+
func Test_AllBy_False(t *testing.T) {
10040
ok := Chain([]testModel{
10141
{ID: 1, Name: "one"},
10242
{ID: 2, Name: "two"},
@@ -109,13 +49,13 @@ func Test_Chain_AllBy_False(t *testing.T) {
10949
}
11050
}
11151

112-
func Test_Chain_AllBy_True(t *testing.T) {
52+
func Test_AllBy_True(t *testing.T) {
11353
ok := Chain([]testModel{
11454
{ID: 1, Name: "one"},
11555
{ID: 2, Name: "one"},
11656
{ID: 3, Name: "one"},
11757
}).AllBy(map[string]interface{}{
118-
"Name": "one",
58+
"name": "one",
11959
})
12060
if !ok {
12161
t.Error("wrong")

any.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
package underscore
22

3-
import (
4-
"reflect"
5-
)
6-
7-
func (m *query) Any(predicate interface{}) bool {
8-
var ok bool
9-
each(m.Source, predicate, func(resRV, _, _ reflect.Value) bool {
10-
ok = resRV.Bool()
11-
return ok
12-
})
13-
return ok
14-
}
15-
16-
func (m *query) AnyBy(properties map[string]interface{}) bool {
17-
return m.Any(func(value, _ interface{}) bool {
18-
return IsMatch(value, properties)
19-
})
20-
}
3+
import "reflect"
214

225
func (m enumerable) Any(predicate interface{}) bool {
236
iterator := m.GetEnumerator()

any_test.go

+4-64
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,15 @@ package underscore
22

33
import "testing"
44

5-
func Benchmark_Any(b *testing.B) {
6-
for n := 0; n < b.N; n++ {
7-
Range(1, benchmarkSize, 1).Any(func(r, _ int) bool {
8-
return r > 1000
9-
})
10-
}
11-
}
12-
135
func Benchmark_Any_New(b *testing.B) {
146
for n := 0; n < b.N; n++ {
15-
Range2(1, benchmarkSize, 1).Any(func(r, _ int) bool {
7+
Range(1, benchmarkSize, 1).Any(func(r, _ int) bool {
168
return r > 1000
179
})
1810
}
1911
}
2012

2113
func Test_Any_False(t *testing.T) {
22-
ok := Chain2([]testModel{
23-
{ID: 1, Name: "one"},
24-
{ID: 2, Name: "two"},
25-
{ID: 3, Name: "three"},
26-
}).Any(func(r testModel, _ int) bool {
27-
return r.ID == 0
28-
})
29-
if ok {
30-
t.Error("wrong")
31-
}
32-
}
33-
34-
func Test_Any_True(t *testing.T) {
35-
ok := Chain2([]testModel{
36-
{ID: 1, Name: "one"},
37-
{ID: 2, Name: "two"},
38-
{ID: 3, Name: "three"},
39-
}).Any(func(r testModel, _ int) bool {
40-
return r.ID == 1
41-
})
42-
if !ok {
43-
t.Error("wrong")
44-
}
45-
}
46-
47-
func Test_AnyBy_False(t *testing.T) {
48-
ok := Chain2([]testModel{
49-
{ID: 1, Name: "one"},
50-
{ID: 2, Name: "two"},
51-
{ID: 3, Name: "three"},
52-
}).AnyBy(map[string]interface{}{
53-
"id": 0,
54-
})
55-
if ok {
56-
t.Error("wrong")
57-
}
58-
}
59-
60-
func Test_AnyBy_True(t *testing.T) {
61-
ok := Chain2([]testModel{
62-
{ID: 1, Name: "one"},
63-
{ID: 2, Name: "two"},
64-
{ID: 3, Name: "three"},
65-
}).AnyBy(map[string]interface{}{
66-
"name": "two",
67-
})
68-
if !ok {
69-
t.Error("wrong")
70-
}
71-
}
72-
73-
func Test_Chain_Any_False(t *testing.T) {
7414
ok := Chain([]testModel{
7515
{ID: 1, Name: "one"},
7616
{ID: 2, Name: "two"},
@@ -83,7 +23,7 @@ func Test_Chain_Any_False(t *testing.T) {
8323
}
8424
}
8525

86-
func Test_Chain_Any_True(t *testing.T) {
26+
func Test_Any_True(t *testing.T) {
8727
ok := Chain([]testModel{
8828
{ID: 1, Name: "one"},
8929
{ID: 2, Name: "two"},
@@ -96,7 +36,7 @@ func Test_Chain_Any_True(t *testing.T) {
9636
}
9737
}
9838

99-
func Test_Chain_AnyBy_False(t *testing.T) {
39+
func Test_AnyBy_False(t *testing.T) {
10040
ok := Chain([]testModel{
10141
{ID: 1, Name: "one"},
10242
{ID: 2, Name: "two"},
@@ -109,7 +49,7 @@ func Test_Chain_AnyBy_False(t *testing.T) {
10949
}
11050
}
11151

112-
func Test_Chain_AnyBy_True(t *testing.T) {
52+
func Test_AnyBy_True(t *testing.T) {
11353
ok := Chain([]testModel{
11454
{ID: 1, Name: "one"},
11555
{ID: 2, Name: "two"},

as-parallel.go

-6
This file was deleted.

chain.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ package underscore
22

33
import "reflect"
44

5-
// Chain will cause all future method calls to return wrapped objects
6-
func Chain(source interface{}) IQuery {
7-
return &query{
8-
Source: source,
9-
}
10-
}
11-
12-
// Chain2 is 初始化
13-
func Chain2(src interface{}) IEnumerable {
5+
// Chain is 初始化
6+
func Chain(src interface{}) IEnumerable {
147
return chainFromRV(
158
reflect.ValueOf(src),
169
)

chain_test.go

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package underscore
22

33
import "testing"
44

5-
var (
6-
benchmarkSize = 1000000
7-
)
5+
var benchmarkSize = 1000000
86

97
type testModel struct {
108
ID int
@@ -17,21 +15,16 @@ type testNestedModel struct {
1715
Age int
1816
}
1917

20-
func Benchmark_Chain(b *testing.B) {
21-
for n := 0; n < b.N; n++ {
22-
var dst int
23-
Range(1, benchmarkSize, 1).Map(func(r, _ int) int {
24-
return -r
25-
}).Where(func(r, _ int) bool {
26-
return r < -20
27-
}).First().Value(&dst)
28-
}
18+
type testSelectManyModel struct {
19+
Str string
20+
Slice []string
21+
Array [2]int
2922
}
3023

31-
func Benchmark_Chain_New(b *testing.B) {
24+
func Benchmark_Chain(b *testing.B) {
3225
for n := 0; n < b.N; n++ {
3326
var dst int
34-
Range2(1, benchmarkSize, 1).Select(func(r, _ int) int {
27+
Range(1, benchmarkSize, 1).Select(func(r, _ int) int {
3528
return -r
3629
}).Where(func(r, _ int) bool {
3730
return r < -20

count_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "testing"
44

55
func Test_Count(t *testing.T) {
66
src := []string{"a", "b", "c"}
7-
dst := Chain2(src).Count()
7+
dst := Chain(src).Count()
88
if dst != len(src) {
99
t.Error("wrong")
1010
}

distinct_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "testing"
55
func Test_Distinct(t *testing.T) {
66
src := []int{1, 2, 1, 4, 1, 3}
77
dst := make([]int, 0)
8-
Chain2(src).Distinct(func(n, _ int) (int, error) {
8+
Chain(src).Distinct(func(n, _ int) (int, error) {
99
return n % 2, nil
1010
}).Value(&dst)
1111
if len(dst) != 2 {
@@ -16,7 +16,7 @@ func Test_Distinct(t *testing.T) {
1616
func Test_Distinct_SelectorIsNil(t *testing.T) {
1717
src := []int{1, 2, 1, 4, 1, 3}
1818
dst := make([]int, 0)
19-
Chain2(src).Distinct(nil).Value(&dst)
19+
Chain(src).Distinct(nil).Value(&dst)
2020
if len(dst) != 4 {
2121
t.Error(dst)
2222
}
@@ -29,7 +29,7 @@ func Test_DistinctBy(t *testing.T) {
2929
{ID: 3, Name: "a"},
3030
}
3131
dst := make([]testModel, 0)
32-
Chain2(src).DistinctBy("name").Value(&dst)
32+
Chain(src).DistinctBy("name").Value(&dst)
3333
if len(dst) != 1 {
3434
t.Error(dst)
3535
}

0 commit comments

Comments
 (0)