From 5e3082706e3b8a02f391be4c5b78a9d7f2032cd9 Mon Sep 17 00:00:00 2001 From: Charles d'Avernas Date: Wed, 26 Jun 2024 14:12:02 +0200 Subject: [PATCH] fix(Builders): Fixed the IWorkflowDefinitionBuilder to allow using any task as function fix(Builders): Added new overloads to the ITaskDefinitionMapBuilder Signed-off-by: Charles d'Avernas --- .../ISwitchTaskDefinitionBuilder.cs | 6 --- .../Interfaces/ITaskDefinitionMapBuilder.cs | 8 ++++ .../Interfaces/IWorkflowDefinitionBuilder.cs | 24 ++-------- .../ServerlessWorkflow.Sdk.Builders.csproj | 2 +- .../TaskDefinitionMapBuilder.cs | 18 +++++--- .../WorkflowDefinitionBuilder.cs | 44 +++++++------------ .../ServerlessWorkflow.Sdk.IO.csproj | 2 +- .../ServerlessWorkflow.Sdk.csproj | 2 +- .../WorkflowDefinitionBuilderTests.cs | 12 ++--- .../Cases/IO/WorkflowDefinitionIOTests.cs | 4 -- .../Services/WorkflowDefinitionFactory.cs | 12 ++--- 11 files changed, 58 insertions(+), 76 deletions(-) diff --git a/src/ServerlessWorkflow.Sdk.Builders/Interfaces/ISwitchTaskDefinitionBuilder.cs b/src/ServerlessWorkflow.Sdk.Builders/Interfaces/ISwitchTaskDefinitionBuilder.cs index f593f69..37fc5c2 100644 --- a/src/ServerlessWorkflow.Sdk.Builders/Interfaces/ISwitchTaskDefinitionBuilder.cs +++ b/src/ServerlessWorkflow.Sdk.Builders/Interfaces/ISwitchTaskDefinitionBuilder.cs @@ -28,10 +28,4 @@ public interface ISwitchTaskDefinitionBuilder /// The configured ISwitchTaskDefinitionBuilder Case(string name, Action setup); - /// - /// Builds the configured - /// - /// A new - SwitchTaskDefinition Build(); - } diff --git a/src/ServerlessWorkflow.Sdk.Builders/Interfaces/ITaskDefinitionMapBuilder.cs b/src/ServerlessWorkflow.Sdk.Builders/Interfaces/ITaskDefinitionMapBuilder.cs index 5a98ef7..4eeb1b3 100644 --- a/src/ServerlessWorkflow.Sdk.Builders/Interfaces/ITaskDefinitionMapBuilder.cs +++ b/src/ServerlessWorkflow.Sdk.Builders/Interfaces/ITaskDefinitionMapBuilder.cs @@ -21,6 +21,14 @@ public interface ITaskDefinitionMapBuilder where TBuilder : ITaskDefinitionMapBuilder { + /// + /// Adds a new task with the specified name to the builder. + /// + /// The name of the task to add. + /// The task to add + /// The current instance of the task definition mapping builder. + TBuilder Do(string name, TaskDefinition task); + /// /// Adds a new task with the specified name and configuration setup to the builder. /// diff --git a/src/ServerlessWorkflow.Sdk.Builders/Interfaces/IWorkflowDefinitionBuilder.cs b/src/ServerlessWorkflow.Sdk.Builders/Interfaces/IWorkflowDefinitionBuilder.cs index 92d4295..c56d6ae 100644 --- a/src/ServerlessWorkflow.Sdk.Builders/Interfaces/IWorkflowDefinitionBuilder.cs +++ b/src/ServerlessWorkflow.Sdk.Builders/Interfaces/IWorkflowDefinitionBuilder.cs @@ -106,33 +106,17 @@ public interface IWorkflowDefinitionBuilder /// Uses the specified function /// /// The name of the function to use - /// The underlying call task the function performs + /// The underlying task the function performs /// The configured - IWorkflowDefinitionBuilder UseFunction(string name, CallTaskDefinition call); + IWorkflowDefinitionBuilder UseFunction(string name, TaskDefinition task); /// /// Uses the specified function /// /// The name of the function to use - /// An used to setup the underlying call task the function performs + /// An used to setup the underlying task the function performs /// The configured - IWorkflowDefinitionBuilder UseFunction(string name, Action setup); - - /// - /// Uses the specified function - /// - /// The name of the function to use - /// The underlying run task the function performs - /// The configured - IWorkflowDefinitionBuilder UseFunction(string name, RunTaskDefinition run); - - /// - /// Uses the specified function - /// - /// The name of the function to use - /// An used to setup the underlying run task the function performs - /// The configured - IWorkflowDefinitionBuilder UseFunction(string name, Action setup); + IWorkflowDefinitionBuilder UseFunction(string name, Action setup); /// /// Uses the specified retry policy diff --git a/src/ServerlessWorkflow.Sdk.Builders/ServerlessWorkflow.Sdk.Builders.csproj b/src/ServerlessWorkflow.Sdk.Builders/ServerlessWorkflow.Sdk.Builders.csproj index a1dbbd0..2717de7 100644 --- a/src/ServerlessWorkflow.Sdk.Builders/ServerlessWorkflow.Sdk.Builders.csproj +++ b/src/ServerlessWorkflow.Sdk.Builders/ServerlessWorkflow.Sdk.Builders.csproj @@ -5,7 +5,7 @@ enable enable 1.0.0 - alpha2.3 + alpha2.4 $(VersionPrefix) $(VersionPrefix) en diff --git a/src/ServerlessWorkflow.Sdk.Builders/TaskDefinitionMapBuilder.cs b/src/ServerlessWorkflow.Sdk.Builders/TaskDefinitionMapBuilder.cs index 11074c5..e25b342 100644 --- a/src/ServerlessWorkflow.Sdk.Builders/TaskDefinitionMapBuilder.cs +++ b/src/ServerlessWorkflow.Sdk.Builders/TaskDefinitionMapBuilder.cs @@ -11,9 +11,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - -using Neuroglia; - namespace ServerlessWorkflow.Sdk.Builders; /// @@ -28,6 +25,16 @@ public class TaskDefinitionMapBuilder /// protected Map? Tasks { get; set; } + /// + public virtual ITaskDefinitionMapBuilder Do(string name, TaskDefinition task) + { + ArgumentException.ThrowIfNullOrWhiteSpace(name); + ArgumentNullException.ThrowIfNull(task); + this.Tasks ??= []; + this.Tasks[name] = task; + return this; + } + /// public virtual ITaskDefinitionMapBuilder Do(string name, Action setup) { @@ -35,9 +42,8 @@ public virtual ITaskDefinitionMapBuilder Do(string name, Action diff --git a/src/ServerlessWorkflow.Sdk.Builders/WorkflowDefinitionBuilder.cs b/src/ServerlessWorkflow.Sdk.Builders/WorkflowDefinitionBuilder.cs index 8bf44fd..93744ff 100644 --- a/src/ServerlessWorkflow.Sdk.Builders/WorkflowDefinitionBuilder.cs +++ b/src/ServerlessWorkflow.Sdk.Builders/WorkflowDefinitionBuilder.cs @@ -160,39 +160,20 @@ public virtual IWorkflowDefinitionBuilder UseExtension(string name, Action - public virtual IWorkflowDefinitionBuilder UseFunction(string name, CallTaskDefinition call) + public virtual IWorkflowDefinitionBuilder UseFunction(string name, TaskDefinition task) { ArgumentException.ThrowIfNullOrWhiteSpace(name); - ArgumentNullException.ThrowIfNull(call); + ArgumentNullException.ThrowIfNull(task); this.Components ??= new(); this.Components.Functions ??= []; - this.Components.Functions[name] = call; + this.Components.Functions[name] = task; return this; } /// - public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action setup) + public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action setup) { - var builder = new CallTaskDefinitionBuilder(); - setup(builder); - return this.UseFunction(name, builder.Build()); - } - - /// - public virtual IWorkflowDefinitionBuilder UseFunction(string name, RunTaskDefinition run) - { - ArgumentException.ThrowIfNullOrWhiteSpace(name); - ArgumentNullException.ThrowIfNull(run); - this.Components ??= new(); - this.Components.Functions ??= []; - this.Components.Functions[name] = run; - return this; - } - - /// - public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action setup) - { - var builder = new RunTaskDefinitionBuilder(); + var builder = new GenericTaskDefinitionBuilder(); setup(builder); return this.UseFunction(name, builder.Build()); } @@ -235,6 +216,16 @@ public virtual IWorkflowDefinitionBuilder UseSecrets(params string[] secrets) return this; } + /// + public virtual IWorkflowDefinitionBuilder Do(string name, TaskDefinition task) + { + ArgumentException.ThrowIfNullOrWhiteSpace(name); + ArgumentNullException.ThrowIfNull(task); + this.Tasks ??= []; + this.Tasks[name] = task; + return this; + } + /// public virtual IWorkflowDefinitionBuilder Do(string name, Action setup) { @@ -242,9 +233,8 @@ public virtual IWorkflowDefinitionBuilder Do(string name, Action diff --git a/src/ServerlessWorkflow.Sdk.IO/ServerlessWorkflow.Sdk.IO.csproj b/src/ServerlessWorkflow.Sdk.IO/ServerlessWorkflow.Sdk.IO.csproj index fc0d880..bcdd380 100644 --- a/src/ServerlessWorkflow.Sdk.IO/ServerlessWorkflow.Sdk.IO.csproj +++ b/src/ServerlessWorkflow.Sdk.IO/ServerlessWorkflow.Sdk.IO.csproj @@ -5,7 +5,7 @@ enable enable 1.0.0 - alpha2.3 + alpha2.4 $(VersionPrefix) $(VersionPrefix) en diff --git a/src/ServerlessWorkflow.Sdk/ServerlessWorkflow.Sdk.csproj b/src/ServerlessWorkflow.Sdk/ServerlessWorkflow.Sdk.csproj index f325c81..ebf218f 100644 --- a/src/ServerlessWorkflow.Sdk/ServerlessWorkflow.Sdk.csproj +++ b/src/ServerlessWorkflow.Sdk/ServerlessWorkflow.Sdk.csproj @@ -5,7 +5,7 @@ enable enable 1.0.0 - alpha2.3 + alpha2.4 $(VersionPrefix) $(VersionPrefix) en diff --git a/tests/ServerlessWorkflow.Sdk.UnitTests/Cases/Builders/WorkflowDefinitionBuilderTests.cs b/tests/ServerlessWorkflow.Sdk.UnitTests/Cases/Builders/WorkflowDefinitionBuilderTests.cs index 7e32aae..2d83186 100644 --- a/tests/ServerlessWorkflow.Sdk.UnitTests/Cases/Builders/WorkflowDefinitionBuilderTests.cs +++ b/tests/ServerlessWorkflow.Sdk.UnitTests/Cases/Builders/WorkflowDefinitionBuilderTests.cs @@ -52,12 +52,14 @@ public void Build_Should_Work() .WithId("fake-client-id") .WithSecret("fake-client-secret"))) .UseFunction("fakeFunction1", function => function - .Function("http") - .With("method", "post") - .With("uri", "https://test.com")) + .Call() + .Function("http") + .With("method", "post") + .With("uri", "https://test.com")) .UseFunction("fakeFunction2", function => function - .Shell() - .WithCommand(@"echo ""Hello, World!""")) + .Run() + .Shell() + .WithCommand(@"echo ""Hello, World!""")) .UseExtension("fakeLoggingExtension", extension => extension .ExtendAll() .When("fake-expression") diff --git a/tests/ServerlessWorkflow.Sdk.UnitTests/Cases/IO/WorkflowDefinitionIOTests.cs b/tests/ServerlessWorkflow.Sdk.UnitTests/Cases/IO/WorkflowDefinitionIOTests.cs index 18c1d22..a3a37d0 100644 --- a/tests/ServerlessWorkflow.Sdk.UnitTests/Cases/IO/WorkflowDefinitionIOTests.cs +++ b/tests/ServerlessWorkflow.Sdk.UnitTests/Cases/IO/WorkflowDefinitionIOTests.cs @@ -12,7 +12,6 @@ // limitations under the License. using ServerlessWorkflow.Sdk.IO; -using System.Text; namespace ServerlessWorkflow.Sdk.UnitTests.Cases.IO; @@ -31,9 +30,6 @@ public async Task WriteThenRead_Workflow_Definition_ToFrom_Yaml_Should_Work() //act await writer.WriteAsync(toSerialize, stream, WorkflowDefinitionFormat.Yaml); await stream.FlushAsync(); - - var yaml = Encoding.UTF8.GetString(stream.ToArray()); - stream.Position = 0; var deserialized = await reader.ReadAsync(stream); diff --git a/tests/ServerlessWorkflow.Sdk.UnitTests/Services/WorkflowDefinitionFactory.cs b/tests/ServerlessWorkflow.Sdk.UnitTests/Services/WorkflowDefinitionFactory.cs index cef873c..f0331c4 100644 --- a/tests/ServerlessWorkflow.Sdk.UnitTests/Services/WorkflowDefinitionFactory.cs +++ b/tests/ServerlessWorkflow.Sdk.UnitTests/Services/WorkflowDefinitionFactory.cs @@ -41,12 +41,14 @@ internal static WorkflowDefinition Create() .WithId("fake-client-id") .WithSecret("fake-client-secret"))) .UseFunction("fakeFunction1", function => function - .Function("http") - .With("method", "post") - .With("uri", "https://test.com")) + .Call() + .Function("http") + .With("method", "post") + .With("uri", "https://test.com")) .UseFunction("fakeFunction2", function => function - .Shell() - .WithCommand(@"echo ""Hello, World!""")) + .Run() + .Shell() + .WithCommand(@"echo ""Hello, World!""")) .UseExtension("fakeLoggingExtension", extension => extension .ExtendAll() .When("fake-expression")