Implement skipper interface

v2
Nikita Tokarchuk 5 years ago
parent 5a3ee358b5
commit 704ce76c11
  1. 1
      mongox/common/loadarray.go
  2. 1
      mongox/common/loadmany.go
  3. 2
      mongox/query/compose.go
  4. 11
      mongox/query/query.go
  5. 22
      mongox/query/skip.go

@ -38,6 +38,7 @@ func LoadArray(db *mongox.Database, target interface{}, filters ...interface{})
opts.Sort = composed.Sorter()
opts.Limit = composed.Limiter()
opts.Skip = composed.Skipper()
result, err := collection.Find(db.Context(), composed.M(), opts)
if err != nil {

@ -49,6 +49,7 @@ func LoadMany(db *mongox.Database, target interface{}, filters ...interface{}) (
opts.Sort = composed.Sorter()
opts.Limit = composed.Limiter()
opts.Skip = composed.Skipper()
cursor, err := collection.Find(db.Context(), composed.M(), opts)
if err != nil {

@ -45,6 +45,8 @@ func applyLimits(q *Query, f interface{}) bool {
q.limiter = f
case Sorter:
q.sorter = f
case Skipper:
q.skipper = f
default:
return false
}

@ -11,6 +11,7 @@ type Query struct {
m bson.M
limiter Limiter
sorter Sorter
skipper Skipper
}
// And function pushes the elem query to the $and array of the query
@ -52,6 +53,16 @@ func (q *Query) Sorter() interface{} {
return q.sorter.Sort()
}
// Skipper is a skipper for a query
func (q *Query) Skipper() *int64 {
if q.skipper == nil {
return nil
}
return q.skipper.Skip()
}
// Empty checks the query for any content
func (q *Query) Empty() bool {

@ -0,0 +1,22 @@
package query
// Skipper is a filter to skip the result
type Skipper interface {
Skip() *int64
}
// Skip is a simple implementation of the Skipper filter
type Skip int64
var _ Skipper = Skip(0)
// Skip returns a skip number
func (l Skip) Skip() *int64 {
lim := int64(l)
if lim <= 0 {
return nil
}
return &lim
}
Loading…
Cancel
Save