From 186404bdf01268f982143e344755fce753ab3d1c Mon Sep 17 00:00:00 2001 From: Charles d'Avernas Date: Mon, 21 Oct 2024 13:30:05 +0200 Subject: [PATCH] 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 --- .../Services/Executors/SwitchTaskExecutor.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/runner/Synapse.Runner/Services/Executors/SwitchTaskExecutor.cs b/src/runner/Synapse.Runner/Services/Executors/SwitchTaskExecutor.cs index 17f87fead..e39e8bd75 100644 --- a/src/runner/Synapse.Runner/Services/Executors/SwitchTaskExecutor.cs +++ b/src/runner/Synapse.Runner/Services/Executors/SwitchTaskExecutor.cs @@ -32,14 +32,15 @@ public class SwitchTaskExecutor(IServiceProvider serviceProvider, ILogger protected override async Task DoExecuteAsync(CancellationToken cancellationToken) { - var matches = new List>(); + MapEntry? match = null; var defaultCase = this.Task.Definition.Switch.FirstOrDefault(kvp => string.IsNullOrWhiteSpace(kvp.Value.When)); foreach (var @case in this.Task.Definition.Switch!.Where(c => !string.IsNullOrWhiteSpace(c.Value.When))) { - if (await this.Task.Workflow.Expressions.EvaluateConditionAsync(@case.Value.When!, this.Task.Input, this.GetExpressionEvaluationArguments(), cancellationToken).ConfigureAwait(false)) matches.Add(@case); + if (!await this.Task.Workflow.Expressions.EvaluateConditionAsync(@case.Value.When!, this.Task.Input, this.GetExpressionEvaluationArguments(), cancellationToken).ConfigureAwait(false)) continue; + match = @case; + break; } - if (matches.Count == 1) await this.SetResultAsync(this.Task.Input, matches.First().Value.Then, cancellationToken).ConfigureAwait(false); - 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); + if (match != null) await this.SetResultAsync(this.Task.Input, match.Value.Then, cancellationToken).ConfigureAwait(false); else if (defaultCase != null) await this.SetResultAsync(this.Task.Input, defaultCase.Value.Then, cancellationToken).ConfigureAwait(false); else await this.SetResultAsync(this.Task.Input, this.Task.Definition.Then, cancellationToken).ConfigureAwait(false); }