Skip to content

Commit

Permalink
More granular compare_key and determine path at initialization time
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Aug 20, 2024
1 parent 5f3f1b1 commit 8a47393
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/floe/workflow/choice_rule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ 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, :value, :path
attr_reader :variable, :compare_key, :type, :value, :path

def initialize(_workflow, _name, payload)
super
Expand Down Expand Up @@ -109,10 +114,13 @@ def is_timestamp?(value, predicate = true)
# rubocop:enable Style/OptionalBooleanParameter

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

@path = compare_key.end_with?("Path")
@type, _operator, @path = OPERATION.match(compare_key)&.captures
# TYPE_CHECK doesn't match this regex, so @path = @type = nil
# @path.nil? means this the compare_value will always be a static value (true or false)
# @type.nil? means we won't type check the variable or compare value
end

def compare_value(context, input)
Expand Down

0 comments on commit 8a47393

Please sign in to comment.