Stream loader now consumes a provided interface instead of target

v2
Nikita Tokarchuk 3 years ago
parent 18d9f9bed3
commit 8cc57cc82f
No known key found for this signature in database
GPG Key ID: DBFD964471BAE65C
  1. 2
      mongox/database/loadstream.go
  2. 28
      mongox/database/streamloader.go
  3. 4
      mongox/mongox.go

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

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

Loading…
Cancel
Save