Implement skipper interface

v2
Nikita Tokarchuk 6 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.Sort = composed.Sorter()
opts.Limit = composed.Limiter() opts.Limit = composed.Limiter()
opts.Skip = composed.Skipper()
result, err := collection.Find(db.Context(), composed.M(), opts) result, err := collection.Find(db.Context(), composed.M(), opts)
if err != nil { if err != nil {

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

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

@ -11,6 +11,7 @@ type Query struct {
m bson.M m bson.M
limiter Limiter limiter Limiter
sorter Sorter sorter Sorter
skipper Skipper
} }
// And function pushes the elem query to the $and array of the query // 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() 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 // Empty checks the query for any content
func (q *Query) Empty() bool { 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