Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix single element "in" clauses #185

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions evaluationStage.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,16 @@ func makeAccessorStage(pair []string) evaluationOperator {
}
}

func ensureSliceStage(op evaluationOperator) evaluationOperator {
return func(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
orig, err := op(left, right, parameters)
if err != nil {
return orig, err
}
return []interface{}{orig}, nil
}
}

func separatorStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {

var ret []interface{}
Expand Down
6 changes: 6 additions & 0 deletions evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ func TestNoParameterEvaluation(test *testing.T) {
Input: "!(1 in (1, 2, 3))",
Expected: false,
},
EvaluationTest{

Name: "Single Element Array membership literal",
Input: "1 in (1)",
Expected: true,
},
EvaluationTest{

Name: "Logical operator reordering (#30)",
Expand Down
10 changes: 10 additions & 0 deletions stagePlanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,22 @@ func planValue(stream *tokenStream) (*evaluationStage, error) {
switch token.Kind {

case CLAUSE:
var prev ExpressionToken
if stream.index > 1 {
prev = stream.tokens[stream.index-2]
}

ret, err = planTokens(stream)
if err != nil {
return nil, err
}

// clauses with single elements don't trigger SEPARATE stage planner
// this ensures that when used as part of an "in" comparison, the array requirement passes
if prev.Kind == COMPARATOR && prev.Value == "in" && ret.symbol == LITERAL {
ret.operator = ensureSliceStage(ret.operator)
}

// advance past the CLAUSE_CLOSE token. We know that it's a CLAUSE_CLOSE, because at parse-time we check for unbalanced parens.
stream.next()

Expand Down