Skip to content

Commit

Permalink
fix(Builders): Fixed the IWorkflowDefinitionBuilder to allow using an…
Browse files Browse the repository at this point in the history
…y task as function

fix(Builders): Added new overloads to the ITaskDefinitionMapBuilder

Signed-off-by: Charles d'Avernas <[email protected]>
  • Loading branch information
cdavernas committed Jun 26, 2024
1 parent 4afe30b commit 5e30827
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,4 @@ public interface ISwitchTaskDefinitionBuilder
/// <returns>The configured <see cref="ISwitchTaskDefinitionBuilder"/></returns>
ISwitchTaskDefinitionBuilder Case(string name, Action<ISwitchCaseDefinitionBuilder> setup);

/// <summary>
/// Builds the configured <see cref="SwitchTaskDefinition"/>
/// </summary>
/// <returns>A new <see cref="SwitchTaskDefinition"/></returns>
SwitchTaskDefinition Build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public interface ITaskDefinitionMapBuilder<TBuilder>
where TBuilder : ITaskDefinitionMapBuilder<TBuilder>
{

/// <summary>
/// Adds a new task with the specified name to the builder.
/// </summary>
/// <param name="name">The name of the task to add.</param>
/// <param name="task">The task to add</param>
/// <returns>The current instance of the task definition mapping builder.</returns>
TBuilder Do(string name, TaskDefinition task);

/// <summary>
/// Adds a new task with the specified name and configuration setup to the builder.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,17 @@ public interface IWorkflowDefinitionBuilder
/// Uses the specified function
/// </summary>
/// <param name="name">The name of the function to use</param>
/// <param name="call">The underlying call task the function performs</param>
/// <param name="task">The underlying task the function performs</param>
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
IWorkflowDefinitionBuilder UseFunction(string name, CallTaskDefinition call);
IWorkflowDefinitionBuilder UseFunction(string name, TaskDefinition task);

/// <summary>
/// Uses the specified function
/// </summary>
/// <param name="name">The name of the function to use</param>
/// <param name="setup">An <see cref="Action{T}"/> used to setup the underlying call task the function performs</param>
/// <param name="setup">An <see cref="Action{T}"/> used to setup the underlying task the function performs</param>
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
IWorkflowDefinitionBuilder UseFunction(string name, Action<ICallTaskDefinitionBuilder> setup);

/// <summary>
/// Uses the specified function
/// </summary>
/// <param name="name">The name of the function to use</param>
/// <param name="run">The underlying run task the function performs</param>
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
IWorkflowDefinitionBuilder UseFunction(string name, RunTaskDefinition run);

/// <summary>
/// Uses the specified function
/// </summary>
/// <param name="name">The name of the function to use</param>
/// <param name="setup">An <see cref="Action{T}"/> used to setup the underlying run task the function performs</param>
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
IWorkflowDefinitionBuilder UseFunction(string name, Action<IRunTaskDefinitionBuilder> setup);
IWorkflowDefinitionBuilder UseFunction(string name, Action<IGenericTaskDefinitionBuilder> setup);

/// <summary>
/// Uses the specified retry policy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>alpha2.3</VersionSuffix>
<VersionSuffix>alpha2.4</VersionSuffix>
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
<FileVersion>$(VersionPrefix)</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
Expand Down
18 changes: 12 additions & 6 deletions src/ServerlessWorkflow.Sdk.Builders/TaskDefinitionMapBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.


using Neuroglia;

namespace ServerlessWorkflow.Sdk.Builders;

/// <summary>
Expand All @@ -28,16 +25,25 @@ public class TaskDefinitionMapBuilder
/// </summary>
protected Map<string, TaskDefinition>? Tasks { get; set; }

/// <inheritdoc/>
public virtual ITaskDefinitionMapBuilder Do(string name, TaskDefinition task)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentNullException.ThrowIfNull(task);
this.Tasks ??= [];
this.Tasks[name] = task;
return this;
}

/// <inheritdoc/>
public virtual ITaskDefinitionMapBuilder Do(string name, Action<IGenericTaskDefinitionBuilder> setup)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentNullException.ThrowIfNull(setup);
var builder = new GenericTaskDefinitionBuilder();
setup(builder);
this.Tasks ??= [];
this.Tasks[name] = builder.Build();
return this;
var task = builder.Build();
return this.Do(name, task);
}

/// <inheritdoc/>
Expand Down
44 changes: 17 additions & 27 deletions src/ServerlessWorkflow.Sdk.Builders/WorkflowDefinitionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,39 +160,20 @@ public virtual IWorkflowDefinitionBuilder UseExtension(string name, Action<IExte
}

/// <inheritdoc/>
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;
}

/// <inheritdoc/>
public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action<ICallTaskDefinitionBuilder> setup)
public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action<IGenericTaskDefinitionBuilder> setup)
{
var builder = new CallTaskDefinitionBuilder();
setup(builder);
return this.UseFunction(name, builder.Build());
}

/// <inheritdoc/>
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;
}

/// <inheritdoc/>
public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action<IRunTaskDefinitionBuilder> setup)
{
var builder = new RunTaskDefinitionBuilder();
var builder = new GenericTaskDefinitionBuilder();
setup(builder);
return this.UseFunction(name, builder.Build());
}
Expand Down Expand Up @@ -235,16 +216,25 @@ public virtual IWorkflowDefinitionBuilder UseSecrets(params string[] secrets)
return this;
}

/// <inheritdoc/>
public virtual IWorkflowDefinitionBuilder Do(string name, TaskDefinition task)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentNullException.ThrowIfNull(task);
this.Tasks ??= [];
this.Tasks[name] = task;
return this;
}

/// <inheritdoc/>
public virtual IWorkflowDefinitionBuilder Do(string name, Action<IGenericTaskDefinitionBuilder> setup)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentNullException.ThrowIfNull(setup);
var builder = new GenericTaskDefinitionBuilder();
setup(builder);
this.Tasks ??= [];
this.Tasks[name] = builder.Build();
return this;
var task = builder.Build();
return this.Do(name, task);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>alpha2.3</VersionSuffix>
<VersionSuffix>alpha2.4</VersionSuffix>
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
<FileVersion>$(VersionPrefix)</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
Expand Down
2 changes: 1 addition & 1 deletion src/ServerlessWorkflow.Sdk/ServerlessWorkflow.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>alpha2.3</VersionSuffix>
<VersionSuffix>alpha2.4</VersionSuffix>
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
<FileVersion>$(VersionPrefix)</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// limitations under the License.

using ServerlessWorkflow.Sdk.IO;
using System.Text;

namespace ServerlessWorkflow.Sdk.UnitTests.Cases.IO;

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5e30827

Please sign in to comment.