Make callback type

This commit is contained in:
Nikita Tokarchuk
2020-07-22 04:45:32 +02:00
parent 08c3c5b377
commit b796d5ac3b
7 changed files with 23 additions and 26 deletions
+17 -1
View File
@@ -4,4 +4,20 @@ import (
"context"
)
type OnDecode func(ctx context.Context, iter interface{}) (err error)
type Callback func(ctx context.Context, iter interface{}) (err error)
type Callbacks []Callback
type OnDecode Callback
// Invoke callbacks sequence
func (c Callbacks) Invoke(ctx context.Context, iter interface{}) (err error) {
for _, cb := range c {
err = cb(ctx, iter)
if err != nil {
return
}
}
return
}
+1 -1
View File
@@ -129,7 +129,7 @@ func applyCallbacks(q *Query, f interface{}) (ok bool) {
switch cb := f.(type) {
case OnDecode:
q.ondecode = append(q.ondecode, cb)
q.ondecode = append(q.ondecode, Callback(cb))
ok = true
}
+2 -2
View File
@@ -11,7 +11,7 @@ type Query struct {
sorter Sorter
skipper Skipper
preloader Preloader
ondecode []OnDecode
ondecode Callbacks
}
// And function pushes the elem query to the $and array of the query
@@ -77,7 +77,7 @@ func (q *Query) Preloader() (ok bool, preloads []string) {
}
// OnDecode callback is called after the mongo decode function
func (q *Query) OnDecode() (callbacks []OnDecode) {
func (q *Query) OnDecode() (callbacks Callbacks) {
return q.ondecode
}