Make callback type

v2
Nikita Tokarchuk 4 years ago
parent 08c3c5b377
commit b796d5ac3b
  1. 19
      mongox/database/callbacks.go
  2. 2
      mongox/database/loadarray.go
  3. 2
      mongox/database/loadone.go
  4. 2
      mongox/database/streamloader.go
  5. 18
      mongox/query/callbacks.go
  6. 2
      mongox/query/compose.go
  7. 4
      mongox/query/query.go

@ -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
}
err = onDecode(d.ctx, elem, composed.OnDecode()...)
err = composed.OnDecode().Invoke(d.Context(), elem)
if err != nil {
_ = result.Close(d.Context())
return

@ -40,7 +40,7 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err erro
return
}
err = onDecode(d.ctx, target, composed.OnDecode()...)
err = composed.OnDecode().Invoke(d.Context(), target)
if err != nil {
return
}

@ -42,7 +42,7 @@ func (l *StreamLoader) Decode() (err error) {
return
}
err = onDecode(l.ctx, l.target, l.query.OnDecode()...)
err = l.query.OnDecode().Invoke(l.ctx, l.target)
if err != nil {
return
}

@ -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
}

@ -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
}

@ -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
}

Loading…
Cancel
Save