From d1bf4e561646c88bb5734b571ea3e2d19b3a2828 Mon Sep 17 00:00:00 2001 From: Mac Tichner Date: Mon, 14 Nov 2022 11:19:30 -0500 Subject: [PATCH 1/6] Allow comparison of Integers and Numeric types --- lib/jmespath/nodes/condition.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/jmespath/nodes/condition.rb b/lib/jmespath/nodes/condition.rb index ccefdf6..e9b78b1 100644 --- a/lib/jmespath/nodes/condition.rb +++ b/lib/jmespath/nodes/condition.rb @@ -43,9 +43,7 @@ def visit(_value) private def comparable?(left_value, right_value) - COMPARABLE_TYPES.any? do |type| - left_value.is_a?(type) && right_value.is_a?(type) - end + COMPARABLE_TYPES.include?(left_value.class) && COMPARABLE_TYPES.include?(right_value.class) end end From 0d308f02818cccb0621e6d23465ae998ab16e890 Mon Sep 17 00:00:00 2001 From: Mac Tichner Date: Mon, 14 Nov 2022 15:21:08 -0500 Subject: [PATCH 2/6] Code + Specs Added --- lib/jmespath/nodes/condition.rb | 9 ++++++--- spec/compliance/filters.json | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/jmespath/nodes/condition.rb b/lib/jmespath/nodes/condition.rb index e9b78b1..e3ed63c 100644 --- a/lib/jmespath/nodes/condition.rb +++ b/lib/jmespath/nodes/condition.rb @@ -28,8 +28,8 @@ def optimize class ComparatorCondition < Node COMPARATOR_TO_CONDITION = {} - COMPARABLE_TYPES = [Integer, String].freeze - + COMPARABLE_TYPES = [Integer, Float, String].freeze + NUMERIC_TYPES = [Integer, Float].freeze def initialize(left, right, child) @left = left @right = right @@ -43,7 +43,10 @@ def visit(_value) private def comparable?(left_value, right_value) - COMPARABLE_TYPES.include?(left_value.class) && COMPARABLE_TYPES.include?(right_value.class) + if COMPARABLE_TYPES.include?(left_value.class) && COMPARABLE_TYPES.include?(right_value.class) + return true if left_value.class == right_value.class + return true if NUMERIC_TYPES.include?(left_value.class) && NUMERIC_TYPES.include?(right_value.class) + end end end diff --git a/spec/compliance/filters.json b/spec/compliance/filters.json index 5086e36..7e237b8 100644 --- a/spec/compliance/filters.json +++ b/spec/compliance/filters.json @@ -465,6 +465,33 @@ } ] }, + { + "given": {"foo": [{"length": 170}, + {"length": 175.5}, + {"length": 185}]}, + "cases": [ + { + "comment": "Less than number", + "expression": "foo[?length < `180`]", + "result": [{"length": 170}, {"length": 175.5}] + }, + { + "comment": "Less than or equal to number", + "expression": "foo[?length <= `175.5`]", + "result": [{"length": 170}, {"length": 175.5}] + }, + { + "comment": "Greater than number", + "expression": "foo[?length > `170`]", + "result": [{"length": 175.5}, {"length": 185}] + }, + { + "comment": "Greater than or equal to number", + "expression": "foo[?length >= `175.5`]", + "result": [{"length": 175.5}, {"length": 185}] + } + ] + }, { "given": { "foo": ["2010-02-01", "2011-05-01"] From a78135f1c5fed7924abdc54f1388aaa93a2adf48 Mon Sep 17 00:00:00 2001 From: Mac Tichner Date: Mon, 14 Nov 2022 15:45:08 -0500 Subject: [PATCH 3/6] use numerics --- lib/jmespath/nodes/condition.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jmespath/nodes/condition.rb b/lib/jmespath/nodes/condition.rb index e3ed63c..420e4da 100644 --- a/lib/jmespath/nodes/condition.rb +++ b/lib/jmespath/nodes/condition.rb @@ -29,7 +29,6 @@ def optimize class ComparatorCondition < Node COMPARATOR_TO_CONDITION = {} COMPARABLE_TYPES = [Integer, Float, String].freeze - NUMERIC_TYPES = [Integer, Float].freeze def initialize(left, right, child) @left = left @right = right @@ -45,7 +44,7 @@ def visit(_value) def comparable?(left_value, right_value) if COMPARABLE_TYPES.include?(left_value.class) && COMPARABLE_TYPES.include?(right_value.class) return true if left_value.class == right_value.class - return true if NUMERIC_TYPES.include?(left_value.class) && NUMERIC_TYPES.include?(right_value.class) + return true if left_value.is_a?(Numeric) && right_value.is_a?(Numeric) end end end From fbb7ff4355a70aaa25278bfd1be656368e2b3dc9 Mon Sep 17 00:00:00 2001 From: Mac Tichner Date: Tue, 15 Nov 2022 07:11:51 -0500 Subject: [PATCH 4/6] nit --- lib/jmespath/nodes/condition.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/jmespath/nodes/condition.rb b/lib/jmespath/nodes/condition.rb index 420e4da..3b267de 100644 --- a/lib/jmespath/nodes/condition.rb +++ b/lib/jmespath/nodes/condition.rb @@ -29,6 +29,7 @@ def optimize class ComparatorCondition < Node COMPARATOR_TO_CONDITION = {} COMPARABLE_TYPES = [Integer, Float, String].freeze + def initialize(left, right, child) @left = left @right = right From 57d77eae3de699e1a2ecb873250f16b04f989f48 Mon Sep 17 00:00:00 2001 From: Matt Muller <53055821+mullermp@users.noreply.github.com> Date: Fri, 18 Nov 2022 07:38:57 -0700 Subject: [PATCH 5/6] Update lib/jmespath/nodes/condition.rb Co-authored-by: Ezra Jennings --- lib/jmespath/nodes/condition.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jmespath/nodes/condition.rb b/lib/jmespath/nodes/condition.rb index 3b267de..4f238ca 100644 --- a/lib/jmespath/nodes/condition.rb +++ b/lib/jmespath/nodes/condition.rb @@ -44,8 +44,7 @@ def visit(_value) def comparable?(left_value, right_value) if COMPARABLE_TYPES.include?(left_value.class) && COMPARABLE_TYPES.include?(right_value.class) - return true if left_value.class == right_value.class - return true if left_value.is_a?(Numeric) && right_value.is_a?(Numeric) + (left_value.class == right_value.class) || (left_value.is_a?(Numeric) && right_value.is_a?(Numeric)) end end end From c27c82d7cf9e35542f8afb822e02c3783be49456 Mon Sep 17 00:00:00 2001 From: Matt Muller Date: Mon, 21 Nov 2022 10:38:10 -0500 Subject: [PATCH 6/6] Add numeric as a comparable type --- CHANGELOG.md | 5 +++++ lib/jmespath/nodes/condition.rb | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1609a8e..6649d2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +Unreleased Changes +------------------ + +* Issue - Allow comparison of Numeric types (includes Float). + 1.6.1 (2022-03-07) ------------------ diff --git a/lib/jmespath/nodes/condition.rb b/lib/jmespath/nodes/condition.rb index 4f238ca..9dd8e19 100644 --- a/lib/jmespath/nodes/condition.rb +++ b/lib/jmespath/nodes/condition.rb @@ -28,7 +28,7 @@ def optimize class ComparatorCondition < Node COMPARATOR_TO_CONDITION = {} - COMPARABLE_TYPES = [Integer, Float, String].freeze + COMPARABLE_TYPES = [Numeric, String].freeze def initialize(left, right, child) @left = left @@ -43,8 +43,8 @@ def visit(_value) private def comparable?(left_value, right_value) - if COMPARABLE_TYPES.include?(left_value.class) && COMPARABLE_TYPES.include?(right_value.class) - (left_value.class == right_value.class) || (left_value.is_a?(Numeric) && right_value.is_a?(Numeric)) + COMPARABLE_TYPES.any? do |type| + left_value.is_a?(type) && right_value.is_a?(type) end end end