Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bsonx/bsonx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package bsonx
import (
"bytes"

"github.com/chenmingyong0423/go-mongox/v2/internal/pkg/utils"

"go.mongodb.org/mongo-driver/v2/bson"
)

Expand Down Expand Up @@ -89,3 +91,19 @@ func dToM(d bson.D) bson.M {
}
return m
}

// StringSortToBsonD transform string sort to bson D
// "-created_at" => bson.D{{"created_at", -1}}
// []string{"age", "-created_at"} => bson.D{{"age", 1}, {"created_at", -1}}
func StringSortToBsonD(sorts ...string) bson.D {
var res bson.D
for _, sort := range sorts {
key, n := utils.SplitSortField(sort)
if key == "" {
continue
}
res = append(res, bson.E{Key: key, Value: n})
}

return res
}
27 changes: 27 additions & 0 deletions bsonx/bsonx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,30 @@
}

}

func TestStringSortToBsonD(t *testing.T) {
testCases := []struct {
name string
value any
want bson.D
}{
{
name: "one sort",
value: []string{"-created_at"},
want: bson.D{{"created_at", -1}},

Check failure on line 75 in bsonx/bsonx_test.go

View workflow job for this annotation

GitHub Actions / lint

composites: go.mongodb.org/mongo-driver/v2/bson.E struct literal uses unkeyed fields (govet)
},
{
name: "two sort",
value: []string{"age", "-created_at"},
want: bson.D{{"age", 1}, {"created_at", -1}},

Check failure on line 80 in bsonx/bsonx_test.go

View workflow job for this annotation

GitHub Actions / lint

composites: go.mongodb.org/mongo-driver/v2/bson.E struct literal uses unkeyed fields (govet)
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
w, _ := bson.Marshal(tc.want)
v, _ := bson.Marshal(StringSortToBsonD(tc.value.([]string)...))
assert.Equal(t, string(w), string(v))
})
}
}
36 changes: 35 additions & 1 deletion finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package finder

import (
"context"

"github.com/chenmingyong0423/go-mongox/v2/callback"
"github.com/chenmingyong0423/go-mongox/v2/operation"

Expand Down Expand Up @@ -45,6 +44,9 @@ type Finder[T any] struct {
modelHook any
beforeHooks []beforeHookFn
afterHooks []afterHookFn[T]

skip, limit int64
sort any
}

func (f *Finder[T]) RegisterBeforeHooks(hooks ...beforeHookFn) *Finder[T] {
Expand All @@ -66,6 +68,21 @@ func (f *Finder[T]) Filter(filter any) *Finder[T] {
return f
}

func (f *Finder[T]) Limit(limit int64) *Finder[T] {
f.limit = limit
return f
}

func (f *Finder[T]) Skip(skip int64) *Finder[T] {
f.skip = skip
return f
}

func (f *Finder[T]) Sort(sort any) *Finder[T] {
f.sort = sort
return f
}

func (f *Finder[T]) Updates(update any) *Finder[T] {
f.updates = update
return f
Expand Down Expand Up @@ -109,6 +126,13 @@ func (f *Finder[T]) postActionHandler(ctx context.Context, globalOpContext *oper
}

func (f *Finder[T]) FindOne(ctx context.Context, opts ...options.Lister[options.FindOneOptions]) (*T, error) {
if f.skip > 0 {
opts = append(opts, options.FindOne().SetSkip(f.skip))
}
if f.sort != nil {
opts = append(opts, options.FindOne().SetSort(f.sort))
}

t := new(T)

globalOpContext := operation.NewOpContext(f.collection, operation.WithDoc(t), operation.WithFilter(f.filter), operation.WithMongoOptions(opts), operation.WithModelHook(f.modelHook))
Expand All @@ -131,6 +155,16 @@ func (f *Finder[T]) FindOne(ctx context.Context, opts ...options.Lister[options.
}

func (f *Finder[T]) Find(ctx context.Context, opts ...options.Lister[options.FindOptions]) ([]*T, error) {
if f.skip > 0 {
opts = append(opts, options.Find().SetSkip(f.skip))
}
if f.limit > 0 {
opts = append(opts, options.Find().SetLimit(f.limit))
}
if f.sort != nil {
opts = append(opts, options.Find().SetSort(f.sort))
}

t := make([]*T, 0)

opContext := operation.NewOpContext(f.collection, operation.WithFilter(f.filter), operation.WithMongoOptions(opts), operation.WithModelHook(f.modelHook))
Expand Down
Loading
Loading