github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/for_while.go (about) 1 package expressions 2 3 import ( 4 "context" 5 6 "github.com/MontFerret/ferret/pkg/runtime/collections" 7 "github.com/MontFerret/ferret/pkg/runtime/core" 8 "github.com/MontFerret/ferret/pkg/runtime/values" 9 ) 10 11 type ForWhileIterableExpression struct { 12 src core.SourceMap 13 mode collections.WhileMode 14 condition core.Expression 15 valVariable string 16 } 17 18 func NewForWhileIterableExpression( 19 src core.SourceMap, 20 mode collections.WhileMode, 21 valVariable string, 22 condition core.Expression, 23 ) (collections.Iterable, error) { 24 if condition == nil { 25 return nil, core.Error(core.ErrMissedArgument, "condition") 26 } 27 28 return &ForWhileIterableExpression{ 29 src: src, 30 mode: mode, 31 valVariable: valVariable, 32 condition: condition, 33 }, nil 34 } 35 36 func (iterable *ForWhileIterableExpression) Iterate(_ context.Context, _ *core.Scope) (collections.Iterator, error) { 37 return collections.NewWhileIterator(iterable.mode, iterable.valVariable, func(ctx context.Context, scope *core.Scope) (bool, error) { 38 res, err := iterable.condition.Exec(ctx, scope) 39 40 if err != nil { 41 return false, err 42 } 43 44 return res == values.True, nil 45 }) 46 }