Skip to content

Commit

Permalink
fix(Sdk): Fixed the TaskDefinition, which was missing the if property
Browse files Browse the repository at this point in the history
Signed-off-by: Charles d'Avernas <[email protected]>
  • Loading branch information
cdavernas committed Jun 26, 2024
1 parent 8dc1190 commit f61dade
Show file tree
Hide file tree
Showing 31 changed files with 100 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// </summary>
/// <param name="functionName">The name of the function to call</param>
public class CallTaskDefinitionBuilder(string? functionName = null)
: TaskDefinitionBuilder<CallTaskDefinition>, ICallTaskDefinitionBuilder
: TaskDefinitionBuilder<ICallTaskDefinitionBuilder, CallTaskDefinition>, ICallTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Represents the default implementation of the <see cref="IDoTaskDefinitionBuilder"/> interface
/// </summary>
public class DoTaskDefinitionBuilder
: TaskDefinitionBuilder<DoTaskDefinition>, IDoTaskDefinitionBuilder
: TaskDefinitionBuilder<IDoTaskDefinitionBuilder, DoTaskDefinition>, IDoTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// </summary>
/// <param name="e">The definition of the event to emit</param>
public class EmitTaskDefinitionBuilder(EventDefinition? e = null)
: IEmitTaskDefinitionBuilder
: TaskDefinitionBuilder<IEmitTaskDefinitionBuilder, EmitTaskDefinition>, IEmitTaskDefinitionBuilder
{

/// <summary>
Expand All @@ -45,7 +45,7 @@ public virtual IEmitTaskDefinitionBuilder Event(Action<IEventDefinitionBuilder>
}

/// <inheritdoc/>
public virtual EmitTaskDefinition Build()
public override EmitTaskDefinition Build()
{
if (this.EventDefinition == null) throw new NullReferenceException("The event to emit must be defined");
return new()
Expand All @@ -57,6 +57,4 @@ public virtual EmitTaskDefinition Build()
};
}

TaskDefinition ITaskDefinitionBuilder.Build() => this.Build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Represents the default implementation of the <see cref="IForTaskDefinitionBuilder"/> interface
/// </summary>
public class ForTaskDefinitionBuilder
: TaskDefinitionBuilder<ForTaskDefinition>, IForTaskDefinitionBuilder
: TaskDefinitionBuilder<IForTaskDefinitionBuilder, ForTaskDefinition>, IForTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Represents the default implementation of the <see cref="IForkTaskDefinitionBuilder"/> interface
/// </summary>
public class ForkTaskDefinitionBuilder
: TaskDefinitionBuilder<ForkTaskDefinition>, IForkTaskDefinitionBuilder
: TaskDefinitionBuilder<IForkTaskDefinitionBuilder, ForkTaskDefinition>, IForkTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="CallTaskDefinition"/>s
/// </summary>
public interface ICallTaskDefinitionBuilder
: ITaskDefinitionBuilder<ICallTaskDefinitionBuilder, CallTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="DoTaskDefinition"/>s
/// </summary>
public interface IDoTaskDefinitionBuilder
: ITaskDefinitionBuilder<DoTaskDefinition>
: ITaskDefinitionBuilder<IDoTaskDefinitionBuilder, DoTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="EmitTaskDefinition"/>s
/// </summary>
public interface IEmitTaskDefinitionBuilder
: ITaskDefinitionBuilder<EmitTaskDefinition>
: ITaskDefinitionBuilder<IEmitTaskDefinitionBuilder, EmitTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="ForTaskDefinition"/>s
/// </summary>
public interface IForTaskDefinitionBuilder
: ITaskDefinitionBuilder<ForTaskDefinition>
: ITaskDefinitionBuilder<IForTaskDefinitionBuilder, ForTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="ForkTaskDefinition"/>s
/// </summary>
public interface IForkTaskDefinitionBuilder
: ITaskDefinitionBuilder<ForkTaskDefinition>
: ITaskDefinitionBuilder<IForkTaskDefinitionBuilder, ForkTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="ListenTaskDefinition"/>s
/// </summary>
public interface IListenTaskDefinitionBuilder
: ITaskDefinitionBuilder<ListenTaskDefinition>
: ITaskDefinitionBuilder<IListenTaskDefinitionBuilder, ListenTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="RaiseTaskDefinition"/>s
/// </summary>
public interface IRaiseTaskDefinitionBuilder
: ITaskDefinitionBuilder<IRaiseTaskDefinitionBuilder, RaiseTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="RunTaskDefinition"/>s
/// </summary>
public interface IRunTaskDefinitionBuilder
: ITaskDefinitionBuilder<RunTaskDefinition>
: ITaskDefinitionBuilder<IRunTaskDefinitionBuilder, RunTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="SetTaskDefinition"/>s
/// </summary>
public interface ISetTaskDefinitionBuilder
: ITaskDefinitionBuilder<SetTaskDefinition>
: ITaskDefinitionBuilder<ISetTaskDefinitionBuilder, SetTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="SwitchTaskDefinition"/>s
/// </summary>
public interface ISwitchTaskDefinitionBuilder
: ITaskDefinitionBuilder<ISwitchTaskDefinitionBuilder, SwitchTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,36 @@ public interface ITaskDefinitionBuilder
/// <summary>
/// Defines the fundamentals of a service used to build <see cref="TaskDefinition"/>s
/// </summary>
/// <typeparam name="TDefinition">The type of <see cref="TaskDefinition"/> to build and configure</typeparam>
public interface ITaskDefinitionBuilder<TDefinition>
/// <typeparam name="TBuilder">The type of the implementing <see cref="ITaskDefinitionBuilder{TBuilder}"/></typeparam>
public interface ITaskDefinitionBuilder<TBuilder>
: ITaskDefinitionBuilder
where TBuilder : ITaskDefinitionBuilder<TBuilder>
{

/// <summary>
/// Configures the task to build to run only if the specified condition matches
/// </summary>
/// <param name="condition">A runtime expression that represents the condition to match for the task to run</param>
/// <returns>The configured <see cref="ITaskDefinitionBuilder{TBuilder}"/></returns>
TBuilder If(string condition);

/// <summary>
/// Configures the task to build to then execute the specified flow directive
/// </summary>
/// <param name="directive">The flow directive to then execute</param>
/// <returns>The configured <see cref="ITaskDefinitionBuilder{TBuilder}"/></returns>
TBuilder Then(string directive);

}

/// <summary>
/// Defines the fundamentals of a service used to build <see cref="TaskDefinition"/>s
/// </summary>
/// <typeparam name="TBuilder">The type of the implementing <see cref="ITaskDefinitionBuilder{TBuilder}"/></typeparam>
/// <typeparam name="TDefinition">The type of <see cref="TaskDefinition"/> to build and configure</typeparam>
public interface ITaskDefinitionBuilder<TBuilder, TDefinition>
: ITaskDefinitionBuilder<TBuilder>
where TBuilder : ITaskDefinitionBuilder<TBuilder>
where TDefinition : TaskDefinition
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="TryTaskDefinition"/>s
/// </summary>
public interface ITryTaskDefinitionBuilder
: ITaskDefinitionBuilder<TryTaskDefinition>
: ITaskDefinitionBuilder<ITryTaskDefinitionBuilder, TryTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Defines the fundamentals of a service used to build <see cref="WaitTaskDefinition"/>s
/// </summary>
public interface IWaitTaskDefinitionBuilder
: ITaskDefinitionBuilder<WaitTaskDefinition>
: ITaskDefinitionBuilder<IWaitTaskDefinitionBuilder, WaitTaskDefinition>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Represents the default implementation of the <see cref="IListenTaskDefinitionBuilder"/> interface
/// </summary>
public class ListenTaskDefinitionBuilder
: TaskDefinitionBuilder<ListenTaskDefinition>, IListenTaskDefinitionBuilder
: TaskDefinitionBuilder<IListenTaskDefinitionBuilder, ListenTaskDefinition>, IListenTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// </summary>
/// <param name="errorDefinition">The error to raise</param>
public class RaiseTaskDefinitionBuilder(ErrorDefinition? errorDefinition = null)
: TaskDefinitionBuilder<RaiseTaskDefinition>, IRaiseTaskDefinitionBuilder
: TaskDefinitionBuilder<IRaiseTaskDefinitionBuilder, RaiseTaskDefinition>, IRaiseTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Represents the default implementation of the <see cref="IRunTaskDefinitionBuilder"/> interface
/// </summary>
public class RunTaskDefinitionBuilder
: TaskDefinitionBuilder<RunTaskDefinition>, IRunTaskDefinitionBuilder
: TaskDefinitionBuilder<IRunTaskDefinitionBuilder, RunTaskDefinition>, IRunTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// </summary>
/// <param name="variables">A name/value mapping of the variables to set</param>
public class SetTaskDefinitionBuilder(IDictionary<string, object>? variables = null)
: TaskDefinitionBuilder<SetTaskDefinition>,
ISetTaskDefinitionBuilder
: TaskDefinitionBuilder<ISetTaskDefinitionBuilder, SetTaskDefinition>, ISetTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Represents the default implementation of the <see cref="ISwitchTaskDefinitionBuilder"/> interface
/// </summary>
public class SwitchTaskDefinitionBuilder
: TaskDefinitionBuilder<SwitchTaskDefinition>, ISwitchTaskDefinitionBuilder
: TaskDefinitionBuilder<ISwitchTaskDefinitionBuilder, SwitchTaskDefinition>, ISwitchTaskDefinitionBuilder
{

/// <summary>
Expand Down
35 changes: 32 additions & 3 deletions src/ServerlessWorkflow.Sdk.Builders/TaskDefinitionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,49 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// <summary>
/// Represents the base class for all <see cref="ITaskDefinitionBuilder{TDefinition}"/> implementations
/// </summary>
/// <typeparam name="TBuilder">The type of the implementing <see cref="ITaskDefinitionBuilder{TBuilder}"/></typeparam>
/// <typeparam name="TDefinition">The type of <see cref="TaskDefinition"/> to build</typeparam>
public abstract class TaskDefinitionBuilder<TDefinition>
: ITaskDefinitionBuilder<TDefinition>
public abstract class TaskDefinitionBuilder<TBuilder, TDefinition>
: ITaskDefinitionBuilder<TBuilder, TDefinition>
where TBuilder : ITaskDefinitionBuilder<TBuilder>
where TDefinition : TaskDefinition
{

/// <summary>
/// Gets/sets the runtime expression, if any, used to determine whether or not to run the task to build
/// </summary>
protected string? IfExpression { get; set; }

/// <summary>
/// Gets/sets the flow directive, if any, used to then execute
/// </summary>
protected string? ThenDirective { get; set; }

/// <inheritdoc/>
public virtual TBuilder If(string condition)
{
ArgumentException.ThrowIfNullOrWhiteSpace(condition);
this.IfExpression = condition;
return (TBuilder)(object)this;
}

/// <inheritdoc/>
public virtual TBuilder Then(string directive)
{
ArgumentException.ThrowIfNullOrWhiteSpace(directive);
this.ThenDirective = directive;
return (TBuilder)(object)this;
}

/// <summary>
/// Applies the configuration common to all types of tasks
/// </summary>
/// <param name="definition">The task definition to configure</param>
/// <returns>The configured task definition</returns>
protected virtual TDefinition Configure(TDefinition definition)
{
//todo: common properties (timeouts, etc)
definition.If = this.IfExpression;
definition.Then = this.ThenDirective;
return definition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// Represents the default implementation of the <see cref="ITryTaskDefinitionBuilder"/> interface
/// </summary>
public class TryTaskDefinitionBuilder
: TaskDefinitionBuilder<TryTaskDefinition>, ITryTaskDefinitionBuilder
: TaskDefinitionBuilder<ITryTaskDefinitionBuilder, TryTaskDefinition>, ITryTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ServerlessWorkflow.Sdk.Builders;
/// </summary>
/// <param name="duration">The amount of time to wait for</param>
public class WaitTaskDefinitionBuilder(Duration? duration = null)
: TaskDefinitionBuilder<WaitTaskDefinition>, IWaitTaskDefinitionBuilder
: TaskDefinitionBuilder<IWaitTaskDefinitionBuilder, WaitTaskDefinition>, IWaitTaskDefinitionBuilder
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public static class WorkflowDefinitionExtensions
/// <param name="path">The name or path to the task to reference</param>
/// <param name="parentReference">A reference to the <see cref="TaskDefinition"/>'s parent, if any</param>
/// <returns>A new <see cref="Uri"/> used to reference the <see cref="TaskDefinition"/></returns>
public static Uri BuildReferenceTo(this WorkflowDefinition workflow, TaskDefinition task, string path, Uri? parentReference = null)
public static Uri BuildReferenceTo(this WorkflowDefinition workflow, TaskDefinition task, string? path, Uri? parentReference = null)
{
ArgumentNullException.ThrowIfNull(workflow);
ArgumentNullException.ThrowIfNull(task);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (string.IsNullOrWhiteSpace(path)) return parentReference ?? throw new ArgumentNullException(nameof(parentReference), "The parent must be set when the path to the task to execute is null (in case the task is a function)");
return parentReference == null
? new Uri($"/{nameof(WorkflowDefinition.Do).ToCamelCase()}/{path}", UriKind.Relative)
? new Uri($"/{nameof(WorkflowDefinition.Do).ToCamelCase()}/{workflow.Do.Keys.ToList().IndexOf(path)}/{path}", UriKind.Relative)
: new Uri($"{parentReference.OriginalString}/{path}", UriKind.Relative);
}

Expand Down
6 changes: 6 additions & 0 deletions src/ServerlessWorkflow.Sdk/Models/TaskDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public abstract record TaskDefinition
[IgnoreDataMember, JsonIgnore, YamlIgnore]
public abstract string Type { get; }

/// <summary>
/// Gets/sets a runtime expression, if any, used to determine whether or not the execute the task in the current context
/// </summary>
[DataMember(Name = "if", Order = 0), JsonPropertyName("if"), JsonPropertyOrder(0), YamlMember(Alias = "if", Order = 0)]
public virtual string? If { get; set; }

/// <summary>
/// Gets/sets the definition, if any, of the task's input data
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void Build_Should_Work()
.UseSecret("fake-secret")
.Do("todo-1", task => task
.Call("http")
.If("fake-condition")
.With("method", "get")
.With("uri", "https://unit-tests.serverlessworkflow.io"))
.Do("todo-2", task => task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ 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 @@ -71,6 +71,7 @@ internal static WorkflowDefinition Create()
.UseSecret("fake-secret")
.Do("todo-1", task => task
.Call("http")
.If("fake-condition")
.With("method", "get")
.With("uri", "https://unit-tests.serverlessworkflow.io"))
.Do("todo-2", task => task
Expand Down

0 comments on commit f61dade

Please sign in to comment.