mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-07-03 17:22:33 +00:00
Add callback mechanism and implement on-decode callback
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -49,14 +49,18 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i = 0; result.Next(d.Context()); {
|
for i = 0; result.Next(d.Context()); {
|
||||||
|
|
||||||
|
var elem interface{}
|
||||||
|
|
||||||
if targetSliceV.Len() == i {
|
if targetSliceV.Len() == i {
|
||||||
elem := reflect.New(targetSliceElemT.Elem())
|
value := reflect.New(targetSliceElemT.Elem())
|
||||||
err = result.Decode(elem.Interface())
|
err = result.Decode(value.Interface())
|
||||||
|
elem = value.Interface()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
targetSliceV = reflect.Append(targetSliceV, elem)
|
targetSliceV = reflect.Append(targetSliceV, value)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
elem := targetSliceV.Index(i).Interface()
|
elem = targetSliceV.Index(i).Interface()
|
||||||
base.Reset(elem)
|
base.Reset(elem)
|
||||||
err = result.Decode(elem)
|
err = result.Decode(elem)
|
||||||
}
|
}
|
||||||
@@ -65,6 +69,12 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = onDecode(d.ctx, elem, composed.OnDecode()...)
|
||||||
|
if err != nil {
|
||||||
|
_ = result.Close(d.Context())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,5 +35,15 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err erro
|
|||||||
|
|
||||||
base.Reset(target)
|
base.Reset(target)
|
||||||
|
|
||||||
return result.Decode(target)
|
err = result.Decode(target)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = onDecode(d.ctx, target, composed.OnDecode()...)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func (d *Database) LoadStream(target interface{}, filters ...interface{}) (loade
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
loader = &StreamLoader{cur: cursor, ctx: d.Context(), target: target}
|
loader = &StreamLoader{cur: cursor, ctx: d.Context(), target: target, query: composed}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import (
|
|||||||
|
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
"github.com/mainnika/mongox-go-driver/v2/mongox"
|
||||||
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
"github.com/mainnika/mongox-go-driver/v2/mongox/base"
|
||||||
|
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StreamLoader is a controller for a database cursor
|
// StreamLoader is a controller for a database cursor
|
||||||
type StreamLoader struct {
|
type StreamLoader struct {
|
||||||
cur *mongox.Cursor
|
cur *mongox.Cursor
|
||||||
|
query *query.Query
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
target interface{}
|
target interface{}
|
||||||
}
|
}
|
||||||
@@ -36,6 +38,14 @@ func (l *StreamLoader) Decode() (err error) {
|
|||||||
base.Reset(l.target)
|
base.Reset(l.target)
|
||||||
|
|
||||||
err = l.cur.Decode(l.target)
|
err = l.cur.Decode(l.target)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = onDecode(l.ctx, l.target, l.query.OnDecode()...)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 || applySkip(q, f)
|
||||||
ok = ok || applyProtection(q, f)
|
ok = ok || applyProtection(q, f)
|
||||||
ok = ok || applyPreloader(q, f)
|
ok = ok || applyPreloader(q, f)
|
||||||
|
ok = ok || applyCallbacks(q, f)
|
||||||
|
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
@@ -123,3 +124,14 @@ func applyPreloader(q *Query, f interface{}) (ok bool) {
|
|||||||
|
|
||||||
return false
|
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
|
sorter Sorter
|
||||||
skipper Skipper
|
skipper Skipper
|
||||||
preloader Preloader
|
preloader Preloader
|
||||||
|
ondecode []OnDecode
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -75,6 +76,11 @@ func (q *Query) Preloader() (ok bool, preloads []string) {
|
|||||||
return
|
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
|
// Empty checks the query for any content
|
||||||
func (q *Query) Empty() (isEmpty bool) {
|
func (q *Query) Empty() (isEmpty bool) {
|
||||||
return len(q.m) == 0
|
return len(q.m) == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user