Skip to content

Commit 856fce4

Browse files
committed
introduce binary choice rules
only access payload in initialize
1 parent 604fc35 commit 856fce4

File tree

2 files changed

+16
-39
lines changed

2 files changed

+16
-39
lines changed

lib/floe/workflow/choice_rule/data.rb

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,36 @@ module Floe
44
class Workflow
55
class ChoiceRule
66
class Data < Floe::Workflow::ChoiceRule
7-
COMPARE_KEYS = %w[IsNull IsPresent IsNumeric IsString IsBoolean IsTimestamp String Numeric Boolean Timestamp].freeze
7+
COMPARE_RULE = /^(String|Numeric|Boolean|Timestamp)(Equals|GreaterThan|LessThan|GreaterThanEquals|LessThanEquals|Matches)(Path)?$/.freeze
8+
OPERATIONS = {"Equals" => :==, "LessThan" => :<, "GreaterThan" => :>, "LessThanEquals" => :<=, "GreaterThanEquals" => :>=, "Matches" => "match?"}.freeze
89

9-
attr_reader :compare_key
10+
attr_reader :operation, :ref, :path
1011

1112
def initialize(*)
1213
super
13-
@compare_key = payload.keys.detect { |key| key.match?(/^(#{COMPARE_KEYS.join("|")})/) }
14+
payload.each.detect do |key, value|
15+
_type, operator, @path = COMPARE_RULE.match(key)&.captures
16+
@operation = OPERATIONS[operator]
17+
@ref = value
18+
operator # detect aborts once an operator is found
19+
end
1420

15-
raise Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have a compare key" if @compare_key.nil?
21+
raise Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have a valid compare key" unless operation
1622
end
1723

1824
def true?(context, input)
1925
lhs = variable_value(context, input)
20-
rhs = compare_value(context, input)
21-
return false unless valid?(lhs)
26+
return false if lhs.nil?
2227

23-
case compare_key
24-
when "StringEquals", "StringEqualsPath",
25-
"NumericEquals", "NumericEqualsPath",
26-
"BooleanEquals", "BooleanEqualsPath",
27-
"TimestampEquals", "TimestampEqualsPath"
28-
lhs == rhs
29-
when "StringLessThan", "StringLessThanPath",
30-
"NumericLessThan", "NumericLessThanPath",
31-
"TimestampLessThan", "TimestampLessThanPath"
32-
lhs < rhs
33-
when "StringGreaterThan", "StringGreaterThanPath",
34-
"NumericGreaterThan", "NumericGreaterThanPath",
35-
"TimestampGreaterThan", "TimestampGreaterThanPath"
36-
lhs > rhs
37-
when "StringLessThanEquals", "StringLessThanEqualsPath",
38-
"NumericLessThanEquals", "NumericLessThanEqualsPath",
39-
"TimestampLessThanEquals", "TimestampLessThanEqualsPath"
40-
lhs <= rhs
41-
when "StringGreaterThanEquals", "StringGreaterThanEqualsPath",
42-
"NumericGreaterThanEquals", "NumericGreaterThanEqualsPath",
43-
"TimestampGreaterThanEquals", "TimestampGreaterThanEqualsPath"
44-
lhs >= rhs
45-
when "StringMatches"
46-
lhs.match?(Regexp.escape(rhs).gsub('\*', '.*?'))
47-
else
48-
raise Floe::InvalidWorkflowError, "Invalid choice [#{compare_key}]"
49-
end
28+
rhs = compare_value(context, input)
29+
rhs = Regexp.escape(rhs).gsub('\*', '.*?') if operation == "match?"
30+
lhs.send(operation, rhs)
5031
end
5132

5233
private
5334

54-
def valid?(value)
55-
!value.nil?
56-
end
57-
5835
def compare_value(context, input)
59-
compare_key.end_with?("Path") ? Path.value(payload[compare_key], context, input) : payload[compare_key]
36+
path ? Path.value(ref, context, input) : ref
6037
end
6138
end
6239
end

spec/workflow/choice_rule_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
let(:input) { {"foo" => "bar"} }
8989

9090
it "raises an exception" do
91-
expect { subject }.to raise_exception(Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have a compare key")
91+
expect { subject }.to raise_exception(Floe::InvalidWorkflowError, "Data-test Expression Choice Rule must have a valid compare key")
9292
end
9393
end
9494

0 commit comments

Comments
 (0)