mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +00:00
Stream loader now consumes a provided interface instead of target
This commit is contained in:
@@ -30,7 +30,7 @@ func (d *Database) LoadStream(target interface{}, filters ...interface{}) (loade
|
||||
return
|
||||
}
|
||||
|
||||
loader = &StreamLoader{cur: cursor, ctx: ctx, target: target, query: composed}
|
||||
loader = &StreamLoader{cur: cursor, ctx: ctx, ref: target, query: composed}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -10,21 +10,21 @@ import (
|
||||
|
||||
// StreamLoader is a controller for a database cursor
|
||||
type StreamLoader struct {
|
||||
cur *mongox.Cursor
|
||||
query *query.Query
|
||||
ctx context.Context
|
||||
target interface{}
|
||||
cur *mongox.Cursor
|
||||
query *query.Query
|
||||
ctx context.Context
|
||||
ref interface{}
|
||||
}
|
||||
|
||||
// DecodeNext loads next documents to a target or returns an error
|
||||
func (l *StreamLoader) DecodeNext() (err error) {
|
||||
// DecodeNextMsg decodes the next document to an interface or returns an error
|
||||
func (l *StreamLoader) DecodeNextMsg(i interface{}) (err error) {
|
||||
|
||||
err = l.Next()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = l.Decode()
|
||||
err = l.DecodeMsg(i)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -32,22 +32,22 @@ func (l *StreamLoader) DecodeNext() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Decode function decodes the current cursor document into the target
|
||||
func (l *StreamLoader) Decode() (err error) {
|
||||
// DecodeMsg decodes the current cursor document into an interface
|
||||
func (l *StreamLoader) DecodeMsg(i interface{}) (err error) {
|
||||
|
||||
if created := base.Reset(l.target); created {
|
||||
err = l.query.OnDecode().Invoke(l.ctx, l.target)
|
||||
if created := base.Reset(i); created {
|
||||
err = l.query.OnDecode().Invoke(l.ctx, i)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = l.cur.Decode(l.target)
|
||||
err = l.cur.Decode(i)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = l.query.OnDecode().Invoke(l.ctx, l.target)
|
||||
err = l.query.OnDecode().Invoke(l.ctx, i)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func (l *StreamLoader) Cursor() (cursor *mongox.Cursor) {
|
||||
func (l *StreamLoader) Close() (err error) {
|
||||
|
||||
closerr := l.cur.Close(l.ctx)
|
||||
invokerr := l.query.OnClose().Invoke(l.ctx, l.target)
|
||||
invokerr := l.query.OnClose().Invoke(l.ctx, l.ref)
|
||||
|
||||
if closerr != nil {
|
||||
err = closerr
|
||||
|
||||
+2
-2
@@ -35,8 +35,8 @@ type Database interface {
|
||||
// StreamLoader is a interface to control database cursor
|
||||
type StreamLoader interface {
|
||||
Cursor() (cursor *Cursor)
|
||||
DecodeNext() (err error)
|
||||
Decode() (err error)
|
||||
DecodeNextMsg(i interface{}) (err error)
|
||||
DecodeMsg(i interface{}) (err error)
|
||||
Next() (err error)
|
||||
Close() (err error)
|
||||
Err() (err error)
|
||||
|
||||
Reference in New Issue
Block a user