Skip to content

Commit

Permalink
Allow string comparison (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnallen3d authored Feb 14, 2022
1 parent 9655aa9 commit 4b8773a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
16 changes: 12 additions & 4 deletions lib/jmespath/nodes/comparator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module JMESPath
# @api private
module Nodes
class Comparator < Node
COMPARABLE_TYPES = [Numeric, String].freeze

attr_reader :left, :right

def initialize(left, right)
Expand Down Expand Up @@ -36,6 +38,12 @@ def optimize
def check(left_value, right_value)
nil
end

def comparable?(left_value, right_value)
COMPARABLE_TYPES.any? do |type|
left_value.is_a?(type) && right_value.is_a?(type)
end
end
end

module Comparators
Expand All @@ -54,7 +62,7 @@ def check(left_value, right_value)

class Gt < Comparator
def check(left_value, right_value)
if left_value.is_a?(Numeric) && right_value.is_a?(Numeric)
if comparable?(left_value, right_value)
left_value > right_value
else
nil
Expand All @@ -64,7 +72,7 @@ def check(left_value, right_value)

class Gte < Comparator
def check(left_value, right_value)
if left_value.is_a?(Numeric) && right_value.is_a?(Numeric)
if comparable?(left_value, right_value)
left_value >= right_value
else
nil
Expand All @@ -74,7 +82,7 @@ def check(left_value, right_value)

class Lt < Comparator
def check(left_value, right_value)
if left_value.is_a?(Numeric) && right_value.is_a?(Numeric)
if comparable?(left_value, right_value)
left_value < right_value
else
nil
Expand All @@ -84,7 +92,7 @@ def check(left_value, right_value)

class Lte < Comparator
def check(left_value, right_value)
if left_value.is_a?(Numeric) && right_value.is_a?(Numeric)
if comparable?(left_value, right_value)
left_value <= right_value
else
nil
Expand Down
17 changes: 13 additions & 4 deletions lib/jmespath/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def optimize

class ComparatorCondition < Node
COMPARATOR_TO_CONDITION = {}
COMPARABLE_TYPES = [Integer, String].freeze

def initialize(left, right, child)
@left = left
Expand All @@ -37,6 +38,14 @@ def initialize(left, right, child)
def visit(value)
nil
end

private

def comparable?(left_value, right_value)
COMPARABLE_TYPES.any? do |type|
left_value.is_a?(type) && right_value.is_a?(type)
end
end
end

class EqCondition < ComparatorCondition
Expand Down Expand Up @@ -99,7 +108,7 @@ class GtCondition < ComparatorCondition
def visit(value)
left_value = @left.visit(value)
right_value = @right.visit(value)
left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value > right_value ? @child.visit(value) : nil
comparable?(left_value, right_value) && left_value > right_value ? @child.visit(value) : nil
end
end

Expand All @@ -109,7 +118,7 @@ class GteCondition < ComparatorCondition
def visit(value)
left_value = @left.visit(value)
right_value = @right.visit(value)
left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value >= right_value ? @child.visit(value) : nil
comparable?(left_value, right_value) && left_value >= right_value ? @child.visit(value) : nil
end
end

Expand All @@ -119,7 +128,7 @@ class LtCondition < ComparatorCondition
def visit(value)
left_value = @left.visit(value)
right_value = @right.visit(value)
left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value < right_value ? @child.visit(value) : nil
comparable?(left_value, right_value) && left_value < right_value ? @child.visit(value) : nil
end
end

Expand All @@ -129,7 +138,7 @@ class LteCondition < ComparatorCondition
def visit(value)
left_value = @left.visit(value)
right_value = @right.visit(value)
left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value <= right_value ? @child.visit(value) : nil
comparable?(left_value, right_value) && left_value <= right_value ? @child.visit(value) : nil
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/compliance/boolean.json
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@
{
"expression": "two < one || three < one",
"result": false
},
{
"expression": "'2010-02-01' > '2011-05-01'",
"result": false
},
{
"expression": "'2010-02-01' <= '2011-05-01'",
"result": true
}
]
}
Expand Down
24 changes: 24 additions & 0 deletions spec/compliance/filters.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,5 +464,29 @@
"result": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}
]
},
{
"given": {
"foo": ["2010-02-01", "2011-05-01"]
},
"cases": [
{
"comment": "Greater than with ISO 8601 date string",
"expression": "foo[?@ > '2010-06-01']",
"result": ["2011-05-01"]
}
]
},
{
"given": {
"foo": [{"date": "2010-02-01"}, {"date": "2011-05-01"}]
},
"cases": [
{
"comment": "Greater than with ISO 8601 date string",
"expression": "foo[?date > '2010-06-01'].date",
"result": ["2011-05-01"]
}
]
}
]

0 comments on commit 4b8773a

Please sign in to comment.