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); }