elsa-core/src/activities/Elsa.Activities.ControlFlow/Activities/Switch.cs
Sipke Schoorstra aef8741d84
Add Liquid expressions (#111)
* Reorganize projects

* Implement Liquid expression evaluator

* Refactor activity references to use `Name`
2019-10-27 15:38:37 +01:00

52 lines
1.9 KiB
C#

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Attributes;
using Elsa.Expressions;
using Elsa.Extensions;
using Elsa.Results;
using Elsa.Services;
using Elsa.Services.Models;
namespace Elsa.Activities.ControlFlow.Activities
{
[ActivityDefinition(
Category = "Control Flow",
Description = "Switch execution based on a given expression.",
Icon = "far fa-list-alt",
RuntimeDescription = "x => !!x.state.expression ? `Switch execution based on <strong>${ x.state.expression.expression }</strong>.` : x.definition.description",
Outcomes = "x => x.state.cases.map(c => c.toString())"
)]
public class Switch : Activity
{
private readonly IWorkflowExpressionEvaluator expressionEvaluator;
public Switch(IWorkflowExpressionEvaluator expressionEvaluator)
{
this.expressionEvaluator = expressionEvaluator;
Cases = new List<string>();
}
[ActivityProperty(Hint = "The expression to evaluate. The evaluated value will be used to switch on.")]
public WorkflowExpression<string> Expression
{
get => GetState<WorkflowExpression<string>>();
set => SetState(value);
}
[ActivityProperty(Hint = "A comma-separated list of possible outcomes of the expression.")]
public IReadOnlyCollection<string> Cases
{
get => GetState<IReadOnlyCollection<string>>();
set => SetState(value);
}
protected override async Task<ActivityExecutionResult> OnExecuteAsync(
WorkflowExecutionContext workflowContext,
CancellationToken cancellationToken)
{
var result = await expressionEvaluator.EvaluateAsync(Expression, workflowContext, cancellationToken);
return Outcome(result);
}
}
}