Skip to content

Commit 5e30827

Browse files
committed
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 <charles.davernas@neuroglia.io>
1 parent 4afe30b commit 5e30827

11 files changed

+58
-76
lines changed

src/ServerlessWorkflow.Sdk.Builders/Interfaces/ISwitchTaskDefinitionBuilder.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,4 @@ public interface ISwitchTaskDefinitionBuilder
2828
/// <returns>The configured <see cref="ISwitchTaskDefinitionBuilder"/></returns>
2929
ISwitchTaskDefinitionBuilder Case(string name, Action<ISwitchCaseDefinitionBuilder> setup);
3030

31-
/// <summary>
32-
/// Builds the configured <see cref="SwitchTaskDefinition"/>
33-
/// </summary>
34-
/// <returns>A new <see cref="SwitchTaskDefinition"/></returns>
35-
SwitchTaskDefinition Build();
36-
3731
}

src/ServerlessWorkflow.Sdk.Builders/Interfaces/ITaskDefinitionMapBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public interface ITaskDefinitionMapBuilder<TBuilder>
2121
where TBuilder : ITaskDefinitionMapBuilder<TBuilder>
2222
{
2323

24+
/// <summary>
25+
/// Adds a new task with the specified name to the builder.
26+
/// </summary>
27+
/// <param name="name">The name of the task to add.</param>
28+
/// <param name="task">The task to add</param>
29+
/// <returns>The current instance of the task definition mapping builder.</returns>
30+
TBuilder Do(string name, TaskDefinition task);
31+
2432
/// <summary>
2533
/// Adds a new task with the specified name and configuration setup to the builder.
2634
/// </summary>

src/ServerlessWorkflow.Sdk.Builders/Interfaces/IWorkflowDefinitionBuilder.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,33 +106,17 @@ public interface IWorkflowDefinitionBuilder
106106
/// Uses the specified function
107107
/// </summary>
108108
/// <param name="name">The name of the function to use</param>
109-
/// <param name="call">The underlying call task the function performs</param>
109+
/// <param name="task">The underlying task the function performs</param>
110110
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
111-
IWorkflowDefinitionBuilder UseFunction(string name, CallTaskDefinition call);
111+
IWorkflowDefinitionBuilder UseFunction(string name, TaskDefinition task);
112112

113113
/// <summary>
114114
/// Uses the specified function
115115
/// </summary>
116116
/// <param name="name">The name of the function to use</param>
117-
/// <param name="setup">An <see cref="Action{T}"/> used to setup the underlying call task the function performs</param>
117+
/// <param name="setup">An <see cref="Action{T}"/> used to setup the underlying task the function performs</param>
118118
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
119-
IWorkflowDefinitionBuilder UseFunction(string name, Action<ICallTaskDefinitionBuilder> setup);
120-
121-
/// <summary>
122-
/// Uses the specified function
123-
/// </summary>
124-
/// <param name="name">The name of the function to use</param>
125-
/// <param name="run">The underlying run task the function performs</param>
126-
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
127-
IWorkflowDefinitionBuilder UseFunction(string name, RunTaskDefinition run);
128-
129-
/// <summary>
130-
/// Uses the specified function
131-
/// </summary>
132-
/// <param name="name">The name of the function to use</param>
133-
/// <param name="setup">An <see cref="Action{T}"/> used to setup the underlying run task the function performs</param>
134-
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
135-
IWorkflowDefinitionBuilder UseFunction(string name, Action<IRunTaskDefinitionBuilder> setup);
119+
IWorkflowDefinitionBuilder UseFunction(string name, Action<IGenericTaskDefinitionBuilder> setup);
136120

137121
/// <summary>
138122
/// Uses the specified retry policy

src/ServerlessWorkflow.Sdk.Builders/ServerlessWorkflow.Sdk.Builders.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<VersionPrefix>1.0.0</VersionPrefix>
8-
<VersionSuffix>alpha2.3</VersionSuffix>
8+
<VersionSuffix>alpha2.4</VersionSuffix>
99
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
1010
<FileVersion>$(VersionPrefix)</FileVersion>
1111
<NeutralLanguage>en</NeutralLanguage>

src/ServerlessWorkflow.Sdk.Builders/TaskDefinitionMapBuilder.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
15-
using Neuroglia;
16-
1714
namespace ServerlessWorkflow.Sdk.Builders;
1815

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

28+
/// <inheritdoc/>
29+
public virtual ITaskDefinitionMapBuilder Do(string name, TaskDefinition task)
30+
{
31+
ArgumentException.ThrowIfNullOrWhiteSpace(name);
32+
ArgumentNullException.ThrowIfNull(task);
33+
this.Tasks ??= [];
34+
this.Tasks[name] = task;
35+
return this;
36+
}
37+
3138
/// <inheritdoc/>
3239
public virtual ITaskDefinitionMapBuilder Do(string name, Action<IGenericTaskDefinitionBuilder> setup)
3340
{
3441
ArgumentException.ThrowIfNullOrWhiteSpace(name);
3542
ArgumentNullException.ThrowIfNull(setup);
3643
var builder = new GenericTaskDefinitionBuilder();
3744
setup(builder);
38-
this.Tasks ??= [];
39-
this.Tasks[name] = builder.Build();
40-
return this;
45+
var task = builder.Build();
46+
return this.Do(name, task);
4147
}
4248

4349
/// <inheritdoc/>

src/ServerlessWorkflow.Sdk.Builders/WorkflowDefinitionBuilder.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -160,39 +160,20 @@ public virtual IWorkflowDefinitionBuilder UseExtension(string name, Action<IExte
160160
}
161161

162162
/// <inheritdoc/>
163-
public virtual IWorkflowDefinitionBuilder UseFunction(string name, CallTaskDefinition call)
163+
public virtual IWorkflowDefinitionBuilder UseFunction(string name, TaskDefinition task)
164164
{
165165
ArgumentException.ThrowIfNullOrWhiteSpace(name);
166-
ArgumentNullException.ThrowIfNull(call);
166+
ArgumentNullException.ThrowIfNull(task);
167167
this.Components ??= new();
168168
this.Components.Functions ??= [];
169-
this.Components.Functions[name] = call;
169+
this.Components.Functions[name] = task;
170170
return this;
171171
}
172172

173173
/// <inheritdoc/>
174-
public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action<ICallTaskDefinitionBuilder> setup)
174+
public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action<IGenericTaskDefinitionBuilder> setup)
175175
{
176-
var builder = new CallTaskDefinitionBuilder();
177-
setup(builder);
178-
return this.UseFunction(name, builder.Build());
179-
}
180-
181-
/// <inheritdoc/>
182-
public virtual IWorkflowDefinitionBuilder UseFunction(string name, RunTaskDefinition run)
183-
{
184-
ArgumentException.ThrowIfNullOrWhiteSpace(name);
185-
ArgumentNullException.ThrowIfNull(run);
186-
this.Components ??= new();
187-
this.Components.Functions ??= [];
188-
this.Components.Functions[name] = run;
189-
return this;
190-
}
191-
192-
/// <inheritdoc/>
193-
public virtual IWorkflowDefinitionBuilder UseFunction(string name, Action<IRunTaskDefinitionBuilder> setup)
194-
{
195-
var builder = new RunTaskDefinitionBuilder();
176+
var builder = new GenericTaskDefinitionBuilder();
196177
setup(builder);
197178
return this.UseFunction(name, builder.Build());
198179
}
@@ -235,16 +216,25 @@ public virtual IWorkflowDefinitionBuilder UseSecrets(params string[] secrets)
235216
return this;
236217
}
237218

219+
/// <inheritdoc/>
220+
public virtual IWorkflowDefinitionBuilder Do(string name, TaskDefinition task)
221+
{
222+
ArgumentException.ThrowIfNullOrWhiteSpace(name);
223+
ArgumentNullException.ThrowIfNull(task);
224+
this.Tasks ??= [];
225+
this.Tasks[name] = task;
226+
return this;
227+
}
228+
238229
/// <inheritdoc/>
239230
public virtual IWorkflowDefinitionBuilder Do(string name, Action<IGenericTaskDefinitionBuilder> setup)
240231
{
241232
ArgumentException.ThrowIfNullOrWhiteSpace(name);
242233
ArgumentNullException.ThrowIfNull(setup);
243234
var builder = new GenericTaskDefinitionBuilder();
244235
setup(builder);
245-
this.Tasks ??= [];
246-
this.Tasks[name] = builder.Build();
247-
return this;
236+
var task = builder.Build();
237+
return this.Do(name, task);
248238
}
249239

250240
/// <inheritdoc/>

src/ServerlessWorkflow.Sdk.IO/ServerlessWorkflow.Sdk.IO.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<VersionPrefix>1.0.0</VersionPrefix>
8-
<VersionSuffix>alpha2.3</VersionSuffix>
8+
<VersionSuffix>alpha2.4</VersionSuffix>
99
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
1010
<FileVersion>$(VersionPrefix)</FileVersion>
1111
<NeutralLanguage>en</NeutralLanguage>

src/ServerlessWorkflow.Sdk/ServerlessWorkflow.Sdk.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<VersionPrefix>1.0.0</VersionPrefix>
8-
<VersionSuffix>alpha2.3</VersionSuffix>
8+
<VersionSuffix>alpha2.4</VersionSuffix>
99
<AssemblyVersion>$(VersionPrefix)</AssemblyVersion>
1010
<FileVersion>$(VersionPrefix)</FileVersion>
1111
<NeutralLanguage>en</NeutralLanguage>

tests/ServerlessWorkflow.Sdk.UnitTests/Cases/Builders/WorkflowDefinitionBuilderTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ public void Build_Should_Work()
5252
.WithId("fake-client-id")
5353
.WithSecret("fake-client-secret")))
5454
.UseFunction("fakeFunction1", function => function
55-
.Function("http")
56-
.With("method", "post")
57-
.With("uri", "https://test.com"))
55+
.Call()
56+
.Function("http")
57+
.With("method", "post")
58+
.With("uri", "https://test.com"))
5859
.UseFunction("fakeFunction2", function => function
59-
.Shell()
60-
.WithCommand(@"echo ""Hello, World!"""))
60+
.Run()
61+
.Shell()
62+
.WithCommand(@"echo ""Hello, World!"""))
6163
.UseExtension("fakeLoggingExtension", extension => extension
6264
.ExtendAll()
6365
.When("fake-expression")

tests/ServerlessWorkflow.Sdk.UnitTests/Cases/IO/WorkflowDefinitionIOTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// limitations under the License.
1313

1414
using ServerlessWorkflow.Sdk.IO;
15-
using System.Text;
1615

1716
namespace ServerlessWorkflow.Sdk.UnitTests.Cases.IO;
1817

@@ -31,9 +30,6 @@ public async Task WriteThenRead_Workflow_Definition_ToFrom_Yaml_Should_Work()
3130
//act
3231
await writer.WriteAsync(toSerialize, stream, WorkflowDefinitionFormat.Yaml);
3332
await stream.FlushAsync();
34-
35-
var yaml = Encoding.UTF8.GetString(stream.ToArray());
36-
3733
stream.Position = 0;
3834
var deserialized = await reader.ReadAsync(stream);
3935

tests/ServerlessWorkflow.Sdk.UnitTests/Services/WorkflowDefinitionFactory.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ internal static WorkflowDefinition Create()
4141
.WithId("fake-client-id")
4242
.WithSecret("fake-client-secret")))
4343
.UseFunction("fakeFunction1", function => function
44-
.Function("http")
45-
.With("method", "post")
46-
.With("uri", "https://test.com"))
44+
.Call()
45+
.Function("http")
46+
.With("method", "post")
47+
.With("uri", "https://test.com"))
4748
.UseFunction("fakeFunction2", function => function
48-
.Shell()
49-
.WithCommand(@"echo ""Hello, World!"""))
49+
.Run()
50+
.Shell()
51+
.WithCommand(@"echo ""Hello, World!"""))
5052
.UseExtension("fakeLoggingExtension", extension => extension
5153
.ExtendAll()
5254
.When("fake-expression")

0 commit comments

Comments
 (0)