Skip to content

Commit

Permalink
bug joshbuddy#145: when evaluating compound expressions, convert sub-…
Browse files Browse the repository at this point in the history
…expression result strings "true"/"false" into booleans before evaluation
  • Loading branch information
gongfarmer committed Apr 23, 2022
1 parent 083d858 commit add40df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/jsonpath/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ def parse_parentheses(str)
res = bool_or_exp(top.shift)
top.each_with_index do |item, index|
if item == '&&'
res &&= top[index + 1]
next_value = bool_or_exp(top[index + 1])
res &&= next_value
elsif item == '||'
res ||= top[index + 1]
next_value = bool_or_exp(top[index + 1])
res ||= next_value
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,17 @@ def test_recognize_filters

def test_or_operator
assert_equal [@object['store']['book'][1], @object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] == 13 || @['price'] == 23)]").on(@object)
result = ["Sayings of the Century", "Sword of Honour", "Moby Dick", "The Lord of the Rings"]
assert_equal result, JsonPath.new("$..book[?(@.price==13 || @.price==9 || @.price==23)].title").on(@object)
assert_equal result, JsonPath.new("$..book[?(@.price==9 || @.price==23 || @.price==13)].title").on(@object)
assert_equal result, JsonPath.new("$..book[?(@.price==23 || @.price==13 || @.price==9)].title").on(@object)
end

def test_and_operator
assert_equal [], JsonPath.new("$..book[?(@['price'] == 13 && @['price'] == 23)]").on(@object)
assert_equal [], JsonPath.new("$..book[?(@.price==13 && @.category==fiction && @.title==no_match)]").on(@object)
assert_equal [], JsonPath.new("$..book[?(@.title==no_match && @.category==fiction && @.price==13)]").on(@object)
assert_equal [], JsonPath.new("$..book[?(@.price==13 && @.title==no_match && @.category==fiction)]").on(@object)
end

def test_and_operator_with_more_results
Expand Down

0 comments on commit add40df

Please sign in to comment.