Skip to content

Commit 19ea5e0

Browse files
Merge pull request #22 from chenmingyong0423/feature/build
refactor aggregation/subtract
2 parents d14d305 + 2638887 commit 19ea5e0

File tree

4 files changed

+22
-30
lines changed

4 files changed

+22
-30
lines changed

builder/aggregation/arithmetic_builder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ func (b *arithmeticBuilder) MultiplyWithoutKey(expressions ...any) *Builder {
4949
return b.parent
5050
}
5151

52-
func (b *arithmeticBuilder) Subtract(key string, s string, start, length int64) *Builder {
53-
e := bson.E{Key: types.AggregationSubtract, Value: []any{s, start, length}}
52+
func (b *arithmeticBuilder) Subtract(key string, expressions ...any) *Builder {
53+
e := bson.E{Key: types.AggregationSubtract, Value: expressions}
5454
if !b.parent.tryMergeValue(key, e) {
5555
b.parent.d = append(b.parent.d, bson.E{Key: key, Value: bson.D{e}})
5656
}
5757
return b.parent
5858
}
5959

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}})
60+
func (b *arithmeticBuilder) SubtractWithoutKey(expressions ...any) *Builder {
61+
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationSubtract, Value: expressions})
6262
return b.parent
6363
}
6464

builder/aggregation/arithmetic_builder_test.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,28 @@ func Test_arithmeticBuilder_MultiplyWithoutKey(t *testing.T) {
107107

108108
func Test_arithmeticBuilder_Subtract(t *testing.T) {
109109
t.Run("test subtract", func(t *testing.T) {
110-
assert.Equal(t, bson.D{bson.E{Key: "total", Value: bson.D{bson.E{Key: "$subtract", Value: []any{"$quarter", int64(0), int64(2)}}}}},
111-
BsonBuilder().Subtract("total", "$quarter", int64(0), int64(2)).Build(),
110+
assert.Equal(t, bson.D{bson.E{Key: "dateDifference", Value: bson.D{bson.E{Key: "$subtract", Value: []any{"$date", 5 * 60 * 1000}}}}},
111+
BsonBuilder().Subtract("dateDifference", []any{"$date", 5 * 60 * 1000}...).Build(),
112112
)
113113
})
114114

115115
}
116116

117117
func Test_arithmeticBuilder_SubtractWithoutKey(t *testing.T) {
118118
testCases := []struct {
119-
name string
120-
s string
121-
start int64
122-
length int64
123-
expected bson.D
119+
name string
120+
expressions []any
121+
expected bson.D
124122
}{
125123
{
126-
name: "normal",
127-
s: "$quarter",
128-
start: 0,
129-
length: 2,
130-
expected: bson.D{bson.E{Key: "$subtract", Value: []any{"$quarter", int64(0), int64(2)}}},
124+
name: "normal",
125+
expressions: []any{"$date", 5 * 60 * 1000},
126+
expected: bson.D{bson.E{Key: "$subtract", Value: []any{"$date", 5 * 60 * 1000}}},
131127
},
132128
}
133129
for _, tc := range testCases {
134130
t.Run(tc.name, func(t *testing.T) {
135-
assert.Equal(t, tc.expected, BsonBuilder().SubtractWithoutKey(tc.s, tc.start, tc.length).Build())
131+
assert.Equal(t, tc.expected, BsonBuilder().SubtractWithoutKey(tc.expressions...).Build())
136132
})
137133
}
138134
}

builder/aggregation/bson_construction_without_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func MultiplyWithoutKey(expressions ...any) bson.D {
5757
return bson.D{{Key: types.AggregationMultiply, Value: expressions}}
5858
}
5959

60-
func SubtractWithoutKey(s string, start, length int64) bson.D {
61-
return bson.D{{Key: types.AggregationSubtract, Value: []any{s, start, length}}}
60+
func SubtractWithoutKey(expressions ...any) bson.D {
61+
return bson.D{{Key: types.AggregationSubtract, Value: expressions}}
6262
}
6363

6464
func DivideWithoutKey(expressions ...any) bson.D {

builder/aggregation/bson_construction_without_key_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,23 +244,19 @@ func TestMultiplyWithoutKey(t *testing.T) {
244244

245245
func TestSubtractWithoutKey(t *testing.T) {
246246
testCases := []struct {
247-
name string
248-
s string
249-
start int64
250-
length int64
251-
want bson.D
247+
name string
248+
expressions []any
249+
expected bson.D
252250
}{
253251
{
254-
name: "normal",
255-
s: "$quarter",
256-
start: 0,
257-
length: 2,
258-
want: bson.D{bson.E{Key: "$subtract", Value: []any{"$quarter", int64(0), int64(2)}}},
252+
name: "normal",
253+
expressions: []any{"$date", 5 * 60 * 1000},
254+
expected: bson.D{bson.E{Key: "$subtract", Value: []any{"$date", 5 * 60 * 1000}}},
259255
},
260256
}
261257
for _, tc := range testCases {
262258
t.Run(tc.name, func(t *testing.T) {
263-
assert.Equal(t, tc.want, SubtractWithoutKey(tc.s, tc.start, tc.length))
259+
assert.Equal(t, tc.expected, SubtractWithoutKey(tc.expressions...))
264260
})
265261
}
266262
}

0 commit comments

Comments
 (0)