Skip to content

Commit 186404b

Browse files
committed
fix(Runner): Fixed the SwitchTaskExecutor to not throw when multiple cases match, and to select the first matching case, if any
Signed-off-by: Charles d'Avernas <[email protected]>
1 parent 57eb3c2 commit 186404b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/runner/Synapse.Runner/Services/Executors/SwitchTaskExecutor.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ public class SwitchTaskExecutor(IServiceProvider serviceProvider, ILogger<Switch
3232
/// <inheritdoc/>
3333
protected override async Task DoExecuteAsync(CancellationToken cancellationToken)
3434
{
35-
var matches = new List<MapEntry<string, SwitchCaseDefinition>>();
35+
MapEntry<string, SwitchCaseDefinition>? match = null;
3636
var defaultCase = this.Task.Definition.Switch.FirstOrDefault(kvp => string.IsNullOrWhiteSpace(kvp.Value.When));
3737
foreach (var @case in this.Task.Definition.Switch!.Where(c => !string.IsNullOrWhiteSpace(c.Value.When)))
3838
{
39-
if (await this.Task.Workflow.Expressions.EvaluateConditionAsync(@case.Value.When!, this.Task.Input, this.GetExpressionEvaluationArguments(), cancellationToken).ConfigureAwait(false)) matches.Add(@case);
39+
if (!await this.Task.Workflow.Expressions.EvaluateConditionAsync(@case.Value.When!, this.Task.Input, this.GetExpressionEvaluationArguments(), cancellationToken).ConfigureAwait(false)) continue;
40+
match = @case;
41+
break;
4042
}
41-
if (matches.Count == 1) await this.SetResultAsync(this.Task.Input, matches.First().Value.Then, cancellationToken).ConfigureAwait(false);
42-
else if (matches.Count > 1) await this.SetErrorAsync(Error.Configuration(this.Task.Instance.Reference, $"At most one matching case is allowed, but cases {string.Join(", ", matches.Select(m => m.Key))} have been matched."), cancellationToken).ConfigureAwait(false);
43+
if (match != null) await this.SetResultAsync(this.Task.Input, match.Value.Then, cancellationToken).ConfigureAwait(false);
4344
else if (defaultCase != null) await this.SetResultAsync(this.Task.Input, defaultCase.Value.Then, cancellationToken).ConfigureAwait(false);
4445
else await this.SetResultAsync(this.Task.Input, this.Task.Definition.Then, cancellationToken).ConfigureAwait(false);
4546
}

0 commit comments

Comments
 (0)