data-cursor.go: the inizialization of the current item is done in the Next() method.
This allows the application of the filter and map operator to the first item too.
This commit is contained in:
parent
dfae593e86
commit
315f5b22d3
@ -5,13 +5,13 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
type dataCursor struct {
|
||||
ds map[string]Functor
|
||||
ctx ExprContext
|
||||
initState bool // true if no item has prodiced yet (this replace di initial Next() call in the contructor)
|
||||
index int
|
||||
count int
|
||||
current any
|
||||
@ -24,18 +24,19 @@ type dataCursor struct {
|
||||
|
||||
func NewDataCursor(ctx ExprContext, ds map[string]Functor, resource any) (dc *dataCursor) {
|
||||
dc = &dataCursor{
|
||||
ds: ds,
|
||||
index: -1,
|
||||
count: 0,
|
||||
current: nil,
|
||||
lastErr: nil,
|
||||
resource: resource,
|
||||
ctx: ctx.Clone(),
|
||||
nextFunc: ds[NextName],
|
||||
ds: ds,
|
||||
initState: true,
|
||||
index: -1,
|
||||
count: 0,
|
||||
current: nil,
|
||||
lastErr: nil,
|
||||
resource: resource,
|
||||
ctx: ctx.Clone(),
|
||||
nextFunc: ds[NextName],
|
||||
cleanFunc: ds[CleanName],
|
||||
resetFunc: ds[ResetName],
|
||||
}
|
||||
dc.Next()
|
||||
//dc.Next()
|
||||
return
|
||||
}
|
||||
|
||||
@ -107,12 +108,14 @@ func (dc *dataCursor) Reset() (success bool, err error) {
|
||||
if dc.resetFunc != nil {
|
||||
if dc.resource != nil {
|
||||
ctx := cloneContext(dc.ctx)
|
||||
if _, err = dc.resetFunc.Invoke(ctx, ResetName, []any{dc.resource}); err == nil {
|
||||
dc.index = -1
|
||||
dc.count = 0
|
||||
}
|
||||
_, err = dc.resetFunc.Invoke(ctx, ResetName, []any{dc.resource})
|
||||
exportObjects(dc.ctx, ctx)
|
||||
dc.Next()
|
||||
dc.index = -1
|
||||
dc.count = 0
|
||||
dc.initState = true
|
||||
dc.current = nil
|
||||
dc.lastErr = nil
|
||||
//dc.Next()
|
||||
} else {
|
||||
err = errInvalidDataSource()
|
||||
}
|
||||
@ -128,19 +131,21 @@ func (dc *dataCursor) Clean() (success bool, err error) {
|
||||
if dc.resource != nil {
|
||||
ctx := cloneContext(dc.ctx)
|
||||
_, err = dc.cleanFunc.Invoke(ctx, CleanName, []any{dc.resource})
|
||||
dc.resource = nil
|
||||
// dc.resource = nil
|
||||
exportObjects(dc.ctx, ctx)
|
||||
} else {
|
||||
err = errInvalidDataSource()
|
||||
}
|
||||
} else {
|
||||
err = errors.New("no 'clean' function defined in the data-source")
|
||||
err = errNoOperation(CleanName)
|
||||
}
|
||||
success = err == nil
|
||||
return
|
||||
}
|
||||
|
||||
func (dc *dataCursor) Current() (item any, err error) { // must return io.EOF at the last item
|
||||
dc.init()
|
||||
|
||||
if dc.current != nil {
|
||||
item = dc.current
|
||||
} else {
|
||||
@ -167,8 +172,17 @@ func (dc *dataCursor) mapItem(mapper Functor, item any) (mappedItem any, err err
|
||||
return
|
||||
}
|
||||
|
||||
func (dc *dataCursor) init() {
|
||||
if dc.initState {
|
||||
dc.initState = false
|
||||
dc.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func (dc *dataCursor) Next() (current any, err error) { // must return io.EOF after the last item
|
||||
if err = dc.lastErr; err != nil {
|
||||
if dc.initState {
|
||||
dc.init()
|
||||
} else if err = dc.lastErr; err != nil {
|
||||
return
|
||||
}
|
||||
current = dc.current
|
||||
@ -214,7 +228,6 @@ func (dc *dataCursor) Index() int {
|
||||
return dc.index - 1
|
||||
}
|
||||
|
||||
|
||||
func (dc *dataCursor) Count() int {
|
||||
return dc.count
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user