1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-23 01:24:30 +02:00
ferret/pkg/runtime/expressions/operators/in.go
3timeslazy acf2f13dcb Linter Cleanups (#294)
* sync with MontFerret/ferret

* fix --param handling

When params is converted to map it uses strings.Split,
which slices a string into all substrings separated by :.

* remove impossible conditions nil != nil

* delete ineffectual assignments

* replace '+= 1' with '++'

* remove useless comparison with nil

* merge variable declarations

* remove bool comparison

* fix imports

* fix imports

* delete unused file

* use copy instead of loop

* delete unused DummyInterface

* remove unnecassary break statements

* tidy modules
2019-05-03 17:10:34 -04:00

66 lines
1.4 KiB
Go

package operators
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
type InOperator struct {
*baseOperator
not bool
}
func NewInOperator(
src core.SourceMap,
left core.Expression,
right core.Expression,
not bool,
) (*InOperator, error) {
if left == nil {
return nil, core.Error(core.ErrMissedArgument, "left expression")
}
if right == nil {
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)
}
return operator.Eval(ctx, left, right)
}
func (operator *InOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) {
err := core.ValidateType(right, types.Array)
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), nil
}
return values.NewBoolean(found), nil
}