github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/return.go (about)

     1  package expressions
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  )
     9  
    10  type (
    11  	ReturnExpression struct {
    12  		src       core.SourceMap
    13  		predicate core.Expression
    14  	}
    15  )
    16  
    17  func NewReturnExpression(
    18  	src core.SourceMap,
    19  	predicate core.Expression,
    20  ) (*ReturnExpression, error) {
    21  	if predicate == nil {
    22  		return nil, core.Error(core.ErrMissedArgument, "expression")
    23  	}
    24  
    25  	return &ReturnExpression{
    26  		src,
    27  		predicate,
    28  	}, nil
    29  }
    30  
    31  func (e *ReturnExpression) Predicate() core.Expression {
    32  	return e.predicate
    33  }
    34  
    35  func (e *ReturnExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
    36  	select {
    37  	case <-ctx.Done():
    38  		return values.None, core.ErrTerminated
    39  	default:
    40  		val, err := e.predicate.Exec(ctx, scope)
    41  
    42  		if err != nil {
    43  			return values.None, core.SourceError(e.src, err)
    44  		}
    45  
    46  		return val, nil
    47  	}
    48  }