Skip to content

Commit

Permalink
Merge pull request ManageIQ#274 from kbrock/choice_rule_payload_valid…
Browse files Browse the repository at this point in the history
…ation_granular

More granular compare_key and determine path at initialization time
  • Loading branch information
agrare committed Aug 27, 2024
2 parents b740fc9 + 0cbda62 commit 415d9e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
25 changes: 21 additions & 4 deletions lib/floe/workflow/choice_rule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ module Floe
class Workflow
class ChoiceRule
class Data < Floe::Workflow::ChoiceRule
COMPARE_KEYS = %w[IsNull IsPresent IsNumeric IsString IsBoolean IsTimestamp String Numeric Boolean Timestamp].freeze
TYPES = ["String", "Numeric", "Boolean", "Timestamp", "Present", "Null"].freeze
COMPARES = ["Equals", "LessThan", "GreaterThan", "LessThanEquals", "GreaterThanEquals", "Matches"].freeze
# e.g.: (Is)(String), (Is)(Present)
TYPE_CHECK = /^(Is)(#{TYPES.join("|")})$/.freeze
# e.g.: (String)(LessThan)(Path), (Numeric)(GreaterThanEquals)()
OPERATION = /^(#{(TYPES - %w[Null Present]).join("|")})(#{COMPARES.join("|")})(Path)?$/.freeze

attr_reader :variable, :compare_key, :compare_predicate, :path

Expand Down Expand Up @@ -108,11 +113,23 @@ def is_timestamp?(value, predicate = true)
# rubocop:enable Naming/PredicateName
# rubocop:enable Style/OptionalBooleanParameter

# parse the compare key at initialization time
def parse_compare_key
@compare_key = payload.keys.detect { |key| key.match?(/^(#{COMPARE_KEYS.join("|")})/) }
payload.each_key do |key|
# e.g. (String)(GreaterThan)(Path)
if (match_values = OPERATION.match(key))
@compare_key = key
_type, _operator, @path = match_values.captures
break
end
# e.g. (Is)(String)
if TYPE_CHECK.match?(key)
@compare_key = key
@path = nil
break
end
end
parser_error!("requires a compare key") unless compare_key

@path = compare_key.end_with?("Path")
end

# parse predicate at initilization time
Expand Down
2 changes: 1 addition & 1 deletion spec/workflow/choice_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
end

context "with an invalid compare key" do
let(:choices) { [{"Variable" => "$.foo", "InvalidCompare" => "$.bar", "Next" => "FirstMatchState"}] }
let(:choices) { [{"Variable" => "$.foo", "StringBeGone" => "$.bar", "Next" => "FirstMatchState"}] }
let(:input) { {"foo" => 0, "bar" => 1} }

it "fails" do
Expand Down

0 comments on commit 415d9e0

Please sign in to comment.