| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Workflows; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | |
| | | 6 | | // ReSharper disable once CheckNamespace |
| | | 7 | | namespace Elsa.Extensions; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides extensions on <see cref="ExpressionExecutionContext"/> and <see cref="ActivityExecutionContext"/> |
| | | 11 | | /// </summary> |
| | | 12 | | public static class InputExtensions |
| | | 13 | | { |
| | | 14 | | /// <param name="input">The input.</param> |
| | | 15 | | /// <typeparam name="T">The type of the input.</typeparam> |
| | | 16 | | extension<T>(Input<T>? input) |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Returns the value of the specified input, or a default value if the input is not found. |
| | | 20 | | /// </summary> |
| | | 21 | | public T? GetOrDefault(ActivityExecutionContext context, Func<T>? defaultValue = default) |
| | | 22 | | { |
| | 1174 | 23 | | var value = context.Get(input); |
| | 1174 | 24 | | return value != null ? value : defaultValue != null ? defaultValue.Invoke() : default; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Returns the value of the specified input, or a default value if the input is not found. |
| | | 29 | | /// </summary> |
| | | 30 | | public T? GetOrDefault(ExpressionExecutionContext context, Func<T>? defaultValue = default) |
| | | 31 | | { |
| | 235 | 32 | | var value = context.Get(input); |
| | 235 | 33 | | return value != null ? value : defaultValue != null ? defaultValue.Invoke() : default; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Returns the value of the specified input. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="context">The context.</param> |
| | | 40 | | /// <param name="inputName">The name of the input.</param> |
| | | 41 | | /// <returns>The value of the specified input.</returns> |
| | | 42 | | /// <exception cref="Exception">Throws an exception if the input is not found.</exception> |
| | | 43 | | public T Get(ActivityExecutionContext context, [CallerArgumentExpression("input")] string? inputName = default) |
| | | 44 | | { |
| | 361 | 45 | | return context.Get(input) ?? throw new Exception($"{inputName} is required."); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Returns the value of the specified input. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="context">The context.</param> |
| | | 52 | | /// <param name="inputName">The name of the input.</param> |
| | | 53 | | /// <returns>The value of the specified input.</returns> |
| | | 54 | | /// <exception cref="Exception">Throws an exception if the input is not found.</exception> |
| | | 55 | | public T Get(ExpressionExecutionContext context, [CallerArgumentExpression("input")] string? inputName = default |
| | | 56 | | { |
| | 21 | 57 | | return context.Get(input) ?? throw new Exception($"{inputName} is required."); |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | } |