| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Helpers; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Memory; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | |
| | | 8 | | // ReSharper disable once CheckNamespace |
| | | 9 | | namespace Elsa.Extensions; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides a set of helper extensions to <see cref="Output{T}"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class OutputExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates an input that references the specified output's value. |
| | | 18 | | /// </summary> |
| | 0 | 19 | | public static Input<T> CreateInput<T>(this Output output) => new(output); |
| | | 20 | | |
| | | 21 | | extension<T>(Output<T>? output) |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Sets the output to the specified value. |
| | | 25 | | /// </summary> |
| | 216 | 26 | | public void Set(ActivityExecutionContext context, T? value, [CallerArgumentExpression("output")] string? outputN |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Sets the output to the specified value. |
| | | 30 | | /// </summary> |
| | 0 | 31 | | public void Set(ExpressionExecutionContext context, T? value) => context.Set(output, value); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Sets the output to the specified value. |
| | | 35 | | /// </summary> |
| | 0 | 36 | | public void Set(ActivityExecutionContext context, Variable<T> value) => context.Set(output, value.Get(context)); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Sets the output to the specified value. |
| | | 40 | | /// </summary> |
| | 0 | 41 | | public void Set(ExpressionExecutionContext context, Variable<T> value) => context.Set(output, value.Get(context) |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | extension(Output? output) |
| | | 45 | | { |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the target type of the specified variable type, if any, linked to the output. |
| | | 48 | | /// </summary> |
| | | 49 | | public Type? GetTargetType(ActivityExecutionContext context) |
| | | 50 | | { |
| | 206 | 51 | | var memoryBlockReference = output?.MemoryBlockReference(); |
| | | 52 | | |
| | 206 | 53 | | if (memoryBlockReference is null) |
| | 206 | 54 | | return null; |
| | | 55 | | |
| | 0 | 56 | | if(!context.ExpressionExecutionContext.TryGetBlock(memoryBlockReference, out var memoryBlock)) |
| | 0 | 57 | | return null; |
| | | 58 | | |
| | 0 | 59 | | var parsedContentVariableType = (memoryBlock.Metadata as VariableBlockMetadata)?.Variable.GetType(); |
| | 0 | 60 | | return parsedContentVariableType?.GenericTypeArguments.FirstOrDefault(); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Returns a value indicating whether the output has a target. |
| | | 65 | | /// </summary> |
| | | 66 | | public bool HasTarget(ActivityExecutionContext context) |
| | | 67 | | { |
| | 28 | 68 | | var memoryBlockReference = output?.MemoryBlockReference(); |
| | 28 | 69 | | return memoryBlockReference is not null && context.ExpressionExecutionContext.TryGetBlock(memoryBlockReferen |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public static object? ParseValue(this Output output, object? value) |
| | | 74 | | { |
| | 31 | 75 | | var genericType = output.GetType(); |
| | 31 | 76 | | return VariableExtensions.ParseValue(genericType, value); |
| | | 77 | | } |
| | | 78 | | } |