github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/block.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 BlockExpression struct { 12 values collections.Iterable 13 statements []core.Expression 14 } 15 16 func NewBlockExpression(values collections.Iterable) (*BlockExpression, error) { 17 if values == nil { 18 return nil, core.Error(core.ErrMissedArgument, "values") 19 } 20 21 return &BlockExpression{ 22 values: values, 23 statements: make([]core.Expression, 0, 5), 24 }, nil 25 } 26 27 func (exp *BlockExpression) Add(stmt core.Expression) { 28 exp.statements = append(exp.statements, stmt) 29 } 30 31 func (exp *BlockExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) { 32 select { 33 case <-ctx.Done(): 34 return values.None, core.ErrTerminated 35 default: 36 for _, stmt := range exp.statements { 37 _, err := stmt.Exec(ctx, scope) 38 39 if err != nil { 40 return values.None, err 41 } 42 } 43 44 return values.None, nil 45 } 46 } 47 48 func (exp *BlockExpression) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) { 49 select { 50 case <-ctx.Done(): 51 return nil, core.ErrTerminated 52 default: 53 iter, err := exp.values.Iterate(ctx, scope) 54 55 if err != nil { 56 return nil, err 57 } 58 59 return collections.NewTapIterator(iter, exp) 60 } 61 }