Add callback mechanism and implement on-decode callback

This commit is contained in:
Nikita Tokarchuk
2020-07-13 04:08:19 +02:00
parent 09fa64ab0e
commit 08c3c5b377
8 changed files with 80 additions and 6 deletions
+7
View File
@@ -0,0 +1,7 @@
package query
import (
"context"
)
type OnDecode func(ctx context.Context, iter interface{}) (err error)
+12
View File
@@ -38,6 +38,7 @@ func Push(q *Query, f interface{}) (ok bool) {
ok = ok || applySkip(q, f)
ok = ok || applyProtection(q, f)
ok = ok || applyPreloader(q, f)
ok = ok || applyCallbacks(q, f)
return ok
}
@@ -123,3 +124,14 @@ func applyPreloader(q *Query, f interface{}) (ok bool) {
return false
}
func applyCallbacks(q *Query, f interface{}) (ok bool) {
switch cb := f.(type) {
case OnDecode:
q.ondecode = append(q.ondecode, cb)
ok = true
}
return
}
+6
View File
@@ -11,6 +11,7 @@ type Query struct {
sorter Sorter
skipper Skipper
preloader Preloader
ondecode []OnDecode
}
// And function pushes the elem query to the $and array of the query
@@ -75,6 +76,11 @@ func (q *Query) Preloader() (ok bool, preloads []string) {
return
}
// OnDecode callback is called after the mongo decode function
func (q *Query) OnDecode() (callbacks []OnDecode) {
return q.ondecode
}
// Empty checks the query for any content
func (q *Query) Empty() (isEmpty bool) {
return len(q.m) == 0