Skip to content

Commit cc7bbaa

Browse files
Merge pull request #11 from danielgtaylor/where-left-slice
fix: ensure left side in where clause is a slice
2 parents 4e47385 + 34c23b5 commit cc7bbaa

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

interpreter.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,19 @@ func (i *interpreter) run(ast *Node, value any) (any, Error) {
454454
}
455455
resultLeft = values
456456
}
457-
for _, item := range resultLeft.([]any) {
458-
// In an unquoted string scenario it makes no sense for the first/only
459-
// token after a `where` clause to be treated as a string. Instead we
460-
// treat a `where` the same as a field select `.` in this scenario.
461-
i.prevFieldSelect = true
462-
resultRight, _ := i.run(ast.Right, item)
463-
if i.strict && err != nil {
464-
return nil, err
465-
}
466-
if toBool(resultRight) {
467-
results = append(results, item)
457+
if leftSlice, ok := resultLeft.([]any); ok {
458+
for _, item := range leftSlice {
459+
// In an unquoted string scenario it makes no sense for the first/only
460+
// token after a `where` clause to be treated as a string. Instead we
461+
// treat a `where` the same as a field select `.` in this scenario.
462+
i.prevFieldSelect = true
463+
resultRight, err := i.run(ast.Right, item)
464+
if i.strict && err != nil {
465+
return nil, err
466+
}
467+
if toBool(resultRight) {
468+
results = append(results, item)
469+
}
468470
}
469471
}
470472
return results, nil

interpreter_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func TestInterpreter(t *testing.T) {
145145
{expr: `foo where method == "GET"`, input: `{"foo": {"op1": {"method": "GET", "path": "/op1"}, "op2": {"method": "PUT", "path": "/op2"}, "op3": {"method": "DELETE", "path": "/op3"}}}`, output: []any{map[string]any{"method": "GET", "path": "/op1"}}},
146146
{expr: `foo where method == "GET"`, inputParsed: map[any]any{"foo": map[any]any{"op1": map[any]any{"method": "GET", "path": "/op1"}, "op2": map[any]any{"method": "PUT", "path": "/op2"}, "op3": map[any]any{"method": "DELETE", "path": "/op3"}}}, output: []any{map[any]any{"method": "GET", "path": "/op1"}}},
147147
{expr: `items where id > 3`, input: `{"items": []}`, err: "where clause requires a non-empty array or object"},
148+
{expr: `items where id > 3`, input: `{"items": 1}`, skipTC: true, output: []any{}},
148149
// Order of operations
149150
{expr: "1 + 2 + 3", output: 6.0},
150151
{expr: "1 + 2 * 3", output: 7.0},

0 commit comments

Comments
 (0)