1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/expressions/operators/equality.go
2018-09-18 16:42:38 -04:00

57 lines
971 B
Go

package operators
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type (
EqualityOperator struct {
*baseOperator
fn Operator
}
)
var equalityOperators = map[string]Operator{
"==": Equal,
"!=": NotEqual,
">": Greater,
"<": Less,
">=": GreaterOrEqual,
"<=": LessOrEqual,
}
func NewEqualityOperator(
src core.SourceMap,
left core.Expression,
right core.Expression,
operator string,
) (*EqualityOperator, error) {
fn, exists := equalityOperators[operator]
if !exists {
return nil, core.Error(core.ErrInvalidArgument, "operator")
}
return &EqualityOperator{
&baseOperator{src, left, right},
fn,
}, nil
}
func (operator *EqualityOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
left, err := operator.left.Exec(ctx, scope)
if err != nil {
return nil, err
}
right, err := operator.right.Exec(ctx, scope)
if err != nil {
return nil, err
}
return operator.fn(left, right), nil
}