diff --git a/iterator.go b/iterator.go index 9b0bc60..14f857d 100644 --- a/iterator.go +++ b/iterator.go @@ -33,6 +33,8 @@ type Iterator interface { type ExtIterator interface { Iterator + Reset() error + Clean() error HasOperation(name string) bool CallOperation(name string, args map[string]any) (value any, err error) } diff --git a/list-iterator.go b/list-iterator.go index 4d5f823..4165bd3 100644 --- a/list-iterator.go +++ b/list-iterator.go @@ -99,7 +99,9 @@ func (it *ListIterator) CallOperation(name string, args map[string]any) (v any, case NextName: v, err = it.Next() case ResetName: - v, err = it.Reset() + err = it.Reset() + case CleanName: + err = it.Clean() case IndexName: v = int64(it.Index()) case CurrentName: @@ -147,8 +149,12 @@ func (it *ListIterator) Count() int { return it.count } -func (it *ListIterator) Reset() (bool, error) { +func (it *ListIterator) Reset() (error) { it.index = it.start - it.step it.count = 0 - return true, nil + return nil +} + +func (it *ListIterator) Clean() (error) { + return nil } diff --git a/operand-iterator.go b/operand-iterator.go index 47d6991..0128dbe 100644 --- a/operand-iterator.go +++ b/operand-iterator.go @@ -92,6 +92,7 @@ func evalIterator(ctx ExprContext, opTerm *term) (v any, err error) { if ds != nil { var dc *dataCursor + dcCtx := ctx.Clone() if initFunc, exists := ds[InitName]; exists && initFunc != nil { var args []any var resource any @@ -109,9 +110,10 @@ func evalIterator(ctx ExprContext, opTerm *term) (v any, err error) { if resource, err = initFunc.InvokeNamed(initCtx, InitName, actualParams); err != nil { return } - dcCtx := ctx.Clone() exportObjects(dcCtx, initCtx) dc = NewDataCursor(dcCtx, ds, resource) + } else { + dc = NewDataCursor(dcCtx, ds, nil) } v = dc diff --git a/t_iterator_test.go b/t_iterator_test.go index bfd6f51..d2787a9 100644 --- a/t_iterator_test.go +++ b/t_iterator_test.go @@ -17,7 +17,7 @@ func TestIteratorParser(t *testing.T) { /* 6 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); mul(it)`, int64(0), nil}, /* 7 */ {`builtin "math.arith"; include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); mul(it)`, int64(12000), nil}, /* 8 */ {`include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); it++; it.index`, int64(0), nil}, - /* 9 */ {`include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); it.clean`, true, nil}, + /* 9 */ {`include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); it.clean`, nil, nil}, /* 10 */ {`it=$(1,2,3); it++`, int64(1), nil}, /* 11 */ {`it=$(1,2,3); it++; it.reset; it++`, int64(1), nil}, /* 12 */ {`it=$([1,2,3,4],1); it++`, int64(2), nil}, @@ -25,8 +25,10 @@ func TestIteratorParser(t *testing.T) { /* 14 */ {`it=$([1,2,3,4],1,3,2); it++; it++;`, int64(4), nil}, /* 15 */ {`it=$([1,2,3,4],1,2,2); it++; it++;`, nil, `EOF`}, /* 16 */ {`include "test-resources/filter.expr"; it=$(ds,10); it++`, int64(2), nil}, + /* 17 */ {`it=$({"next":func(){5}}); it++`, int64(5), nil}, + /* 18 */ {`it=$({"next":func(){5}}); it.clean`, nil, nil}, } - // runTestSuiteSpec(t, section, inputs, 11) + //runTestSuiteSpec(t, section, inputs, 18) runTestSuite(t, section, inputs) }