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

     1  package operators
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  )
     8  
     9  type (
    10  	EqualityOperator struct {
    11  		*baseOperator
    12  		fn OperatorFunc
    13  	}
    14  )
    15  
    16  var equalityOperators = map[string]OperatorFunc{
    17  	"==": Equal,
    18  	"!=": NotEqual,
    19  	">":  Greater,
    20  	"<":  Less,
    21  	">=": GreaterOrEqual,
    22  	"<=": LessOrEqual,
    23  }
    24  
    25  func NewEqualityOperator(
    26  	src core.SourceMap,
    27  	left core.Expression,
    28  	right core.Expression,
    29  	operator string,
    30  ) (*EqualityOperator, error) {
    31  	fn, exists := equalityOperators[operator]
    32  
    33  	if !exists {
    34  		return nil, core.Error(core.ErrInvalidArgument, "operator")
    35  	}
    36  
    37  	return &EqualityOperator{
    38  		&baseOperator{src, left, right},
    39  		fn,
    40  	}, nil
    41  }
    42  
    43  func (operator *EqualityOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
    44  	left, err := operator.left.Exec(ctx, scope)
    45  
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	right, err := operator.right.Exec(ctx, scope)
    51  
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return operator.Eval(ctx, left, right)
    57  }
    58  
    59  func (operator *EqualityOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
    60  	return operator.fn(left, right), nil
    61  }