Skip to content

Commit 4abfe12

Browse files
Merge pull request #23 from chenmingyong0423/feature/build
refactor(bsonx): reconstructed D and A function
2 parents 19ea5e0 + 9da4c14 commit 4abfe12

File tree

9 files changed

+28
-72
lines changed

9 files changed

+28
-72
lines changed

bsonx/bsonx.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,16 @@ func E(key string, value any) bson.E {
2626
return bson.E{Key: key, Value: value}
2727
}
2828

29-
func A[T any](values ...T) bson.A {
29+
func A(values ...any) bson.A {
3030
value := make(bson.A, 0, len(values))
3131
for _, v := range values {
3232
value = append(value, v)
3333
}
3434
return value
3535
}
3636

37-
func D(bsonElements ...bson.E) bson.D {
38-
value := make(bson.D, 0, len(bsonElements))
39-
for _, element := range bsonElements {
40-
value = append(value, element)
41-
}
42-
return value
37+
func D(key string, value any) bson.D {
38+
return bson.D{bson.E{Key: key, Value: value}}
4339
}
4440

4541
func Id(value any) bson.M {

bsonx/bsonx_test.go

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,7 @@ import (
2222
)
2323

2424
func TestD(t *testing.T) {
25-
testCases := []struct {
26-
name string
27-
input []bson.E
28-
expected bson.D
29-
}{
30-
{
31-
name: "nil input",
32-
input: nil,
33-
expected: bson.D{},
34-
},
35-
{
36-
name: "empty input",
37-
input: []bson.E{},
38-
expected: bson.D{},
39-
},
40-
{
41-
name: "one element",
42-
input: []bson.E{
43-
E("name", "chenmingyong"),
44-
},
45-
expected: bson.D{
46-
{Key: "name", Value: "chenmingyong"},
47-
},
48-
},
49-
{
50-
name: "many elements",
51-
input: []bson.E{
52-
E("name", "chenmingyong"),
53-
E("age", 24),
54-
},
55-
expected: bson.D{
56-
{Key: "name", Value: "chenmingyong"},
57-
{Key: "age", Value: 24},
58-
},
59-
},
60-
}
61-
for _, tc := range testCases {
62-
t.Run(tc.name, func(t *testing.T) {
63-
assert.Equal(t, tc.expected, D(tc.input...))
64-
})
65-
}
25+
assert.Equal(t, bson.D{bson.E{Key: "name", Value: "Mingyong Chen"}}, D("name", "Mingyong Chen"))
6626
}
6727

6828
func TestId(t *testing.T) {
@@ -76,7 +36,7 @@ func TestE(t *testing.T) {
7636
func TestA(t *testing.T) {
7737
testCases := []struct {
7838
name string
79-
values []string
39+
values []any
8040
want bson.A
8141
}{
8242
{
@@ -86,12 +46,12 @@ func TestA(t *testing.T) {
8646
},
8747
{
8848
name: "empty values",
89-
values: []string{},
49+
values: []any{},
9050
want: bson.A{},
9151
},
9252
{
9353
name: "multiple values",
94-
values: []string{"1", "2"},
54+
values: []any{"1", "2"},
9555
want: bson.A{"1", "2"},
9656
},
9757
}

builder/aggregation/aggregation_stage_builder_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func TestStageBuilder_Group(t *testing.T) {
333333
name: "id is nil",
334334
id: nil,
335335
accumulators: BsonBuilder().
336-
Sum("totalSaleAmount", bsonx.D(bsonx.E("$multiply", []any{"$price", "$quantity"}))).
336+
Sum("totalSaleAmount", bsonx.D("$multiply", []any{"$price", "$quantity"})).
337337
Avg("averageQuantity", "$quantity").
338338
Sum("count", 1).Build(),
339339
want: mongo.Pipeline{
@@ -347,7 +347,7 @@ func TestStageBuilder_Group(t *testing.T) {
347347
},
348348
{
349349
name: "accumulators is nil",
350-
id: bsonx.D(bsonx.E("x", "$x")),
350+
id: bsonx.D("x", "$x"),
351351
accumulators: nil,
352352
want: mongo.Pipeline{
353353
bson.D{bson.E{Key: "$group", Value: bson.D{
@@ -356,9 +356,9 @@ func TestStageBuilder_Group(t *testing.T) {
356356
},
357357
{
358358
name: "id and accumulators are not nil",
359-
id: bsonx.D(bsonx.E("x", "$x")),
359+
id: bsonx.D("x", "$x"),
360360
accumulators: BsonBuilder().
361-
Sum("totalSaleAmount", bsonx.D(bsonx.E("$multiply", []any{"$price", "$quantity"}))).
361+
Sum("totalSaleAmount", bsonx.D("$multiply", []any{"$price", "$quantity"})).
362362
Avg("averageQuantity", "$quantity").
363363
Sum("count", 1).Build(),
364364
want: mongo.Pipeline{

builder/aggregation/bson_construction_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func Test_Lte(t *testing.T) {
280280
}
281281

282282
func Test_Cond(t *testing.T) {
283-
assert.Equal(t, bson.D{bson.E{Key: "discount", Value: bson.D{{Key: "$cond", Value: []any{bson.D{{Key: "$gte", Value: []any{"$qty", 250}}}, 30, 20}}}}}, Cond("discount", bsonx.D(bsonx.E("$gte", []any{"$qty", 250})), 30, 20))
283+
assert.Equal(t, bson.D{bson.E{Key: "discount", Value: bson.D{{Key: "$cond", Value: []any{bson.D{{Key: "$gte", Value: []any{"$qty", 250}}}, 30, 20}}}}}, Cond("discount", bsonx.D("$gte", []any{"$qty", 250}), 30, 20))
284284
}
285285

286286
func Test_IfNull(t *testing.T) {
@@ -300,10 +300,10 @@ func Test_Switch(t *testing.T) {
300300
}}},
301301
Switch("summary", []types.CaseThen{
302302
{
303-
Case: bsonx.D(bsonx.E("$eq", []any{0, 5})), Then: "equals",
303+
Case: bsonx.D("$eq", []any{0, 5}), Then: "equals",
304304
},
305305
{
306-
Case: bsonx.D(bsonx.E("$gt", []any{0, 5})), Then: "greater than",
306+
Case: bsonx.D("$gt", []any{0, 5}), Then: "greater than",
307307
},
308308
}, "Did not match"),
309309
)
@@ -427,15 +427,15 @@ func Test_DateToString(t *testing.T) {
427427
}
428428

429429
func Test_And(t *testing.T) {
430-
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$and", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 100}}}, bson.D{bson.E{Key: "$lt", Value: []any{"$qty", 250}}}}}}}}, And("item", bsonx.D(bsonx.E("$gt", []any{"$qty", 100})), bsonx.D(bsonx.E("$lt", []any{"$qty", 250}))))
430+
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$and", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 100}}}, bson.D{bson.E{Key: "$lt", Value: []any{"$qty", 250}}}}}}}}, And("item", bsonx.D("$gt", []any{"$qty", 100}), bsonx.D("$lt", []any{"$qty", 250})))
431431
}
432432

433433
func Test_Not(t *testing.T) {
434-
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$not", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 250}}}}}}}}, Not("item", bsonx.D(bsonx.E("$gt", []any{"$qty", 250}))))
434+
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$not", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 250}}}}}}}}, Not("item", bsonx.D("$gt", []any{"$qty", 250})))
435435
}
436436

437437
func Test_Or(t *testing.T) {
438-
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$or", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 250}}}, bson.D{bson.E{Key: "$lt", Value: []any{"$qty", 50}}}}}}}}, Or("item", bsonx.D(bsonx.E("$gt", []any{"$qty", 250})), bsonx.D(bsonx.E("$lt", []any{"$qty", 50}))))
438+
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$or", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 250}}}, bson.D{bson.E{Key: "$lt", Value: []any{"$qty", 50}}}}}}}}, Or("item", bsonx.D("$gt", []any{"$qty", 250}), bsonx.D("$lt", []any{"$qty", 50})))
439439
}
440440

441441
func Test_Concat(t *testing.T) {

builder/aggregation/cond_builder_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ func Test_condBuilder_Switch(t *testing.T) {
105105
}}},
106106
BsonBuilder().Switch("summary", []types.CaseThen{
107107
{
108-
Case: bsonx.D(bsonx.E("$eq", []any{0, 5})), Then: "equals",
108+
Case: bsonx.D("$eq", []any{0, 5}), Then: "equals",
109109
},
110110
{
111-
Case: bsonx.D(bsonx.E("$gt", []any{0, 5})), Then: "greater than",
111+
Case: bsonx.D("$gt", []any{0, 5}), Then: "greater than",
112112
},
113113
}, "Did not match").Build(),
114114
)
@@ -147,10 +147,10 @@ func Test_condBuilder_SwitchWithoutKey(t *testing.T) {
147147
name: "normal",
148148
cases: []types.CaseThen{
149149
{
150-
Case: bsonx.D(bsonx.E("$eq", []any{0, 5})), Then: "equals",
150+
Case: bsonx.D("$eq", []any{0, 5}), Then: "equals",
151151
},
152152
{
153-
Case: bsonx.D(bsonx.E("$gt", []any{0, 5})), Then: "greater than",
153+
Case: bsonx.D("$gt", []any{0, 5}), Then: "greater than",
154154
},
155155
},
156156
defaultCase: "Did not match",

builder/aggregation/logical_builder_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
func Test_logicalBuilder_And(t *testing.T) {
2929
t.Run("test And", func(t *testing.T) {
30-
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$and", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 100}}}, bson.D{bson.E{Key: "$lt", Value: []any{"$qty", 250}}}}}}}}, BsonBuilder().And("item", bsonx.D(bsonx.E("$gt", []any{"$qty", 100})), bsonx.D(bsonx.E("$lt", []any{"$qty", 250}))).Build())
30+
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$and", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 100}}}, bson.D{bson.E{Key: "$lt", Value: []any{"$qty", 250}}}}}}}}, BsonBuilder().And("item", bsonx.D("$gt", []any{"$qty", 100}), bsonx.D("$lt", []any{"$qty", 250})).Build())
3131
})
3232
}
3333

@@ -61,7 +61,7 @@ func Test_logicalBuilder_AndWithoutKey(t *testing.T) {
6161
}
6262

6363
func Test_logicalBuilder_Not(t *testing.T) {
64-
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$not", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 250}}}}}}}}, BsonBuilder().Not("item", bsonx.D(bsonx.E("$gt", []any{"$qty", 250}))).Build())
64+
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$not", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 250}}}}}}}}, BsonBuilder().Not("item", bsonx.D("$gt", []any{"$qty", 250})).Build())
6565
}
6666

6767
func Test_logicalBuilder_NotWithoutKey(t *testing.T) {
@@ -94,7 +94,7 @@ func Test_logicalBuilder_NotWithoutKey(t *testing.T) {
9494
}
9595

9696
func Test_logicalBuilder_Or(t *testing.T) {
97-
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$or", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 250}}}, bson.D{bson.E{Key: "$lt", Value: []any{"$qty", 50}}}}}}}}, BsonBuilder().Or("item", bsonx.D(bsonx.E("$gt", []any{"$qty", 250})), bsonx.D(bsonx.E("$lt", []any{"$qty", 50}))).Build())
97+
assert.Equal(t, bson.D{bson.E{Key: "item", Value: bson.D{bson.E{Key: "$or", Value: []any{bson.D{bson.E{Key: "$gt", Value: []any{"$qty", 250}}}, bson.D{bson.E{Key: "$lt", Value: []any{"$qty", 50}}}}}}}}, BsonBuilder().Or("item", bsonx.D("$gt", []any{"$qty", 250}), bsonx.D("$lt", []any{"$qty", 50})).Build())
9898
}
9999

100100
func Test_logicalBuilder_OrWithoutKey(t *testing.T) {

builder/update/array_update_builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func Test_arrayUpdateBuilder_Pull(t *testing.T) {
5959
assert.Equal(
6060
t,
6161
bson.D{bson.E{Key: "$pull", Value: bson.D{bson.E{Key: "fruits", Value: bson.D{bson.E{Key: "$in", Value: []string{"apples", "oranges"}}}}}}},
62-
BsonBuilder().Pull("fruits", bsonx.D(bsonx.E("$in", []string{"apples", "oranges"}))).Build(),
62+
BsonBuilder().Pull("fruits", bsonx.D("$in", []string{"apples", "oranges"})).Build(),
6363
)
6464
})
6565

builder/update/field_update_builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ func Test_fieldUpdateBuilder_CurrentDate(t *testing.T) {
101101
})
102102

103103
t.Run("multiple operation", func(t *testing.T) {
104-
assert.Equal(t, bson.D{{Key: "$currentDate", Value: bson.D{bson.E{Key: "lastModified", Value: true}, bson.E{Key: "cancellation.date", Value: bson.D{bson.E{Key: "$type", Value: "timestamp"}}}}}}, BsonBuilder().CurrentDate("lastModified", true).CurrentDate("cancellation.date", bsonx.D(bsonx.E("$type", "timestamp"))).Build())
104+
assert.Equal(t, bson.D{{Key: "$currentDate", Value: bson.D{bson.E{Key: "lastModified", Value: true}, bson.E{Key: "cancellation.date", Value: bson.D{bson.E{Key: "$type", Value: "timestamp"}}}}}}, BsonBuilder().CurrentDate("lastModified", true).CurrentDate("cancellation.date", bsonx.D("$type", "timestamp")).Build())
105105
})
106106
}

finder/finder_e2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func TestFinder_e2e_Find(t *testing.T) {
402402
require.Equal(t, int64(2), deleteResult.DeletedCount)
403403
finder.filter = bson.D{}
404404
},
405-
filter: bsonx.D(),
405+
filter: bson.D{},
406406
wantErr: require.Error,
407407
},
408408
{
@@ -453,7 +453,7 @@ func TestFinder_e2e_Find(t *testing.T) {
453453
require.Equal(t, int64(2), deleteResult.DeletedCount)
454454
finder.filter = bson.D{}
455455
},
456-
filter: bsonx.D(),
456+
filter: bson.D{},
457457
want: []*types.TestUser{
458458
{
459459
Name: "chenmingyong",

0 commit comments

Comments
 (0)