2018-09-23 03:06:19 +02:00
|
|
|
package operators
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-13 19:31:18 +02:00
|
|
|
|
2018-09-23 03:06:19 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-09-23 03:06:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type InOperator struct {
|
|
|
|
*baseOperator
|
|
|
|
not bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInOperator(
|
|
|
|
src core.SourceMap,
|
|
|
|
left core.Expression,
|
|
|
|
right core.Expression,
|
|
|
|
not bool,
|
|
|
|
) (*InOperator, error) {
|
2018-10-07 23:54:02 +02:00
|
|
|
if left == nil {
|
2018-09-23 03:06:19 +02:00
|
|
|
return nil, core.Error(core.ErrMissedArgument, "left expression")
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
if right == nil {
|
2018-09-23 03:06:19 +02:00
|
|
|
return nil, core.Error(core.ErrMissedArgument, "right expression")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &InOperator{&baseOperator{src, left, right}, not}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (operator *InOperator) 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)
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
return operator.Eval(ctx, left, right)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (operator *InOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
|
2019-02-13 19:31:18 +02:00
|
|
|
err := core.ValidateType(right, types.Array)
|
2018-09-23 03:06:19 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Return the error? AQL just returns false
|
|
|
|
return values.False, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := right.(*values.Array)
|
|
|
|
found := arr.IndexOf(left) > -1
|
|
|
|
|
|
|
|
if operator.not {
|
|
|
|
return values.NewBoolean(found == false), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewBoolean(found == true), nil
|
|
|
|
}
|