Add fluent methods for input handling in workflow builder

Updated the WorkflowBuilder and its interface, IWorkflowBuilder, to include a series of fluent methods for handling inputs. These methods allow easier set up and addition of input definitions to workflows. An extension method to get input names was also added to the ExpressionExecutionContextExtensions.
This commit is contained in:
Sipke Schoorstra 2024-02-05 21:38:36 +01:00
parent ef1d736184
commit d511b04cee
3 changed files with 91 additions and 1 deletions

View file

@ -112,7 +112,60 @@ public class WorkflowBuilder : IWorkflowBuilder
/// <inheritdoc />
public IWorkflowBuilder WithVariables(params Variable[] variables)
{
foreach (var variable in variables) Variables.Add(variable);
foreach (var variable in variables)
Variables.Add(variable);
return this;
}
/// <inheritdoc />
public InputDefinition WithInput<T>(string name, string? description = default)
{
return WithInput(inputDefinition =>
{
inputDefinition.Name = name;
if (description != null)
inputDefinition.Description = description;
});
}
/// <inheritdoc />
public InputDefinition WithInput(string name, Type type, string? description = default)
{
return WithInput(inputDefinition =>
{
inputDefinition.Name = name;
inputDefinition.Type = type;
if (description != null)
inputDefinition.Description = description;
});
}
/// <inheritdoc />
public InputDefinition WithInput(string name, Type type, Action<InputDefinition>? setup = default)
{
return WithInput(inputDefinition =>
{
inputDefinition.Name = name;
inputDefinition.Type = type;
setup?.Invoke(inputDefinition);
});
}
/// <inheritdoc />
public InputDefinition WithInput(Action<InputDefinition> setup)
{
var inputDefinition = new InputDefinition();
setup(inputDefinition);
Inputs.Add(inputDefinition);
return inputDefinition;
}
/// <inheritdoc />
public IWorkflowBuilder WithInput(InputDefinition inputDefinition)
{
Inputs.Add(inputDefinition);
return this;
}

View file

@ -109,6 +109,31 @@ public interface IWorkflowBuilder
/// A fluent method for adding a variable to <see cref="Variables"/>.
/// </summary>
IWorkflowBuilder WithVariables(params Variable[] variables);
/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
InputDefinition WithInput<T>(string name, string? description = default);
/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
InputDefinition WithInput(string name, Type type, string? description = default);
/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
InputDefinition WithInput(string name, Type type, Action<InputDefinition>? setup = default);
/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
InputDefinition WithInput(Action<InputDefinition> setup);
/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
IWorkflowBuilder WithInput(InputDefinition inputDefinition);
/// <summary>
/// A fluent method for adding a property to <see cref="CustomProperties"/>.

View file

@ -338,6 +338,18 @@ public static class ExpressionExecutionContextExtensions
}
}
/// <summary>
/// Returns the input value associated with the specified <see cref="InputDefinition"/> in the given <see cref="ExpressionExecutionContext"/>.
/// </summary>
/// <typeparam name="T">The type of the input value.</typeparam>
/// <param name="context">The <see cref="ExpressionExecutionContext"/> containing the input.</param>
/// <param name="inputDefinition">The <see cref="InputDefinition"/> specifying the input to retrieve.</param>
/// <returns>The input value associated with the specified <see cref="InputDefinition"/> in the <see cref="ExpressionExecutionContext"/>.</returns>
public static T? GetInput<T>(this ExpressionExecutionContext context, InputDefinition inputDefinition)
{
return context.GetInput<T>(inputDefinition.Name);
}
/// <summary>
/// Returns the value of the specified input.
/// </summary>