Skip to content

Commit 6136777

Browse files
feat(aggregation):
- extend WithoutKey method
1 parent 8d46367 commit 6136777

18 files changed

+1545
-17
lines changed

builder/aggregation/accumulators_builder.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func (b *accumulatorsBuilder) Sum(key string, expression any) *Builder {
3131
return b.parent
3232
}
3333

34+
func (b *accumulatorsBuilder) SumWithoutKey(expression any) *Builder {
35+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationSum, Value: expression})
36+
return b.parent
37+
}
38+
3439
func (b *accumulatorsBuilder) Push(key string, expression any) *Builder {
3540
e := bson.E{Key: types.AggregationPush, Value: expression}
3641
if !b.parent.tryMergeValue(key, e) {
@@ -39,6 +44,11 @@ func (b *accumulatorsBuilder) Push(key string, expression any) *Builder {
3944
return b.parent
4045
}
4146

47+
func (b *accumulatorsBuilder) PushWithoutKey(expression any) *Builder {
48+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationPush, Value: expression})
49+
return b.parent
50+
}
51+
4252
func (b *accumulatorsBuilder) Avg(key string, expression any) *Builder {
4353
e := bson.E{Key: types.AggregationAvg, Value: expression}
4454
if !b.parent.tryMergeValue(key, e) {
@@ -47,6 +57,11 @@ func (b *accumulatorsBuilder) Avg(key string, expression any) *Builder {
4757
return b.parent
4858
}
4959

60+
func (b *accumulatorsBuilder) AvgWithoutKey(expression any) *Builder {
61+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationAvg, Value: expression})
62+
return b.parent
63+
}
64+
5065
func (b *accumulatorsBuilder) First(key string, expression any) *Builder {
5166
e := bson.E{Key: types.AggregationFirst, Value: expression}
5267
if !b.parent.tryMergeValue(key, e) {
@@ -55,6 +70,11 @@ func (b *accumulatorsBuilder) First(key string, expression any) *Builder {
5570
return b.parent
5671
}
5772

73+
func (b *accumulatorsBuilder) FirstWithoutKey(expression any) *Builder {
74+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationFirst, Value: expression})
75+
return b.parent
76+
}
77+
5878
func (b *accumulatorsBuilder) Last(key string, expression any) *Builder {
5979
e := bson.E{Key: types.AggregationLast, Value: expression}
6080
if !b.parent.tryMergeValue(key, e) {
@@ -63,6 +83,11 @@ func (b *accumulatorsBuilder) Last(key string, expression any) *Builder {
6383
return b.parent
6484
}
6585

86+
func (b *accumulatorsBuilder) LastWithoutKey(expression any) *Builder {
87+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationLast, Value: expression})
88+
return b.parent
89+
}
90+
6691
func (b *accumulatorsBuilder) Min(key string, expression any) *Builder {
6792
e := bson.E{Key: types.AggregationMin, Value: expression}
6893
if !b.parent.tryMergeValue(key, e) {
@@ -71,10 +96,20 @@ func (b *accumulatorsBuilder) Min(key string, expression any) *Builder {
7196
return b.parent
7297
}
7398

99+
func (b *accumulatorsBuilder) MinWithoutKey(expression any) *Builder {
100+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationMin, Value: expression})
101+
return b.parent
102+
}
103+
74104
func (b *accumulatorsBuilder) Max(key string, expression any) *Builder {
75105
e := bson.E{Key: types.AggregationMax, Value: expression}
76106
if !b.parent.tryMergeValue(key, e) {
77107
b.parent.d = append(b.parent.d, bson.E{Key: key, Value: bson.D{e}})
78108
}
79109
return b.parent
80110
}
111+
112+
func (b *accumulatorsBuilder) MaxWithoutKey(expression any) *Builder {
113+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationMax, Value: expression})
114+
return b.parent
115+
}

builder/aggregation/accumulators_builder_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ func Test_accumulatorsBuilder_Sum(t *testing.T) {
3030
})
3131
}
3232

33+
func Test_accumulatorsBuilder_SumWithoutKey(t *testing.T) {
34+
testCases := []struct {
35+
name string
36+
expression any
37+
expected bson.D
38+
}{
39+
{
40+
name: "normal",
41+
expression: "$price",
42+
expected: bson.D{{Key: "$sum", Value: "$price"}},
43+
},
44+
}
45+
for _, tc := range testCases {
46+
t.Run(tc.name, func(t *testing.T) {
47+
assert.Equal(t, tc.expected, BsonBuilder().SumWithoutKey(tc.expression).Build())
48+
})
49+
}
50+
}
51+
3352
func Test_accumulatorsBuilder_Push(t *testing.T) {
3453
t.Run("single operation", func(t *testing.T) {
3554
assert.Equal(t, bson.D{bson.E{Key: "items", Value: bson.D{{Key: "$push", Value: "$item"}}}}, BsonBuilder().Push("items", "$item").Build())
@@ -39,6 +58,26 @@ func Test_accumulatorsBuilder_Push(t *testing.T) {
3958
})
4059
}
4160

61+
func Test_accumulatorsBuilder_PushWithoutKey(t *testing.T) {
62+
testCases := []struct {
63+
name string
64+
expression any
65+
expected bson.D
66+
}{
67+
{
68+
name: "normal",
69+
// { item: "$item", quantity: "$quantity" }
70+
expression: BsonBuilder().AddKeyValues("item", "$item").AddKeyValues("quantity", "$quantity").Build(),
71+
expected: bson.D{{Key: "$push", Value: bson.D{{Key: "item", Value: "$item"}, {Key: "quantity", Value: "$quantity"}}}},
72+
},
73+
}
74+
for _, tc := range testCases {
75+
t.Run(tc.name, func(t *testing.T) {
76+
assert.Equal(t, tc.expected, BsonBuilder().PushWithoutKey(tc.expression).Build())
77+
})
78+
}
79+
}
80+
4281
func Test_accumulatorsBuilder_Avg(t *testing.T) {
4382
t.Run("single operation", func(t *testing.T) {
4483
assert.Equal(t, bson.D{bson.E{Key: "avgAmount", Value: bson.D{{Key: "$avg", Value: "$price"}}}}, BsonBuilder().Avg("avgAmount", "$price").Build())
@@ -48,6 +87,26 @@ func Test_accumulatorsBuilder_Avg(t *testing.T) {
4887
})
4988
}
5089

90+
func Test_accumulatorsBuilder_AvgWithoutKey(t *testing.T) {
91+
testCases := []struct {
92+
name string
93+
expression any
94+
expected bson.D
95+
}{
96+
{
97+
name: "normal",
98+
// { $multiply: [ "$price", "$quantity" ] }
99+
expression: BsonBuilder().MultiplyWithoutKey("$price", "$quantity").Build(),
100+
expected: bson.D{{Key: "$avg", Value: bson.D{{Key: "$multiply", Value: []any{"$price", "$quantity"}}}}},
101+
},
102+
}
103+
for _, tc := range testCases {
104+
t.Run(tc.name, func(t *testing.T) {
105+
assert.Equal(t, tc.expected, BsonBuilder().AvgWithoutKey(tc.expression).Build())
106+
})
107+
}
108+
}
109+
51110
func Test_accumulatorsBuilder_First(t *testing.T) {
52111
t.Run("single operation", func(t *testing.T) {
53112
assert.Equal(t, bson.D{bson.E{Key: "firstType", Value: bson.D{{Key: "$first", Value: "$type"}}}}, BsonBuilder().First("firstType", "$type").Build())
@@ -57,6 +116,25 @@ func Test_accumulatorsBuilder_First(t *testing.T) {
57116
})
58117
}
59118

119+
func Test_accumulatorsBuilder_FirstWithoutKey(t *testing.T) {
120+
testCases := []struct {
121+
name string
122+
expression any
123+
expected bson.D
124+
}{
125+
{
126+
name: "normal",
127+
expression: "$type",
128+
expected: bson.D{{Key: "$first", Value: "$type"}},
129+
},
130+
}
131+
for _, tc := range testCases {
132+
t.Run(tc.name, func(t *testing.T) {
133+
assert.Equal(t, tc.expected, BsonBuilder().FirstWithoutKey(tc.expression).Build())
134+
})
135+
}
136+
}
137+
60138
func Test_accumulatorsBuilder_Last(t *testing.T) {
61139
t.Run("single operation", func(t *testing.T) {
62140
assert.Equal(t, bson.D{bson.E{Key: "lastType", Value: bson.D{{Key: "$last", Value: "$type"}}}}, BsonBuilder().Last("lastType", "$type").Build())
@@ -66,6 +144,25 @@ func Test_accumulatorsBuilder_Last(t *testing.T) {
66144
})
67145
}
68146

147+
func Test_accumulatorsBuilder_LastWithoutKey(t *testing.T) {
148+
testCases := []struct {
149+
name string
150+
expression any
151+
expected bson.D
152+
}{
153+
{
154+
name: "normal",
155+
expression: "$type",
156+
expected: bson.D{{Key: "$last", Value: "$type"}},
157+
},
158+
}
159+
for _, tc := range testCases {
160+
t.Run(tc.name, func(t *testing.T) {
161+
assert.Equal(t, tc.expected, BsonBuilder().LastWithoutKey(tc.expression).Build())
162+
})
163+
}
164+
}
165+
69166
func Test_accumulatorsBuilder_Min(t *testing.T) {
70167
t.Run("single operation", func(t *testing.T) {
71168
assert.Equal(t, bson.D{bson.E{Key: "minPrice", Value: bson.D{{Key: "$min", Value: "$price"}}}}, BsonBuilder().Min("minPrice", "$price").Build())
@@ -74,6 +171,24 @@ func Test_accumulatorsBuilder_Min(t *testing.T) {
74171
assert.Equal(t, bson.D{bson.E{Key: "minPrice", Value: bson.D{{Key: "$min", Value: "$price"}}}, bson.E{Key: "minFee", Value: bson.D{{Key: "$min", Value: "$fee"}}}}, BsonBuilder().Min("minPrice", "$price").Min("minFee", "$fee").Build())
75172
})
76173
}
174+
func Test_accumulatorsBuilder_MinWithoutKey(t *testing.T) {
175+
testCases := []struct {
176+
name string
177+
expression any
178+
expected bson.D
179+
}{
180+
{
181+
name: "normal",
182+
expression: "$price",
183+
expected: bson.D{{Key: "$min", Value: "$price"}},
184+
},
185+
}
186+
for _, tc := range testCases {
187+
t.Run(tc.name, func(t *testing.T) {
188+
assert.Equal(t, tc.expected, BsonBuilder().MinWithoutKey(tc.expression).Build())
189+
})
190+
}
191+
}
77192

78193
func Test_accumulatorsBuilder_Max(t *testing.T) {
79194
t.Run("single operation", func(t *testing.T) {
@@ -83,3 +198,22 @@ func Test_accumulatorsBuilder_Max(t *testing.T) {
83198
assert.Equal(t, bson.D{bson.E{Key: "maxPrice", Value: bson.D{{Key: "$max", Value: "$price"}}}, bson.E{Key: "maxFee", Value: bson.D{{Key: "$max", Value: "$fee"}}}}, BsonBuilder().Max("maxPrice", "$price").Max("maxFee", "$fee").Build())
84199
})
85200
}
201+
202+
func Test_accumulatorsBuilder_MaxWithoutKey(t *testing.T) {
203+
testCases := []struct {
204+
name string
205+
expression any
206+
expected bson.D
207+
}{
208+
{
209+
name: "normal",
210+
expression: "$price",
211+
expected: bson.D{{Key: "$max", Value: "$price"}},
212+
},
213+
}
214+
for _, tc := range testCases {
215+
t.Run(tc.name, func(t *testing.T) {
216+
assert.Equal(t, tc.expected, BsonBuilder().MaxWithoutKey(tc.expression).Build())
217+
})
218+
}
219+
}

builder/aggregation/arithmetic_builder.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func (b *arithmeticBuilder) Add(key string, expressions ...any) *Builder {
3131
return b.parent
3232
}
3333

34+
func (b *arithmeticBuilder) AddWithoutKey(expressions ...any) *Builder {
35+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationAdd, Value: expressions})
36+
return b.parent
37+
}
38+
3439
func (b *arithmeticBuilder) Multiply(key string, expressions ...any) *Builder {
3540
e := bson.E{Key: types.AggregationMultiply, Value: expressions}
3641
if !b.parent.tryMergeValue(key, e) {
@@ -39,6 +44,11 @@ func (b *arithmeticBuilder) Multiply(key string, expressions ...any) *Builder {
3944
return b.parent
4045
}
4146

47+
func (b *arithmeticBuilder) MultiplyWithoutKey(expressions ...any) *Builder {
48+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationMultiply, Value: expressions})
49+
return b.parent
50+
}
51+
4252
func (b *arithmeticBuilder) Subtract(key string, s string, start, length int64) *Builder {
4353
e := bson.E{Key: types.AggregationSubtract, Value: []any{s, start, length}}
4454
if !b.parent.tryMergeValue(key, e) {
@@ -47,6 +57,11 @@ func (b *arithmeticBuilder) Subtract(key string, s string, start, length int64)
4757
return b.parent
4858
}
4959

60+
func (b *arithmeticBuilder) SubtractWithoutKey(s string, start, length int64) *Builder {
61+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationSubtract, Value: []any{s, start, length}})
62+
return b.parent
63+
}
64+
5065
func (b *arithmeticBuilder) Divide(key string, expressions ...any) *Builder {
5166
e := bson.E{Key: types.AggregationDivide, Value: expressions}
5267
if !b.parent.tryMergeValue(key, e) {
@@ -55,10 +70,20 @@ func (b *arithmeticBuilder) Divide(key string, expressions ...any) *Builder {
5570
return b.parent
5671
}
5772

73+
func (b *arithmeticBuilder) DivideWithoutKey(expressions ...any) *Builder {
74+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationDivide, Value: expressions})
75+
return b.parent
76+
}
77+
5878
func (b *arithmeticBuilder) Mod(key string, expressions ...any) *Builder {
5979
e := bson.E{Key: types.AggregationMod, Value: expressions}
6080
if !b.parent.tryMergeValue(key, e) {
6181
b.parent.d = append(b.parent.d, bson.E{Key: key, Value: bson.D{e}})
6282
}
6383
return b.parent
6484
}
85+
86+
func (b *arithmeticBuilder) ModWithoutKey(expressions ...any) *Builder {
87+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationMod, Value: expressions})
88+
return b.parent
89+
}

0 commit comments

Comments
 (0)