Add preloader filter

Fix pipeline structure


Fix preloader logic
This commit is contained in:
Nikita Tokarchuk
2019-05-16 13:55:36 +02:00
parent e60fef13df
commit b7e92a5055
6 changed files with 207 additions and 20 deletions
+11
View File
@@ -30,6 +30,7 @@ func Push(q *Query, f interface{}) bool {
ok = ok || applySort(q, f)
ok = ok || applySkip(q, f)
ok = ok || applyProtection(q, f)
ok = ok || applyPreloader(q, f)
return ok
}
@@ -108,3 +109,13 @@ func applyProtection(q *Query, f interface{}) bool {
return true
}
func applyPreloader(q *Query, f interface{}) bool {
if f, ok := f.(Preloader); ok {
q.preloader = f
return true
}
return false
}
+17
View File
@@ -0,0 +1,17 @@
package query
// Preloader is a filter to skip the result
type Preloader interface {
Preload() []string
}
// Preload is a simple implementation of the Skipper filter
type Preload []string
var _ Preloader = Preload{}
// Preload returns a preload list
func (l Preload) Preload() []string {
return Preload(l)
}
+21 -4
View File
@@ -8,10 +8,11 @@ import (
// Query is an enchanched bson.M map
type Query struct {
m bson.M
limiter Limiter
sorter Sorter
skipper Skipper
m bson.M
limiter Limiter
sorter Sorter
skipper Skipper
preloader Preloader
}
// And function pushes the elem query to the $and array of the query
@@ -63,6 +64,22 @@ func (q *Query) Skipper() *int64 {
return q.skipper.Skip()
}
// Preloader is a preloader list for a query
func (q *Query) Preloader() (empty bool, preloader []string) {
if q.preloader == nil {
return false, nil
}
preloader = q.preloader.Preload()
if len(preloader) == 0 {
return false, nil
}
return true, preloader
}
// Empty checks the query for any content
func (q *Query) Empty() bool {