Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
20 changes: 20 additions & 0 deletions bsonx/bsonx.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,23 @@ 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 {
if len(sort) == 0 || sort == "-" {
continue
}

if sort[0] == '-' {
res = append(res, bson.E{Key: sort[1:], Value: -1})
} else {
res = append(res, bson.E{Key: sort, Value: 1})
}
}

return res
}
37 changes: 37 additions & 0 deletions bsonx/bsonx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,40 @@ func TestA(t *testing.T) {
}

}

func TestStringSortToBsonD(t *testing.T) {
testCases := []struct {
name string
value any
want bson.D
}{
{
name: "empty string",
value: []string{""},
want: nil,
},
{
name: "only minus sign",
value: []string{"-"},
want: nil,
},
{
name: "one sort",
value: []string{"-created_at"},
want: bson.D{{Key: "created_at", Value: -1}},
},
{
name: "two sort",
value: []string{"age", "-created_at"},
want: bson.D{{Key: "age", Value: 1}, {Key: "created_at", Value: -1}},
},
}

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))
})
}
}
21 changes: 21 additions & 0 deletions finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 +69,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 +127,7 @@ 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) {
opts = append(opts, options.FindOne().SetSkip(f.skip).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 +150,8 @@ 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) {
opts = append(opts, options.Find().SetSkip(f.skip).SetLimit(f.limit).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