mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +00:00
Add callback mechanism and implement on-decode callback
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type OnDecode func(ctx context.Context, iter interface{}) (err error)
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user