1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
# frozen_string_literal: true
module JMESPath
# @api private
module Nodes
class Condition < Node
def initialize(test, child)
@test = test
@child = child
end
def visit(value)
if JMESPath::Util.falsey?(@test.visit(value))
nil
else
@child.visit(value)
end
end
def optimize
test = @test.optimize
if (new_type = ComparatorCondition::COMPARATOR_TO_CONDITION[@test.class])
new_type.new(test.left, test.right, @child).optimize
else
self.class.new(test, @child.optimize)
end
end
end
class ComparatorCondition < Node
COMPARATOR_TO_CONDITION = {}
COMPARABLE_TYPES = [Numeric, String].freeze
def initialize(left, right, child)
@left = left
@right = right
@child = child
end
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
COMPARATOR_TO_CONDITION[Comparators::Eq] = self
def visit(value)
Util.as_json(@left.visit(value)) == Util.as_json(@right.visit(value)) ? @child.visit(value) : nil
end
def optimize
if @right.is_a?(Literal)
LiteralRightEqCondition.new(@left, @right, @child)
else
self
end
end
end
class LiteralRightEqCondition < EqCondition
def initialize(left, right, child)
super
@right = @right.value
end
def visit(value)
Util.as_json(@left.visit(value)) == @right ? @child.visit(value) : nil
end
end
class NeqCondition < ComparatorCondition
COMPARATOR_TO_CONDITION[Comparators::Neq] = self
def visit(value)
Util.as_json(@left.visit(value)) != Util.as_json(@right.visit(value)) ? @child.visit(value) : nil
end
def optimize
if @right.is_a?(Literal)
LiteralRightNeqCondition.new(@left, @right, @child)
else
self
end
end
end
class LiteralRightNeqCondition < NeqCondition
def initialize(left, right, child)
super
@right = @right.value
end
def visit(value)
Util.as_json(@left.visit(value)) != @right ? @child.visit(value) : nil
end
end
class GtCondition < ComparatorCondition
COMPARATOR_TO_CONDITION[Comparators::Gt] = self
def visit(value)
left_value = @left.visit(value)
right_value = @right.visit(value)
comparable?(left_value, right_value) && left_value > right_value ? @child.visit(value) : nil
end
end
class GteCondition < ComparatorCondition
COMPARATOR_TO_CONDITION[Comparators::Gte] = self
def visit(value)
left_value = @left.visit(value)
right_value = @right.visit(value)
comparable?(left_value, right_value) && left_value >= right_value ? @child.visit(value) : nil
end
end
class LtCondition < ComparatorCondition
COMPARATOR_TO_CONDITION[Comparators::Lt] = self
def visit(value)
left_value = @left.visit(value)
right_value = @right.visit(value)
comparable?(left_value, right_value) && left_value < right_value ? @child.visit(value) : nil
end
end
class LteCondition < ComparatorCondition
COMPARATOR_TO_CONDITION[Comparators::Lte] = self
def visit(value)
left_value = @left.visit(value)
right_value = @right.visit(value)
comparable?(left_value, right_value) && left_value <= right_value ? @child.visit(value) : nil
end
end
end
end
|