Skip to content

Commit fdda798

Browse files
cleitonmarxahmetb
authored andcommitted
Removed typed files (#47)
* Removed typed files * Removed unnecessary tests * Added mustPanicWithError utility function for tests
1 parent 0273c4d commit fdda798

Some content is hidden

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

64 files changed

+2343
-3631
lines changed

Diff for: aggregate.go

+78-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ func (q Query) Aggregate(
2626
return result
2727
}
2828

29+
// AggregateT is the typed version of Aggregate.
30+
//
31+
// NOTE: Aggregate method has better performance than AggregateT
32+
//
33+
// f is of type: func(TSource, TSource) TSource
34+
func (q Query) AggregateT(f interface{}) interface{} {
35+
36+
fGenericFunc, err := newGenericFunc(
37+
"AggregateT", "f", f,
38+
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
39+
)
40+
if err != nil {
41+
panic(err)
42+
}
43+
44+
fFunc := func(result interface{}, current interface{}) interface{} {
45+
return fGenericFunc.Call(result, current)
46+
}
47+
48+
return q.Aggregate(fFunc)
49+
}
50+
2951
// AggregateWithSeed applies an accumulator function over a sequence.
3052
// The specified seed value is used as the initial accumulator value.
3153
//
@@ -52,6 +74,27 @@ func (q Query) AggregateWithSeed(
5274
return result
5375
}
5476

77+
// AggregateWithSeedT is the typed version of AggregateWithSeed.
78+
//
79+
// NOTE: AggregateWithSeed method has better performance than AggregateWithSeedT
80+
//
81+
// f is of a type "func(TAccumulate, TSource) TAccumulate"
82+
func (q Query) AggregateWithSeedT(seed interface{}, f interface{}) interface{} {
83+
fGenericFunc, err := newGenericFunc(
84+
"AggregateWithSeed", "f", f,
85+
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
86+
)
87+
if err != nil {
88+
panic(err)
89+
}
90+
91+
fFunc := func(result interface{}, current interface{}) interface{} {
92+
return fGenericFunc.Call(result, current)
93+
}
94+
95+
return q.AggregateWithSeed(seed, fFunc)
96+
}
97+
5598
// AggregateWithSeedBy applies an accumulator function over a sequence.
5699
// The specified seed value is used as the initial accumulator value,
57100
// and the specified function is used to select the result value.
@@ -63,7 +106,6 @@ func (q Query) AggregateWithSeed(
63106
// as the initial aggregate value. The result of func replaces the previous aggregated value.
64107
//
65108
// The final result of func is passed to resultSelector to obtain the final result of Aggregate.
66-
//
67109
func (q Query) AggregateWithSeedBy(
68110
seed interface{},
69111
f func(interface{}, interface{}) interface{},
@@ -79,3 +121,38 @@ func (q Query) AggregateWithSeedBy(
79121

80122
return resultSelector(result)
81123
}
124+
125+
// AggregateWithSeedByT is the typed version of AggregateWithSeedBy.
126+
//
127+
// NOTE: AggregateWithSeedBy method has better performance than AggregateWithSeedByT
128+
//
129+
// f is of a type "func(TAccumulate, TSource) TAccumulate"
130+
//
131+
// resultSelectorFn is of type "func(TAccumulate) TResult"
132+
func (q Query) AggregateWithSeedByT(seed interface{}, f interface{}, resultSelectorFn interface{}) interface{} {
133+
fGenericFunc, err := newGenericFunc(
134+
"AggregateWithSeedByT", "f", f,
135+
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
136+
)
137+
if err != nil {
138+
panic(err)
139+
}
140+
141+
fFunc := func(result interface{}, current interface{}) interface{} {
142+
return fGenericFunc.Call(result, current)
143+
}
144+
145+
resultSelectorGenericFunc, err := newGenericFunc(
146+
"AggregateWithSeedByT", "resultSelectorFn", resultSelectorFn,
147+
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
148+
)
149+
if err != nil {
150+
panic(err)
151+
}
152+
153+
resultSelectorFunc := func(result interface{}) interface{} {
154+
return resultSelectorGenericFunc.Call(result)
155+
}
156+
157+
return q.AggregateWithSeedBy(seed, fFunc, resultSelectorFunc)
158+
}

Diff for: aggregate_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ func TestAggregate(t *testing.T) {
2626
}
2727
}
2828

29+
func TestAggregateT_PanicWhenFunctionIsInvalid(t *testing.T) {
30+
mustPanicWithError(t, "AggregateT: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
31+
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateT(func(x int, r string, i string) string {
32+
if len(r) > len(i) {
33+
return r
34+
}
35+
return i
36+
})
37+
})
38+
}
39+
2940
func TestAggregateWithSeed(t *testing.T) {
3041
input := []string{"apple", "mango", "orange", "banana", "grape"}
3142
want := "passionfruit"
@@ -43,6 +54,17 @@ func TestAggregateWithSeed(t *testing.T) {
4354
}
4455
}
4556

57+
func TestAggregateWithSeedT_PanicWhenFunctionIsInvalid(t *testing.T) {
58+
mustPanicWithError(t, "AggregateWithSeed: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
59+
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedT(3, func(x int, r string, i string) string {
60+
if len(r) > len(i) {
61+
return r
62+
}
63+
return i
64+
})
65+
})
66+
}
67+
4668
func TestAggregateWithSeedBy(t *testing.T) {
4769
input := []string{"apple", "mango", "orange", "passionfruit", "grape"}
4870
want := "PASSIONFRUIT"
@@ -63,3 +85,35 @@ func TestAggregateWithSeedBy(t *testing.T) {
6385
t.Errorf("From(%v).AggregateWithSeed()=%v expected %v", input, r, want)
6486
}
6587
}
88+
89+
func TestAggregateWithSeedByT_PanicWhenFunctionIsInvalid(t *testing.T) {
90+
mustPanicWithError(t, "AggregateWithSeedByT: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
91+
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedByT(3,
92+
func(x int, r string, i string) string {
93+
if len(r) > len(i) {
94+
return r
95+
}
96+
return i
97+
},
98+
func(r string) string {
99+
return r
100+
},
101+
)
102+
})
103+
}
104+
105+
func TestAggregateWithSeedByT_PanicWhenResultSelectorFnIsInvalid(t *testing.T) {
106+
mustPanicWithError(t, "AggregateWithSeedByT: parameter [resultSelectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(string,int)string'", func() {
107+
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedByT(3,
108+
func(x int, r int) int {
109+
if x > r {
110+
return x
111+
}
112+
return r
113+
},
114+
func(r string, t int) string {
115+
return r
116+
},
117+
)
118+
})
119+
}

Diff for: aggregatetyped.go

-88
This file was deleted.

0 commit comments

Comments
 (0)