Skip to content

Commit e8cf212

Browse files
committed
move valid? into preprocess_exp
Think this was the only reference to MiqExpressoin#valid?
1 parent 361e823 commit e8cf212

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

lib/miq_expression.rb

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class MiqExpression
2+
class InvalidExpression < StandardError ; end
3+
24
# bit array of the types of nodes available/desired
35
MODE_NONE = 0
46
MODE_RUBY = 1
@@ -35,28 +37,11 @@ def initialize(exp, ctype = nil)
3537
@ruby = nil
3638
end
3739

38-
def valid?(component = exp)
39-
operator = component.keys.first
40-
case operator.downcase
41-
when "and", "or"
42-
component[operator].all?(&method(:valid?))
43-
when "not", "!"
44-
valid?(component[operator])
45-
when "find"
46-
validate_set = Set.new(%w[checkall checkany checkcount search])
47-
validate_keys = component[operator].keys.select { |k| validate_set.include?(k) }
48-
validate_keys.all? { |k| valid?(component[operator][k]) }
49-
else
50-
if component[operator].key?("field")
51-
field = Field.parse(component[operator]["field"])
52-
return false if field && !field.valid?
53-
end
54-
if Field.is_field?(component[operator]["value"])
55-
field = Field.parse(component[operator]["value"])
56-
return false unless field && field.valid?
57-
end
58-
true
59-
end
40+
def valid?
41+
preprocess_exp(exp)
42+
true
43+
rescue InvalidExpression
44+
false
6045
end
6146

6247
def set_tagged_target(model, associations = [])
@@ -180,14 +165,14 @@ def to_ruby(timezone = nil, prune_sql: false)
180165
nil
181166
elsif @ruby
182167
@ruby.dup
183-
elsif valid?
168+
else
184169
pexp = preprocess_exp(exp)
185170
pexp, _ = prune_exp(pexp, MODE_RUBY) if prune_sql
186171
@ruby = self.class._to_ruby(pexp, context_type, timezone) || true
187172
@ruby == true ? nil : @ruby.dup
188-
else
189-
""
190173
end
174+
rescue InvalidExpression
175+
""
191176
end
192177

193178
def self._to_ruby(exp, context_type, tz)
@@ -329,6 +314,8 @@ def to_sql(tz = nil)
329314
sql = to_arel(pexp, tz).to_sql if pexp.present?
330315
incl = includes_for_sql if sql.present?
331316
[sql, incl, attrs]
317+
rescue InvalidExpression
318+
[nil, nil, {:supported_by_sql => false}]
332319
end
333320

334321
def preprocess_exp(exp)
@@ -350,15 +337,18 @@ def preprocess_exp(exp)
350337
# op => {"regkey"=>"foo", "regval"=>"bar", "value"=>"baz"}
351338
# op => {"field" => "foo", "value" => "baz"}
352339
# op => {"count" => "Vm.snapshots", "value"=>"1"}
340+
# op => {"field" => "<count>", "value"=>"1"}
353341
# op => {"tag"=>"Host.managed-environment", "value"=>"prod"}
354342
operator_values = operator_values.dup
355343
field = operator_values["field"]
356-
if field
344+
if field && field != "<count>"
357345
field_field = operator_values["field-field"] = Field.parse(field)
346+
raise(InvalidExpression, field) unless field_field.valid?
358347
end
359348
value = operator_values["value"]
360349
if value
361350
value_field = operator_values["value-field"] = Field.parse(value)
351+
raise(InvalidExpression, field) if value_field && !value_field.valid?
362352
end
363353

364354
# attempt to do conversion only if db type of column is integer and value to compare to is String

0 commit comments

Comments
 (0)