-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module Floe | ||
class Workflow | ||
class IntrinsicFunction | ||
class << self | ||
def klass(payload) | ||
function_module_name, function_name = | ||
payload.match(/^(?<module>\w+)\.(?<function>\w+)\(.*\)$/) | ||
.named_captures.values_at("module", "function") | ||
|
||
begin | ||
function_module = Floe::Workflow::IntrinsicFunctions.const_get(function_module_name) | ||
function_module.const_get(function_name) | ||
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 | ||
|
||
private alias_method :orig_new, :new | ||
|
||
def new(*args) | ||
if self == Floe::Workflow::IntrinsicFunction | ||
klass(*args).new(*args) | ||
else | ||
orig_new(*args) | ||
end | ||
end | ||
end | ||
|
||
attr_reader :args | ||
|
||
def initialize(payload) | ||
args = payload.match(/^\w+\.\w+\((.*)\)$/).captures.first | ||
@args = args.split(", ").map do |arg| | ||
if arg.start_with?("$.") | ||
Path.new(arg) | ||
elsif arg.match?(/^\w+\.\w+\(.*\)$/) | ||
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 |
23 changes: 23 additions & 0 deletions
23
lib/floe/workflow/intrinsic_functions/states/string_to_json.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 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 = | ||
if arg.kind_of?(Floe::Workflow::Path) || arg.kind_of?(Floe::Workflow::IntrinsicFunction) | ||
arg.value(context, inputs) | ||
else | ||
arg | ||
end | ||
|
||
JSON.parse(arg) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
RSpec.describe Floe::Workflow::IntrinsicFunction do | ||
describe ".klass" do | ||
it "returns the intrinsic function class" do | ||
payload = "States.StringToJson('{\"number\": 20}')" | ||
expect(described_class.klass(payload)).to eq(Floe::Workflow::IntrinsicFunctions::States::StringToJson) | ||
end | ||
|
||
it "raises an exception for an invalid intrinsic function" do | ||
payload = "States.MyFirstFunction()" | ||
expect { described_class.klass(payload) }.to raise_error(NotImplementedError) | ||
end | ||
end | ||
|
||
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 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters