From 0cbda62d4514063feb1e003c4afb1c65b4db50ff Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Fri, 23 Aug 2024 15:27:28 -0400 Subject: [PATCH] More granular compare_key and determine path at initialization time Invalid operators no longer slip through the cracks at parse time --- lib/floe/workflow/choice_rule/data.rb | 25 +++++++++++++++++++++---- spec/workflow/choice_rule_spec.rb | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/floe/workflow/choice_rule/data.rb b/lib/floe/workflow/choice_rule/data.rb index fff623c7..90596c6d 100644 --- a/lib/floe/workflow/choice_rule/data.rb +++ b/lib/floe/workflow/choice_rule/data.rb @@ -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 @@ -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 diff --git a/spec/workflow/choice_rule_spec.rb b/spec/workflow/choice_rule_spec.rb index 2129e9de..edcbf037 100644 --- a/spec/workflow/choice_rule_spec.rb +++ b/spec/workflow/choice_rule_spec.rb @@ -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