Skip to content

Commit 147c99a

Browse files
author
phuong
committed
update pointer D
1 parent 21615e5 commit 147c99a

File tree

16 files changed

+139
-112
lines changed

16 files changed

+139
-112
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ You can walkaround to find out more commands, but here are some
2626

2727
#### You might using comparision queries like this:
2828
```go
29-
filter := moper.D{}.
29+
filter := moper.NewD().
3030
Equal("damage", 10).
3131
EqualLess("health", 100).
3232
Greater("speed", 20.1)
@@ -41,7 +41,7 @@ filter := moper.Init(
4141

4242
#### Update commands
4343
```go
44-
update := moper.D{}.Set(
44+
update := moper.NewD().Set(
4545
moper.P{"damage", 10},
4646
moper.P{"health", 1},
4747
).Inc(
@@ -53,10 +53,10 @@ Support simple aggregation:
5353
```go
5454
intArr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
5555

56-
matchStage := moper.D{}.MatchD(moper.D{}.InArray("damage", intArr))
57-
groupStage := moper.D{}.Group(
56+
matchStage := moper.NewD().MatchD(moper.NewD().InArray("damage", intArr))
57+
groupStage := moper.NewD().Group(
5858
moper.P{K: "_id", V: nil},
59-
moper.P{K: "total", V: moper.D{}.Sum("damage")},
59+
moper.P{K: "total", V: moper.NewD().Sum("damage")},
6060
)
6161

6262
req := &mocom.AggregationRequest[Hero]{
@@ -92,8 +92,8 @@ type Model interface {
9292
}
9393

9494
// Hero is Model
95-
filter := moper.D{}.Equal("damage", -i)
96-
update := moper.D{}.Set(moper.P{K: "damage", V: i})
95+
filter := moper.NewD().Equal("damage", -i)
96+
update := moper.NewD().Set(moper.P{K: "damage", V: i})
9797

9898
result, err := mocom.UpdateMany[Hero](ctx, filter, update)
9999
```

internal/mopertest/aggregation_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
func TestAggregation(t *testing.T) {
1515
intArr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
1616

17-
matchStage := moper.D{}.MatchD(moper.D{}.InArray("damage", intArr))
18-
groupStage := moper.D{}.Group(
17+
matchStage := moper.NewD().MatchD(*moper.NewD().InArray("damage", intArr))
18+
groupStage := moper.NewD().Group(
1919
moper.P{K: "_id", V: nil},
20-
moper.P{K: "total", V: moper.D{}.Sum("damage")},
20+
moper.P{K: "total", V: moper.NewD().Sum("damage")},
2121
)
2222

2323
req := &mocom.AggregationRequest[Hero]{
24-
Pipeline: []moper.D{matchStage, groupStage},
24+
Pipeline: []*moper.D{matchStage, groupStage},
2525
Options: []*options.AggregateOptions{},
2626
}
2727
result, err := mocom.Aggregate(context.Background(), req)
@@ -43,18 +43,18 @@ func TestAggregation(t *testing.T) {
4343

4444
func TestLookup(t *testing.T) {
4545
intArr := []int{1}
46-
matchStage := moper.D{}.MatchD(moper.D{}.InArray("damage", intArr))
46+
matchStage := moper.NewD().MatchD(*moper.NewD().InArray("damage", intArr))
4747

48-
lookupStage := moper.D{}.LookUp().
48+
lookupStage := moper.NewD().LookUp().
4949
From(Weapon{}.CollName()).
5050
LocalField("damage").
5151
ForeignField("damage").
5252
As("weapon")
5353

54-
unwindStage := moper.D{}.Equal("$unwind", moper.D{}.Equal("path", "$weapon").Equal("preserveNullAndEmptyArrays", false))
54+
unwindStage := moper.NewD().Equal("$unwind", moper.NewD().Equal("path", "$weapon").Equal("preserveNullAndEmptyArrays", false))
5555

5656
req := &mocom.AggregationRequest[Hero]{
57-
Pipeline: []moper.D{matchStage, lookupStage.D(), unwindStage},
57+
Pipeline: []*moper.D{matchStage, lookupStage.D(), unwindStage},
5858
Options: []*options.AggregateOptions{},
5959
}
6060
result, err := mocom.Aggregate(context.Background(), req)

internal/mopertest/comparision_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestEquals(t *testing.T) {
1212
ctx := context.Background()
1313
for i := 0; i < ROUND; i++ {
14-
filter := moper.D{}.Equal("damage", i+1)
14+
filter := moper.NewD().Equal("damage", i+1)
1515

1616
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
1717
t.Error("[TestEquals]:", err)
@@ -27,7 +27,7 @@ func TestNotEquals(t *testing.T) {
2727
for i := 0; i < ROUND; i++ {
2828
num := i + 1
2929

30-
filter := moper.D{}.NotEqual("damage", num)
30+
filter := moper.NewD().NotEqual("damage", num)
3131

3232
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
3333
t.Error("[TestNotEquals]:", err)
@@ -47,7 +47,7 @@ func TestIn(t *testing.T) {
4747
for j := 0; j < i; j++ {
4848
dmg2 := j + 1
4949

50-
filter := moper.D{}.InEll("damage", dmg1, dmg2)
50+
filter := moper.NewD().InEll("damage", dmg1, dmg2)
5151

5252
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
5353
t.Error("[TestIn]", err)
@@ -67,7 +67,7 @@ func TestNotIn(t *testing.T) {
6767
for j := 0; j < i; j++ {
6868
dmg2 := j + 1
6969

70-
filter := moper.D{}.NotInEll("damage", dmg1, dmg2)
70+
filter := moper.NewD().NotInEll("damage", dmg1, dmg2)
7171

7272
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
7373
t.Error("[TestNotIn]:", err)
@@ -84,7 +84,7 @@ func TestLess(t *testing.T) {
8484
for i := 0; i <= ROUND; i++ {
8585
num := i * (i - 1) / 2
8686

87-
filter := moper.D{}.Less("damage", i)
87+
filter := moper.NewD().Less("damage", i)
8888

8989
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
9090
t.Error("[TestLess]:", err)
@@ -100,7 +100,7 @@ func TestEqualLess(t *testing.T) {
100100
for i := 0; i <= ROUND; i++ {
101101
num := i * (i + 1) / 2
102102

103-
filter := moper.D{}.EqualLess("damage", i)
103+
filter := moper.NewD().EqualLess("damage", i)
104104

105105
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
106106
t.Error("[TestEqualLess]:", err)
@@ -116,7 +116,7 @@ func TestGreater(t *testing.T) {
116116
for i := 0; i <= ROUND; i++ {
117117
num := TOTAL - i*(i+1)/2
118118

119-
filter := moper.D{}.Greater("damage", i)
119+
filter := moper.NewD().Greater("damage", i)
120120

121121
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
122122
t.Error("[TestGreater]:", err)
@@ -133,7 +133,7 @@ func TestEqualGreater(t *testing.T) {
133133
for i := 0; i <= ROUND; i++ {
134134
num := TOTAL - i*(i-1)/2
135135

136-
filter := moper.D{}.EqualGreater("damage", i)
136+
filter := moper.NewD().EqualGreater("damage", i)
137137

138138
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
139139
t.Error("[TestEqualGreater]:", err)

internal/mopertest/element_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010

1111
func TestExists(t *testing.T) {
1212
ctx := context.Background()
13-
filter := moper.D{}.Exists("omit", true)
13+
filter := moper.NewD().Exists("omit", true)
1414
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
1515
t.Error("[TestExists]", err)
1616
} else if count != int64(ROUND) {
1717
t.Error("[TestExists]", count, "!=", int64(ROUND))
1818
}
1919

20-
filter = moper.D{}.Exists("omit", false)
20+
filter = moper.NewD().Exists("omit", false)
2121
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
2222
t.Error("[TestExists]", err)
2323
} else if count != int64(ROUND*(ROUND-1)/2) {

internal/mopertest/logical_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ func TestOr(t *testing.T) {
1313

1414
for i := 1; i < ROUND; i++ {
1515
for j := i + 1; j <= ROUND; j++ {
16-
filter := moper.D{}.Or(
17-
moper.D{}.Equal("damage", i),
18-
moper.D{}.Equal("damage", j),
16+
filter := moper.NewD().Or(
17+
*moper.NewD().Equal("damage", i),
18+
*moper.NewD().Equal("damage", j),
1919
)
2020
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
2121
t.Error("[TestOr]", err)

internal/mopertest/transaction_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func TestTransactionSuccess(t *testing.T) {
2424
Options: &options.TransactionOptions{},
2525
Func: func(ctx mongo.SessionContext) (interface{}, error) {
2626
// update damage x to y
27-
filter := moper.D{}.Equal("damage", x)
28-
update := moper.D{}.Set(moper.P{K: "damage", V: y})
27+
filter := moper.NewD().Equal("damage", x)
28+
update := moper.NewD().Set(moper.P{K: "damage", V: y})
2929

3030
_, err := mocom.UpdateMany[Hero](ctx, filter, update)
3131
if err != nil {
@@ -34,8 +34,8 @@ func TestTransactionSuccess(t *testing.T) {
3434
}
3535

3636
// update damage y to z
37-
filter2 := moper.D{}.Equal("damage", y)
38-
update2 := moper.D{}.Set(moper.P{K: "damage", V: z})
37+
filter2 := moper.NewD().Equal("damage", y)
38+
update2 := moper.NewD().Set(moper.P{K: "damage", V: z})
3939

4040
result2, err := mocom.UpdateMany[Hero](ctx, filter2, update2)
4141
if err != nil {
@@ -52,7 +52,7 @@ func TestTransactionSuccess(t *testing.T) {
5252
}
5353

5454
// get all hero has damage x or y
55-
filter := moper.D{}.InEll("damage", x, y)
55+
filter := moper.NewD().InEll("damage", x, y)
5656
count, err := mocom.Count[Hero](ctx, filter)
5757
if err != nil {
5858
t.Error(err)
@@ -61,7 +61,7 @@ func TestTransactionSuccess(t *testing.T) {
6161
}
6262

6363
// get all hero has damage z
64-
filter = moper.D{}.Equal("damage", z)
64+
filter = moper.NewD().Equal("damage", z)
6565
count, err = mocom.Count[Hero](ctx, filter)
6666
if err != nil {
6767
t.Error(err)
@@ -85,8 +85,8 @@ func TestTransactionFailed(t *testing.T) {
8585
Options: &options.TransactionOptions{},
8686
Func: func(ctx mongo.SessionContext) (interface{}, error) {
8787
// update damage x to y
88-
filter := moper.D{}.Equal("damage", x)
89-
update := moper.D{}.Set(moper.P{K: "damage", V: y})
88+
filter := moper.NewD().Equal("damage", x)
89+
update := moper.NewD().Set(moper.P{K: "damage", V: y})
9090

9191
_, err := mocom.UpdateMany[Hero](ctx, filter, update)
9292
if err != nil {
@@ -95,8 +95,8 @@ func TestTransactionFailed(t *testing.T) {
9595
}
9696

9797
// update damage y to z
98-
filter2 := moper.D{}.Equal("damage", y)
99-
update2 := moper.D{}.Set(moper.P{K: "damage", V: z})
98+
filter2 := moper.NewD().Equal("damage", y)
99+
update2 := moper.NewD().Set(moper.P{K: "damage", V: z})
100100

101101
_, err = mocom.UpdateMany[Hero](ctx, filter2, update2)
102102
if err != nil {
@@ -113,7 +113,7 @@ func TestTransactionFailed(t *testing.T) {
113113
}
114114

115115
// get all hero has damage x or y
116-
filter := moper.D{}.InEll("damage", x, y)
116+
filter := moper.NewD().InEll("damage", x, y)
117117
count, err := mocom.Count[Hero](ctx, filter)
118118
if err != nil {
119119
t.Error(err)
@@ -122,7 +122,7 @@ func TestTransactionFailed(t *testing.T) {
122122
}
123123

124124
// get all hero has damage z
125-
filter = moper.D{}.Equal("damage", z)
125+
filter = moper.NewD().Equal("damage", z)
126126
count, err = mocom.Count[Hero](ctx, filter)
127127
if err != nil {
128128
t.Error(err)

internal/mopertest/update_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ func TestSet(t *testing.T) {
1414

1515
// change all damages to negative
1616
for i := 1; i <= ROUND; i++ {
17-
filter := moper.D{}.Equal("damage", i)
18-
update := moper.D{}.Set(moper.P{K: "damage", V: -i})
17+
filter := moper.NewD().Equal("damage", i)
18+
update := moper.NewD().Set(moper.P{K: "damage", V: -i})
1919

20-
result, err := mocom.UpdateMany[Hero](ctx, filter, bson.D(update))
20+
result, err := mocom.UpdateMany[Hero](ctx, filter, bson.D(*update))
2121
if err != nil {
2222
t.Error("[TestSet]", err)
2323
return
@@ -31,8 +31,8 @@ func TestSet(t *testing.T) {
3131

3232
// change damages to positive
3333
for i := 1; i <= ROUND; i++ {
34-
filter := moper.D{}.Equal("damage", -i)
35-
update := moper.D{}.Set(moper.P{K: "damage", V: i})
34+
filter := moper.NewD().Equal("damage", -i)
35+
update := moper.NewD().Set(moper.P{K: "damage", V: i})
3636

3737
result, err := mocom.UpdateMany[Hero](ctx, filter, update)
3838
if err != nil {
@@ -50,9 +50,9 @@ func TestSet(t *testing.T) {
5050
func TestInc(t *testing.T) {
5151
ctx := context.Background()
5252
for i := ROUND; i >= 0; i-- {
53-
filter := moper.D{}.Equal("damage", i)
53+
filter := moper.NewD().Equal("damage", i)
5454

55-
update := moper.D{}.Inc(moper.P{K: "damage", V: i})
55+
update := moper.NewD().Inc(moper.P{K: "damage", V: i})
5656

5757
result, err := mocom.UpdateMany[Hero](ctx, filter, update)
5858
if err != nil {
@@ -67,8 +67,8 @@ func TestInc(t *testing.T) {
6767
}
6868

6969
for i := 1; i <= ROUND; i++ {
70-
filter := moper.D{}.Equal("damage", i*2)
71-
update := moper.D{}.Inc(moper.P{
70+
filter := moper.NewD().Equal("damage", i*2)
71+
update := moper.NewD().Inc(moper.P{
7272
K: "damage",
7373
V: -i,
7474
})
@@ -89,16 +89,16 @@ func TestInc(t *testing.T) {
8989
func TestPush(t *testing.T) {
9090
ctx := context.Background()
9191

92-
filter := moper.D{}.Equal("damage", ROUND)
93-
update := moper.D{}.Push(moper.P{K: "skillIds", V: 6})
92+
filter := moper.NewD().Equal("damage", ROUND)
93+
update := moper.NewD().Push(moper.P{K: "skillIds", V: 6})
9494

9595
result, err := mocom.UpdateMany[Hero](ctx, filter, update)
9696
if err != nil {
9797
t.Error("[TestPush]", err)
9898
return
9999
}
100100

101-
filter2 := moper.D{}.Equal("skillIds", []int{1, 2, 3, 4, 5, 6})
101+
filter2 := moper.NewD().Equal("skillIds", []int{1, 2, 3, 4, 5, 6})
102102

103103
if count, err := mocom.Count[Hero](ctx, filter2); err != nil {
104104
t.Error("[TestPush]", err)

mocom/mocom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Aggregate[T Model](ctx context.Context, req *AggregationRequest[T]) (res []
4040
// Flush clears all records of collection and return number of deleted records
4141
func Flush[T Model](ctx context.Context) (int64, error) {
4242
var t T
43-
result, err := db.Collection(t.CollName()).DeleteMany(ctx, moper.D{})
43+
result, err := db.Collection(t.CollName()).DeleteMany(ctx, moper.NewD())
4444
if err != nil {
4545
return 0, err
4646
}

mocom/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (id *ID) SetID(t interface{}) {
2424
}
2525

2626
type AggregationRequest[T Model] struct {
27-
Pipeline []moper.D
27+
Pipeline []*moper.D
2828
Options []*options.AggregateOptions
2929
}
3030

moper/aggregate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package moper
22

3-
func (d D) Match(pairs ...P) D {
3+
func (d *D) Match(pairs ...P) *D {
44
return d.Equal("$match", toPair(pairs))
55
}
66

7-
func (d D) MatchD(pair D) D {
7+
func (d *D) MatchD(pair D) *D {
88
return d.Equal("$match", pair)
99
}
1010

11-
func (d D) Group(pairs ...P) D {
11+
func (d *D) Group(pairs ...P) *D {
1212
return d.Equal("$group", toPair(pairs))
1313
}
1414

15-
func (d D) GroupD(pair D) D {
15+
func (d *D) GroupD(pair D) *D {
1616
return d.Equal("$group", pair)
1717
}
1818

19-
func (d D) Sum(fieldName string) D {
19+
func (d *D) Sum(fieldName string) *D {
2020
return d.Equal("$sum", "$"+fieldName)
2121
}

0 commit comments

Comments
 (0)