1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-08-13 19:52:52 +02:00

Feature/#1 array comparison operators (#71)

* #1 Added ALL IN

* #1 Completed Array operator

* #1 Fixed linting issues
This commit is contained in:
Tim Voronov
2018-10-07 17:54:02 -04:00
committed by GitHub
parent 809a51b217
commit 0dfd58dc89
18 changed files with 1735 additions and 706 deletions

View File

@@ -0,0 +1,193 @@
package operators
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type (
ArrayOperatorType int
ArrayOperator struct {
*baseOperator
aotype ArrayOperatorType
comparator core.OperatorExpression
}
)
const (
ArrayOperatorTypeAll ArrayOperatorType = 0
ArrayOperatorTypeAny ArrayOperatorType = 1
ArrayOperatorTypeNone ArrayOperatorType = 2
)
func IsValidArrayOperatorType(aotype ArrayOperatorType) bool {
switch aotype {
case ArrayOperatorTypeAll, ArrayOperatorTypeAny, ArrayOperatorTypeNone:
return true
default:
return false
}
}
func ToIsValidArrayOperatorType(stype string) (ArrayOperatorType, error) {
switch stype {
case "ALL":
return ArrayOperatorTypeAll, nil
case "ANY":
return ArrayOperatorTypeAny, nil
case "NONE":
return ArrayOperatorTypeNone, nil
default:
return ArrayOperatorType(-1), core.Error(core.ErrInvalidArgument, stype)
}
}
func NewArrayOperator(
src core.SourceMap,
left core.Expression,
right core.Expression,
aotype ArrayOperatorType,
comparator core.OperatorExpression,
) (*ArrayOperator, error) {
if left == nil {
return nil, core.Error(core.ErrMissedArgument, "left expression")
}
if right == nil {
return nil, core.Error(core.ErrMissedArgument, "right expression")
}
if IsValidArrayOperatorType(aotype) == false {
return nil, core.Error(core.ErrInvalidArgument, "operator type")
}
if comparator == nil {
return nil, core.Error(core.ErrMissedArgument, "comparator expression")
}
base := &baseOperator{src, left, right}
return &ArrayOperator{base, aotype, comparator}, nil
}
func (operator *ArrayOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
left, err := operator.left.Exec(ctx, scope)
if err != nil {
return values.False, core.SourceError(operator.src, err)
}
right, err := operator.right.Exec(ctx, scope)
if err != nil {
return values.False, core.SourceError(operator.src, err)
}
return operator.Eval(ctx, left, right)
}
func (operator *ArrayOperator) Eval(ctx context.Context, left, right core.Value) (core.Value, error) {
err := core.ValidateType(left, core.ArrayType)
if err != nil {
// TODO: Return the error? AQL just returns false
return values.False, nil
}
arr := left.(*values.Array)
switch operator.aotype {
case ArrayOperatorTypeAll:
return operator.all(ctx, arr, right)
case ArrayOperatorTypeAny:
return operator.any(ctx, arr, right)
default:
return operator.none(ctx, arr, right)
}
}
func (operator *ArrayOperator) all(ctx context.Context, arr *values.Array, value core.Value) (core.Value, error) {
result := values.False
var err error
arr.ForEach(func(el core.Value, _ int) bool {
out, e := operator.comparator.Eval(ctx, el, value)
if e != nil {
err = e
return false
}
if out == values.True {
result = values.True
} else {
result = values.False
return false
}
return true
})
if err != nil {
return values.False, err
}
return result, nil
}
func (operator *ArrayOperator) any(ctx context.Context, arr *values.Array, value core.Value) (core.Value, error) {
result := values.False
var err error
arr.ForEach(func(el core.Value, _ int) bool {
out, e := operator.comparator.Eval(ctx, el, value)
if e != nil {
err = e
return false
}
if out == values.True {
result = values.True
return false
}
return true
})
if err != nil {
return values.False, err
}
return result, nil
}
func (operator *ArrayOperator) none(ctx context.Context, arr *values.Array, value core.Value) (core.Value, error) {
result := values.False
var err error
arr.ForEach(func(el core.Value, _ int) bool {
out, e := operator.comparator.Eval(ctx, el, value)
if e != nil {
err = e
return false
}
if out == values.False {
result = values.True
} else {
result = values.False
return false
}
return true
})
if err != nil {
return values.False, err
}
return result, nil
}

View File

@@ -8,11 +8,11 @@ import (
type (
EqualityOperator struct {
*baseOperator
fn Operator
fn OperatorFunc
}
)
var equalityOperators = map[string]Operator{
var equalityOperators = map[string]OperatorFunc{
"==": Equal,
"!=": NotEqual,
">": Greater,
@@ -30,7 +30,7 @@ func NewEqualityOperator(
fn, exists := equalityOperators[operator]
if !exists {
return nil, core.Error(core.ErrInvalidArgument, "operator")
return nil, core.Error(core.ErrInvalidArgument, "aotype")
}
return &EqualityOperator{
@@ -52,5 +52,9 @@ func (operator *EqualityOperator) Exec(ctx context.Context, scope *core.Scope) (
return nil, err
}
return operator.Eval(ctx, left, right)
}
func (operator *EqualityOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
return operator.fn(left, right), nil
}

View File

@@ -17,11 +17,11 @@ func NewInOperator(
right core.Expression,
not bool,
) (*InOperator, error) {
if core.IsNil(left) {
if left == nil {
return nil, core.Error(core.ErrMissedArgument, "left expression")
}
if core.IsNil(right) {
if right == nil {
return nil, core.Error(core.ErrMissedArgument, "right expression")
}
@@ -41,7 +41,11 @@ func (operator *InOperator) Exec(ctx context.Context, scope *core.Scope) (core.V
return values.False, core.SourceError(operator.src, err)
}
err = core.ValidateType(right, core.ArrayType)
return operator.Eval(ctx, left, right)
}
func (operator *InOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
err := core.ValidateType(right, core.ArrayType)
if err != nil {
// TODO: Return the error? AQL just returns false

View File

@@ -15,17 +15,17 @@ type (
)
const (
AndType LogicalOperatorType = 0
OrType LogicalOperatorType = 1
NotType LogicalOperatorType = 2
LogicalOperatorTypeAnd LogicalOperatorType = 0
LogicalOperatorTypeOr LogicalOperatorType = 1
LogicalOperatorTypeNot LogicalOperatorType = 2
)
var logicalOperators = map[string]LogicalOperatorType{
"&&": AndType,
"AND": AndType,
"||": OrType,
"OR": OrType,
"NOT": NotType,
"&&": LogicalOperatorTypeAnd,
"AND": LogicalOperatorTypeAnd,
"||": LogicalOperatorTypeOr,
"OR": LogicalOperatorTypeOr,
"NOT": LogicalOperatorTypeNot,
}
func NewLogicalOperator(
@@ -51,7 +51,7 @@ func NewLogicalOperator(
}
func (operator *LogicalOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
if operator.value == NotType {
if operator.value == LogicalOperatorTypeNot {
val, err := operator.right.Exec(ctx, scope)
if err != nil {
@@ -69,7 +69,7 @@ func (operator *LogicalOperator) Exec(ctx context.Context, scope *core.Scope) (c
leftBool := values.ToBoolean(left)
if operator.value == AndType && leftBool == values.False {
if operator.value == LogicalOperatorTypeAnd && leftBool == values.False {
if left.Type() == core.BooleanType {
return values.False, nil
}
@@ -77,7 +77,7 @@ func (operator *LogicalOperator) Exec(ctx context.Context, scope *core.Scope) (c
return left, nil
}
if operator.value == OrType && leftBool == values.True {
if operator.value == LogicalOperatorTypeOr && leftBool == values.True {
return left, nil
}
@@ -89,3 +89,25 @@ func (operator *LogicalOperator) Exec(ctx context.Context, scope *core.Scope) (c
return right, nil
}
func (operator *LogicalOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
if operator.value == LogicalOperatorTypeNot {
return Not(right, values.None), nil
}
leftBool := values.ToBoolean(left)
if operator.value == LogicalOperatorTypeAnd && leftBool == values.False {
if left.Type() == core.BooleanType {
return values.False, nil
}
return left, nil
}
if operator.value == LogicalOperatorTypeOr && leftBool == values.True {
return left, nil
}
return right, nil
}

View File

@@ -6,32 +6,45 @@ import (
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type MathOperator struct {
*baseOperator
fn Operator
leftOnly bool
}
type (
MathOperatorType string
MathOperator struct {
*baseOperator
fn OperatorFunc
leftOnly bool
}
)
var mathOperators = map[string]Operator{
"+": Add,
"-": Subtract,
"*": Multiply,
"/": Divide,
"%": Modulus,
"++": Increment,
"--": Decrement,
const (
MathOperatorTypeAdd MathOperatorType = "+"
MathOperatorTypeSubtract MathOperatorType = "-"
MathOperatorTypeMultiply MathOperatorType = "*"
MathOperatorTypeDivide MathOperatorType = "/"
MathOperatorTypeModulus MathOperatorType = "%"
MathOperatorTypeIncrement MathOperatorType = "++"
MathOperatorTypeDecrement MathOperatorType = "--"
)
var mathOperators = map[MathOperatorType]OperatorFunc{
MathOperatorTypeAdd: Add,
MathOperatorTypeSubtract: Subtract,
MathOperatorTypeMultiply: Multiply,
MathOperatorTypeDivide: Divide,
MathOperatorTypeModulus: Modulus,
MathOperatorTypeIncrement: Increment,
MathOperatorTypeDecrement: Decrement,
}
func NewMathOperator(
src core.SourceMap,
left core.Expression,
right core.Expression,
operator string,
operator MathOperatorType,
) (*MathOperator, error) {
fn, exists := mathOperators[operator]
if !exists {
return nil, core.Error(core.ErrInvalidArgument, "operator")
return nil, core.Error(core.ErrInvalidArgument, "operator type")
}
var leftOnly bool
@@ -55,7 +68,7 @@ func (operator *MathOperator) Exec(ctx context.Context, scope *core.Scope) (core
}
if operator.leftOnly {
return operator.fn(left, values.None), nil
return operator.Eval(ctx, left, values.None)
}
right, err := operator.right.Exec(ctx, scope)
@@ -64,5 +77,13 @@ func (operator *MathOperator) Exec(ctx context.Context, scope *core.Scope) (core
return nil, err
}
return operator.Eval(ctx, left, right)
}
func (operator *MathOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
if operator.leftOnly {
return operator.fn(left, values.None), nil
}
return operator.fn(left, right), nil
}

View File

@@ -6,18 +6,23 @@ import (
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type Operator func(left, right core.Value) core.Value
type baseOperator struct {
src core.SourceMap
left core.Expression
right core.Expression
}
type (
OperatorFunc func(left, right core.Value) core.Value
baseOperator struct {
src core.SourceMap
left core.Expression
right core.Expression
}
)
func (operator *baseOperator) Exec(_ context.Context, _ *core.Scope) (core.Value, error) {
return values.None, core.ErrInvalidOperation
}
func (operator *baseOperator) Eval(_ context.Context, _, _ core.Value) (core.Value, error) {
return values.None, core.ErrInvalidOperation
}
// Equality
func Equal(left, right core.Value) core.Value {
if left.Compare(right) == 0 {

View File

@@ -44,13 +44,17 @@ func (operator *RangeOperator) Exec(ctx context.Context, scope *core.Scope) (cor
return values.None, core.SourceError(operator.src, err)
}
err = core.ValidateType(left, core.IntType)
right, err := operator.right.Exec(ctx, scope)
if err != nil {
return values.None, core.SourceError(operator.src, err)
}
right, err := operator.right.Exec(ctx, scope)
return operator.Eval(ctx, left, right)
}
func (operator *RangeOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
err := core.ValidateType(left, core.IntType)
if err != nil {
return values.None, core.SourceError(operator.src, err)