mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-06-12 16:53:35 +00:00
Make callback type
This commit is contained in:
@@ -1,19 +0,0 @@
|
|||||||
package database
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
|
||||||
)
|
|
||||||
|
|
||||||
func onDecode(ctx context.Context, iter interface{}, callbacks ...query.OnDecode) (err error) {
|
|
||||||
|
|
||||||
for _, cb := range callbacks {
|
|
||||||
err = cb(ctx, iter)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -69,7 +69,7 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = onDecode(d.ctx, elem, composed.OnDecode()...)
|
err = composed.OnDecode().Invoke(d.Context(), elem)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = result.Close(d.Context())
|
_ = result.Close(d.Context())
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err erro
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = onDecode(d.ctx, target, composed.OnDecode()...)
|
err = composed.OnDecode().Invoke(d.Context(), target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func (l *StreamLoader) Decode() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = onDecode(l.ctx, l.target, l.query.OnDecode()...)
|
err = l.query.OnDecode().Invoke(l.ctx, l.target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,20 @@ import (
|
|||||||
"context"
|
"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
|
||||||
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ func applyCallbacks(q *Query, f interface{}) (ok bool) {
|
|||||||
|
|
||||||
switch cb := f.(type) {
|
switch cb := f.(type) {
|
||||||
case OnDecode:
|
case OnDecode:
|
||||||
q.ondecode = append(q.ondecode, cb)
|
q.ondecode = append(q.ondecode, Callback(cb))
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ type Query struct {
|
|||||||
sorter Sorter
|
sorter Sorter
|
||||||
skipper Skipper
|
skipper Skipper
|
||||||
preloader Preloader
|
preloader Preloader
|
||||||
ondecode []OnDecode
|
ondecode Callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -77,7 +77,7 @@ func (q *Query) Preloader() (ok bool, preloads []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OnDecode callback is called after the mongo decode function
|
// OnDecode callback is called after the mongo decode function
|
||||||
func (q *Query) OnDecode() (callbacks []OnDecode) {
|
func (q *Query) OnDecode() (callbacks Callbacks) {
|
||||||
return q.ondecode
|
return q.ondecode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user