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 return
} }
loader = &StreamLoader{cur: cursor, ctx: ctx, target: target, query: composed} loader = &StreamLoader{cur: cursor, ctx: ctx, ref: target, query: composed}
return return
} }

@ -10,21 +10,21 @@ import (
// 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 query *query.Query
ctx context.Context ctx context.Context
target interface{} ref interface{}
} }
// DecodeNext loads next documents to a target or returns an error // DecodeNextMsg decodes the next document to an interface or returns an error
func (l *StreamLoader) DecodeNext() (err error) { func (l *StreamLoader) DecodeNextMsg(i interface{}) (err error) {
err = l.Next() err = l.Next()
if err != nil { if err != nil {
return return
} }
err = l.Decode() err = l.DecodeMsg(i)
if err != nil { if err != nil {
return return
} }
@ -32,22 +32,22 @@ func (l *StreamLoader) DecodeNext() (err error) {
return return
} }
// Decode function decodes the current cursor document into the target // DecodeMsg decodes the current cursor document into an interface
func (l *StreamLoader) Decode() (err error) { func (l *StreamLoader) DecodeMsg(i interface{}) (err error) {
if created := base.Reset(l.target); created { if created := base.Reset(i); created {
err = l.query.OnDecode().Invoke(l.ctx, l.target) err = l.query.OnDecode().Invoke(l.ctx, i)
} }
if err != nil { if err != nil {
return return
} }
err = l.cur.Decode(l.target) err = l.cur.Decode(i)
if err != nil { if err != nil {
return return
} }
err = l.query.OnDecode().Invoke(l.ctx, l.target) err = l.query.OnDecode().Invoke(l.ctx, i)
if err != nil { if err != nil {
return return
} }
@ -79,7 +79,7 @@ func (l *StreamLoader) Cursor() (cursor *mongox.Cursor) {
func (l *StreamLoader) Close() (err error) { func (l *StreamLoader) Close() (err error) {
closerr := l.cur.Close(l.ctx) 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 { if closerr != nil {
err = closerr err = closerr

@ -35,8 +35,8 @@ type Database interface {
// StreamLoader is a interface to control database cursor // StreamLoader is a interface to control database cursor
type StreamLoader interface { type StreamLoader interface {
Cursor() (cursor *Cursor) Cursor() (cursor *Cursor)
DecodeNext() (err error) DecodeNextMsg(i interface{}) (err error)
Decode() (err error) DecodeMsg(i interface{}) (err error)
Next() (err error) Next() (err error)
Close() (err error) Close() (err error)
Err() (err error) Err() (err error)

Loading…
Cancel
Save