-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for collections and dictionaries of input
- Loading branch information
Bob Hauser
committed
Dec 16, 2024
1 parent
5d92c57
commit a7a1b8b
Showing
10 changed files
with
323 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../Elsa.Workflows.IntegrationTests/Activities/CollectionsOfInput/CollectionsOfInputTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Elsa.Testing.Shared; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit.Abstractions; | ||
|
||
namespace Elsa.Workflows.IntegrationTests.Activities.CollectionInputs; | ||
public class CollectionsOfInputTests | ||
{ | ||
private readonly IWorkflowRunner _workflowRunner; | ||
private readonly CapturingTextWriter _capturingTextWriter = new(); | ||
private readonly IServiceProvider _services; | ||
|
||
public CollectionsOfInputTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
_services = new TestApplicationBuilder(testOutputHelper).WithCapturingTextWriter(_capturingTextWriter).Build(); | ||
_workflowRunner = _services.GetRequiredService<IWorkflowRunner>(); | ||
} | ||
|
||
[Fact] | ||
public async Task CollectionOfInputsTest() | ||
{ | ||
await _services.PopulateRegistriesAsync(); | ||
await _workflowRunner.RunAsync<WriteMultiLineWorkflow>(); | ||
var lines = _capturingTextWriter.Lines.ToArray(); | ||
Assert.Equal(new[] { "banana", "orange", "apple" }, lines); | ||
} | ||
|
||
[Fact] | ||
public async Task DictionaryOfInputValuesTest() | ||
{ | ||
await _services.PopulateRegistriesAsync(); | ||
await _workflowRunner.RunAsync<DynamicArgumentsWorkflow>(); | ||
var lines = _capturingTextWriter.Lines.ToArray(); | ||
Assert.Equal(new[] { "name: Frank (string)", "isAdmin: False (bool)", "age: 42 (double)" }, lines); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...gration/Elsa.Workflows.IntegrationTests/Activities/CollectionsOfInput/DynamicArguments.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Runtime.CompilerServices; | ||
using Elsa.Workflows.Models; | ||
|
||
namespace Elsa.Workflows.IntegrationTests.Activities.CollectionInputs; | ||
|
||
public class DynamicArguments : CodeActivity | ||
{ | ||
public DynamicArguments([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line) { } | ||
|
||
public DynamicArguments(Dictionary<string, Input<object>> arguments, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(source, line) => Arguments = arguments; | ||
|
||
public Dictionary<string, Input<object>>? Arguments { get; set; } = default; | ||
|
||
protected override void Execute(ActivityExecutionContext context) | ||
{ | ||
var provider = context.GetService<IStandardOutStreamProvider>() ?? new StandardOutStreamProvider(Console.Out); | ||
var textWriter = provider.GetTextWriter(); | ||
|
||
if (Arguments != null) | ||
{ | ||
foreach (var argument in Arguments) | ||
{ | ||
var name = argument.Key; | ||
string value = context.Get(argument.Value) switch | ||
{ | ||
bool boolValue => $"{boolValue} (bool)", | ||
string stringValue => $"{stringValue} (string)", | ||
double doubleValue => $"{doubleValue} (double)", | ||
_ => throw new NotImplementedException(), | ||
}; | ||
|
||
textWriter.WriteLine($"{name}: {value}"); | ||
} | ||
} | ||
} | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
...Elsa.Workflows.IntegrationTests/Activities/CollectionsOfInput/DynamicArgumentsWorkflow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Elsa.Expressions.Models; | ||
using Elsa.Workflows.Activities; | ||
using Elsa.Workflows.Memory; | ||
using Elsa.Workflows.Models; | ||
|
||
namespace Elsa.Workflows.IntegrationTests.Activities.CollectionInputs; | ||
class DynamicArgumentsWorkflow : WorkflowBase | ||
{ | ||
protected override void Build(IWorkflowBuilder workflow) | ||
{ | ||
var nameVariable = new Variable<string>("name", "Frank"); | ||
|
||
workflow.Root = new Sequence | ||
{ | ||
Variables = { nameVariable }, | ||
Activities = | ||
{ | ||
new DynamicArguments(new Dictionary<string, Input<object>>() | ||
{ | ||
{ "name", new Input<object>(new Expression("JavaScript", "getVariable('name')")) }, | ||
{ "isAdmin", new Input<object>(false) }, | ||
{ "age", new Input<object>(new Expression("JavaScript", "let age = 30 + 12; age;")) } | ||
}) | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.