Skip to content

Commit

Permalink
update pointer D
Browse files Browse the repository at this point in the history
  • Loading branch information
phuong committed Dec 13, 2022
1 parent 21615e5 commit 147c99a
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 112 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ You can walkaround to find out more commands, but here are some

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

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

matchStage := moper.D{}.MatchD(moper.D{}.InArray("damage", intArr))
groupStage := moper.D{}.Group(
matchStage := moper.NewD().MatchD(moper.NewD().InArray("damage", intArr))
groupStage := moper.NewD().Group(
moper.P{K: "_id", V: nil},
moper.P{K: "total", V: moper.D{}.Sum("damage")},
moper.P{K: "total", V: moper.NewD().Sum("damage")},
)

req := &mocom.AggregationRequest[Hero]{
Expand Down Expand Up @@ -92,8 +92,8 @@ type Model interface {
}

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

result, err := mocom.UpdateMany[Hero](ctx, filter, update)
```
Expand Down
16 changes: 8 additions & 8 deletions internal/mopertest/aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
func TestAggregation(t *testing.T) {
intArr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

matchStage := moper.D{}.MatchD(moper.D{}.InArray("damage", intArr))
groupStage := moper.D{}.Group(
matchStage := moper.NewD().MatchD(*moper.NewD().InArray("damage", intArr))
groupStage := moper.NewD().Group(
moper.P{K: "_id", V: nil},
moper.P{K: "total", V: moper.D{}.Sum("damage")},
moper.P{K: "total", V: moper.NewD().Sum("damage")},
)

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

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

lookupStage := moper.D{}.LookUp().
lookupStage := moper.NewD().LookUp().
From(Weapon{}.CollName()).
LocalField("damage").
ForeignField("damage").
As("weapon")

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

req := &mocom.AggregationRequest[Hero]{
Pipeline: []moper.D{matchStage, lookupStage.D(), unwindStage},
Pipeline: []*moper.D{matchStage, lookupStage.D(), unwindStage},
Options: []*options.AggregateOptions{},
}
result, err := mocom.Aggregate(context.Background(), req)
Expand Down
16 changes: 8 additions & 8 deletions internal/mopertest/comparision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestEquals(t *testing.T) {
ctx := context.Background()
for i := 0; i < ROUND; i++ {
filter := moper.D{}.Equal("damage", i+1)
filter := moper.NewD().Equal("damage", i+1)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

if count, err := mocom.Count[Hero](ctx, filter); err != nil {
t.Error("[TestEqualGreater]:", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/mopertest/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (

func TestExists(t *testing.T) {
ctx := context.Background()
filter := moper.D{}.Exists("omit", true)
filter := moper.NewD().Exists("omit", true)
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
t.Error("[TestExists]", err)
} else if count != int64(ROUND) {
t.Error("[TestExists]", count, "!=", int64(ROUND))
}

filter = moper.D{}.Exists("omit", false)
filter = moper.NewD().Exists("omit", false)
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
t.Error("[TestExists]", err)
} else if count != int64(ROUND*(ROUND-1)/2) {
Expand Down
6 changes: 3 additions & 3 deletions internal/mopertest/logical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func TestOr(t *testing.T) {

for i := 1; i < ROUND; i++ {
for j := i + 1; j <= ROUND; j++ {
filter := moper.D{}.Or(
moper.D{}.Equal("damage", i),
moper.D{}.Equal("damage", j),
filter := moper.NewD().Or(
*moper.NewD().Equal("damage", i),
*moper.NewD().Equal("damage", j),
)
if count, err := mocom.Count[Hero](ctx, filter); err != nil {
t.Error("[TestOr]", err)
Expand Down
24 changes: 12 additions & 12 deletions internal/mopertest/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestTransactionSuccess(t *testing.T) {
Options: &options.TransactionOptions{},
Func: func(ctx mongo.SessionContext) (interface{}, error) {
// update damage x to y
filter := moper.D{}.Equal("damage", x)
update := moper.D{}.Set(moper.P{K: "damage", V: y})
filter := moper.NewD().Equal("damage", x)
update := moper.NewD().Set(moper.P{K: "damage", V: y})

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

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

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

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

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

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

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

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

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

// get all hero has damage z
filter = moper.D{}.Equal("damage", z)
filter = moper.NewD().Equal("damage", z)
count, err = mocom.Count[Hero](ctx, filter)
if err != nil {
t.Error(err)
Expand Down
24 changes: 12 additions & 12 deletions internal/mopertest/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func TestSet(t *testing.T) {

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

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

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

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

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

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

for i := 1; i <= ROUND; i++ {
filter := moper.D{}.Equal("damage", i*2)
update := moper.D{}.Inc(moper.P{
filter := moper.NewD().Equal("damage", i*2)
update := moper.NewD().Inc(moper.P{
K: "damage",
V: -i,
})
Expand All @@ -89,16 +89,16 @@ func TestInc(t *testing.T) {
func TestPush(t *testing.T) {
ctx := context.Background()

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

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

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

if count, err := mocom.Count[Hero](ctx, filter2); err != nil {
t.Error("[TestPush]", err)
Expand Down
2 changes: 1 addition & 1 deletion mocom/mocom.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Aggregate[T Model](ctx context.Context, req *AggregationRequest[T]) (res []
// Flush clears all records of collection and return number of deleted records
func Flush[T Model](ctx context.Context) (int64, error) {
var t T
result, err := db.Collection(t.CollName()).DeleteMany(ctx, moper.D{})
result, err := db.Collection(t.CollName()).DeleteMany(ctx, moper.NewD())
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion mocom/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (id *ID) SetID(t interface{}) {
}

type AggregationRequest[T Model] struct {
Pipeline []moper.D
Pipeline []*moper.D
Options []*options.AggregateOptions
}

Expand Down
10 changes: 5 additions & 5 deletions moper/aggregate.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package moper

func (d D) Match(pairs ...P) D {
func (d *D) Match(pairs ...P) *D {
return d.Equal("$match", toPair(pairs))
}

func (d D) MatchD(pair D) D {
func (d *D) MatchD(pair D) *D {
return d.Equal("$match", pair)
}

func (d D) Group(pairs ...P) D {
func (d *D) Group(pairs ...P) *D {
return d.Equal("$group", toPair(pairs))
}

func (d D) GroupD(pair D) D {
func (d *D) GroupD(pair D) *D {
return d.Equal("$group", pair)
}

func (d D) Sum(fieldName string) D {
func (d *D) Sum(fieldName string) *D {
return d.Equal("$sum", "$"+fieldName)
}
Loading

0 comments on commit 147c99a

Please sign in to comment.