Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add Intrinsic Functions #84

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/floe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
require_relative "floe/workflow/choice_rule/and"
require_relative "floe/workflow/choice_rule/data"
require_relative "floe/workflow/context"
require_relative "floe/workflow/intrinsic_function"
require_relative "floe/workflow/intrinsic_functions/states/string_to_json"
require_relative "floe/workflow/path"
require_relative "floe/workflow/payload_template"
require_relative "floe/workflow/reference_path"
Expand Down
56 changes: 56 additions & 0 deletions lib/floe/workflow/intrinsic_function.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

module Floe
class Workflow
class IntrinsicFunction
INTRINSIC_FUNCTION_REGEX = /^(?<module>\w+)\.(?<function>\w+)\((?<args>.*)\)$/.freeze

class << self
def new(*args)
if self == Floe::Workflow::IntrinsicFunction
detect_class(*args).new(*args)
else
super
end
end

private def detect_class(payload)
function_module_name, function_name =
payload.match(INTRINSIC_FUNCTION_REGEX)
.named_captures
.values_at("module", "function")

begin
function_module = Floe::Workflow::IntrinsicFunctions.const_get(function_module_name)
function_module.const_get(function_name)
Copy link
Member

@Fryguy Fryguy Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one concern here. I was looking through the list of Intrinsic Functions and there is one called States.Array. This would imply we have a Floe::Workflow::IntrinsicFunctions::States::Array class, and that Array shadowing kind of scares me (regardless of const_get or literal). I'm wondering if instead of a full namespacing, we instead just smash the module/function together so you'd get something more like: Floe::Workflow::IntrinsicFunctions::StatesArray. Similarly, there is a States::Hash class. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm we could have States be a class with methods def StringToJson and def Array rather than having classes for each intrinsic function. I like having these separated because if/when we have our own ManageIQ intrinsic functions they will be nicely separated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe? methods named with capitals will surely get confusing. If they are class methods then, technicall :: will call those too, which works, but would still be weird. Reminds me of the Vmdb::BUILD method we have.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like going the send route, aka: def StringToJson (or def intrinsicStringToJson if we need to start with a lowercase). Alternatively a case statement may not be the most ruby thing, but that tends to be the defacto implementation for this type of construct across all programming languages (both target language and complier/interpreter language).

All intrinsics are in the same namespace: States.*. I think hardcoding this rather than a generic "look for a period" works best, and we can hardcode any additional namespaces in the future. Lets also avoid regular expressions and use indexOf(")") kind of stuff. It is way too hard to deal with properly nesting parens and regular expressions. Though this implementation may be ok for a first pass.

rescue NameError
raise NotImplementedError, "#{function_module_name}.#{function_name} is not implemented"
end
end

def value(payload, context, input = {})
new(payload).value(context, input)
end
end

attr_reader :args

def initialize(payload)
args = payload.match(INTRINSIC_FUNCTION_REGEX).named_captures["args"]
@args = args.split(", ").map do |arg|
if arg.start_with?("$.")
Path.new(arg)
elsif arg.match?(INTRINSIC_FUNCTION_REGEX)
Floe::Workflow::IntrinsicFunction.new(arg)
else
arg
end
end
end

def value(_context, _inputs)
raise NotImplementedError, "must be implemented in a subclass"
end
end
end
end
18 changes: 18 additions & 0 deletions lib/floe/workflow/intrinsic_functions/states/string_to_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Floe
class Workflow
module IntrinsicFunctions
module States
class StringToJson < Floe::Workflow::IntrinsicFunction
def value(context, inputs)
arg = args.first
arg = arg.value(context, inputs) if arg.respond_to?(:value)

JSON.parse(arg)
end
end
end
end
end
end
10 changes: 8 additions & 2 deletions lib/floe/workflow/payload_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ def parse_payload_hash(value)
end

def parse_payload_string(value)
value.start_with?("$") ? Path.new(value) : value
if value.start_with?("$")
Path.new(value)
elsif value.match?(/^\w+\.\w+\(.+\)$/)
IntrinsicFunction.new(value)
else
value
end
end

def interpolate_value(value, context, inputs)
case value
when Array then interpolate_value_array(value, context, inputs)
when Hash then interpolate_value_hash(value, context, inputs)
when Path then value.value(context, inputs)
when Path, IntrinsicFunction then value.value(context, inputs)
else
value
end
Expand Down
51 changes: 51 additions & 0 deletions spec/workflow/intrinsic_function_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
RSpec.describe Floe::Workflow::IntrinsicFunction do
describe "#initialize" do
let(:payload) { "States.StringToJson()" }

it "returns an instance of the intrinsic function" do
expect(described_class.new(payload)).to be_kind_of(Floe::Workflow::IntrinsicFunctions::States::StringToJson)
end

context "with an invalid intrinsic function" do
let(:payload) { "States.MyFirstFunction()" }

it "raises an exception for an invalid intrinsic function" do
expect { described_class.new(payload) }.to raise_error(NotImplementedError)
end
end

context "with no arguments" do
it "parses args as empty" do
function = described_class.new(payload)
expect(function.args).to be_empty
end
end

context "with a string arguments" do
let(:payload) { "States.StringToJson(foobar)" }

it "parses the arguments" do
function = described_class.new(payload)
expect(function.args).to eq(["foobar"])
end
end

context "with a Path" do
let(:payload) { "States.StringToJson($.someString)" }

it "parses the arguments" do
function = described_class.new(payload)
expect(function.args.first).to be_kind_of(Floe::Workflow::Path)
end
end

context "with an IntrinsicFunction" do
let(:payload) { "States.StringToJson(States.StringToJson($.someString))" }

it "parses the arguments" do
function = described_class.new(payload)
expect(function.args.first).to be_kind_of(Floe::Workflow::IntrinsicFunction)
end
end
end
end
12 changes: 12 additions & 0 deletions spec/workflow/payload_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,17 @@
expect(subject.value(context, inputs)).to eq({"foo" => ["bar", "baz"], "bar" => {"hello" => "world"}})
end
end

context "with intrinsic functions" do
context "States.StringToJson" do
let(:context) { {} }
let(:payload) { {"foo.$" => "States.StringToJson($.someString)"} }
let(:inputs) { {"someString" => "{\"number\": 20}"} }

it "sets foo to the parsed JSON from inputs" do
expect(subject.value(context, inputs)).to eq({"foo" => {"number" => 20}})
end
end
end
end
end