Various API enhancements

This commit is contained in:
Sipke Schoorstra 2022-03-09 20:21:30 +01:00
parent 63ffacbb71
commit f9a7ee45f1
8 changed files with 75 additions and 6 deletions

View file

@ -14,6 +14,17 @@ public enum ForOperator
public class For : Activity
{
public For()
{
}
public For(int start, int end, ForOperator forOperator = ForOperator.LessThanOrEqual)
{
Start = new Input<int>(start);
End = new Input<int>(end);
Operator = new Input<ForOperator>(forOperator);
}
public Input<int> Start { get; set; } = new(0);
public Input<int> End { get; set; } = new(0);
public Input<int> Step { get; set; } = new(1);

View file

@ -0,0 +1,36 @@
using Elsa.Contracts;
using Elsa.Models;
namespace Elsa.Expressions;
public class OutputExpression : IExpression
{
public OutputExpression(Output? output)
{
Output = output;
}
public OutputExpression()
{
}
public Output? Output { get; set; }
}
public class OutputExpression<T> : OutputExpression
{
public OutputExpression(Output<T>? output) : base(output)
{
}
}
public class OutputExpressionHandler : IExpressionHandler
{
public ValueTask<object?> EvaluateAsync(IExpression expression, Type returnType, ExpressionExecutionContext context)
{
var outputExpression = (OutputExpression)expression;
var output = outputExpression.Output;
var value = context.Get(output);
return ValueTask.FromResult(value);
}
}

View file

@ -33,9 +33,12 @@ public static class ActivityExtensions
return query.Select(x => x!).ToList();
}
/// <summary>
/// Creates an input from the activity's result.
/// </summary>
public static Input<T> CreateInput<T>(this Activity<T> activity) => activity.Result.CreateInput();
public static IEnumerable<Variable> GetVariables(this IActivity activity)
{
var properties = activity.GetType().GetProperties();

View file

@ -0,0 +1,11 @@
using Elsa.Models;
namespace Elsa.Extensions;
public static class OutputExtensions
{
/// <summary>
/// Creates an input that references the specified output's value.
/// </summary>
public static Input<T> CreateInput<T>(this Output<T>? output) => new(output);
}

View file

@ -38,4 +38,5 @@ public abstract class ActivityWithResult : Activity
public abstract class Activity<T> : ActivityWithResult
{
public new Output<T>? Result { get; set; }
}

View file

@ -26,6 +26,8 @@ public class ExpressionExecutionContext
public object Get(RegisterLocationReference locationReference) => GetLocation(locationReference).Value!;
public T Get<T>(RegisterLocationReference locationReference) => (T)Get(locationReference);
public T? Get<T>(Input<T>? input) => input != null ? (T?)GetLocation(input.LocationReference).Value : default;
public T? Get<T>(Output<T>? output) => output != null ? (T?)GetLocation(output.LocationReference).Value : default;
public object? Get(Output? output) => output != null ? GetLocation(output.LocationReference).Value : default;
public T? GetVariable<T>(string name) => (T?)GetVariable(name);
public T? GetVariable<T>() => (T?)GetVariable(typeof(T).Name);

View file

@ -40,6 +40,14 @@ public class Input<T> : Input
public Input(Variable<T> variable) : base(new VariableExpression(variable), variable, typeof(T))
{
}
public Input(Output<T>? output) : base(new OutputExpression<T>(output), output?.LocationReference ?? new Literal(), typeof(T))
{
}
public Input(Output? output) : base(new OutputExpression(output), output?.LocationReference ?? new Literal(), typeof(T))
{
}
public Input(Literal<T> literal) : base(new LiteralExpression(literal.Value), literal, typeof(T))
{

View file

@ -1,3 +0,0 @@
namespace Elsa.Models;
//public record NodeIdentity(ActivityNode ActivityNode, string NodeName);