46 lines
1.7 KiB
C#
46 lines
1.7 KiB
C#
using System.Text.Json.Nodes;
|
|
using w4c_workflows.Models.Nodes;
|
|
|
|
namespace w4c_workflows.Services.Nodes.Executors;
|
|
|
|
/// <summary>
|
|
/// Conditional branch node. Evaluates the configured comparison once per item
|
|
/// and routes the item to output 0 (true) or output 1 (false). The comparison
|
|
/// itself lives in <see cref="NodeConditionEvaluator"/> so every branch node
|
|
/// (if / filter / switch) behaves identically.
|
|
/// </summary>
|
|
public sealed class IfNodeExecutor : INodeExecutor
|
|
{
|
|
public string Type => "core.if";
|
|
|
|
public Task<NodeExecutionOutcome> RunAsync(NodeExecutionContext context, CancellationToken ct)
|
|
{
|
|
var op = context.Parameters["operator"] is JsonValue value && value.TryGetValue<string>(out var text)
|
|
? text
|
|
: NodeConditionEvaluator.DefaultOperator;
|
|
var caseSensitive = context.Parameters["caseSensitive"] is JsonValue flag && flag.TryGetValue<bool>(out var cs) && cs;
|
|
var left = context.Parameters["left"];
|
|
var right = context.Parameters["right"];
|
|
|
|
bool matched;
|
|
try
|
|
{
|
|
matched = NodeConditionEvaluator.Evaluate(left, op, right, caseSensitive);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
return Task.FromResult(NodeExecutionOutcome.Failed(ex.Message, "invalid_condition"));
|
|
}
|
|
|
|
var trueItems = new List<FlowItem>();
|
|
var falseItems = new List<FlowItem>();
|
|
foreach (var item in context.Input(0))
|
|
(matched ? trueItems : falseItems).Add(item);
|
|
|
|
return Task.FromResult(new NodeExecutionOutcome
|
|
{
|
|
Outputs = new List<IReadOnlyList<FlowItem>> { trueItems, falseItems },
|
|
});
|
|
}
|
|
}
|