Skip to content

Commit

Permalink
Parse value/path in ChoiceRule::Date#initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Aug 8, 2024
1 parent ba8b278 commit e7350f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/floe/workflow/choice_rule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class ChoiceRule
class Data < Floe::Workflow::ChoiceRule
COMPARE_KEYS = %w[IsNull IsPresent IsNumeric IsString IsBoolean IsTimestamp String Numeric Boolean Timestamp].freeze

attr_reader :variable, :compare_key
attr_reader :variable, :compare_key, :value, :path

def initialize(_workflow, _name, payload)
super

@variable = parse_path("Variable", payload)
@compare_key = payload.keys.detect { |key| key.match?(/^(#{COMPARE_KEYS.join("|")})/) }
parser_error!("requires a compare key") unless compare_key
parse_compare_key
@value = path ? parse_path(compare_key, payload) : payload[compare_key]
end

def true?(context, input)
Expand Down Expand Up @@ -109,7 +109,14 @@ def variable_value(context, input)
end

def compare_value(context, input)
compare_key.end_with?("Path") ? Path.value(payload[compare_key], context, input) : payload[compare_key]
path ? value.value(context, input) : value
end

def parse_compare_key
@compare_key = payload.keys.detect { |key| key.match?(/^(#{COMPARE_KEYS.join("|")})/) }
parser_error!("requires a compare key") unless compare_key

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

def parse_path(field_name, payload)
Expand Down
5 changes: 5 additions & 0 deletions spec/workflow/choice_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
it { expect { workflow }.to raise_exception(Floe::InvalidWorkflowError, "States.Choice1.Choices.0.Data field \"Variable\" value \"wrong\" Path [wrong] must start with \"$\"") }
end

context "with non-path Compare Key" do
let(:choices) { [{"Variable" => "$foo", "StringEqualsPath" => "wrong", "Next" => "FirstMatchState"}] }
it { expect { workflow }.to raise_exception(Floe::InvalidWorkflowError, "States.Choice1.Choices.0.Data field \"StringEqualsPath\" value \"wrong\" Path [wrong] must start with \"$\"") }
end

context "with second level Next (Not)" do
let(:choices) { [{"Not" => {"Variable" => "$.foo", "StringEquals" => "bar", "Next" => "FirstMatchState"}, "Next" => "FirstMatchState"}] }

Expand Down
1 change: 1 addition & 0 deletions spec/workflow/states/choice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
context "with a missing variable" do
it "shows error" do
workflow.run_nonblock
expect(ctx.failed?).to eq(true)
expect(ctx.output).to eq(
{
"Cause" => "Path [$.foo] references an invalid value",
Expand Down

0 comments on commit e7350f6

Please sign in to comment.