| | | 1 | | using Elsa.Expressions.Helpers; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Workflows; |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides extension methods for <see cref="MemoryBlockReference"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class MemoryBlockReferenceExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets the value of the memory reference referenced by the specified <see cref="MemoryBlockReference"/>. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="context">The <see cref="ExpressionExecutionContext"/> to get the value from.</param> |
| | | 17 | | /// <param name="blockId">The ID of the memory block to get the value for.</param> |
| | | 18 | | /// <returns>The value of the memory reference referenced by the specified block ID.</returns> |
| | | 19 | | public static object? Get(this ExpressionExecutionContext context, string blockId) |
| | | 20 | | { |
| | 73 | 21 | | var matchingContext = context.FindContextContainingBlock(blockId) ?? context; |
| | 73 | 22 | | return matchingContext.Memory.TryGetBlock(blockId, out var block) ? block.Value : default; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the value of the memory reference referenced by the specified <see cref="MemoryBlockReference"/>. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="context">The <see cref="ActivityExecutionContext"/> to get the value from.</param> |
| | | 29 | | /// <param name="blockId">The ID of the memory block to get the value for.</param> |
| | | 30 | | /// <returns>The value of the memory reference referenced by the specified block ID.</returns> |
| | 73 | 31 | | public static object? Get(this ActivityExecutionContext context, string blockId) => context.ExpressionExecutionConte |
| | | 32 | | |
| | | 33 | | /// <param name="reference">The <see cref="MemoryBlockReference"/> to get the value for.</param> |
| | | 34 | | extension(MemoryBlockReference reference) |
| | | 35 | | { |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the value of the memory reference referenced by the specified <see cref="MemoryBlockReference"/>. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="context">The <see cref="ActivityExecutionContext"/> to get the value from.</param> |
| | | 40 | | /// <returns>The value of the memory reference referenced by the specified <see cref="MemoryBlockReference"/>.</ |
| | | 41 | | public object? Get(ActivityExecutionContext context) |
| | | 42 | | { |
| | 1 | 43 | | var matchingContext = context.ExpressionExecutionContext.FindContextContainingBlock(reference.Id) ?? context |
| | 1 | 44 | | return reference.Get(matchingContext); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the <see cref="MemoryBlock"/> of referenced by the specified <see cref="MemoryBlockReference"/>. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="context">The <see cref="ExpressionExecutionContext"/> to get the reference from.</param> |
| | | 51 | | /// <returns>The <see cref="MemoryBlock"/> referenced by the specified <see cref="MemoryBlockReference"/>.</retu |
| | | 52 | | public MemoryBlock GetBlock(ExpressionExecutionContext context) |
| | | 53 | | { |
| | 124 | 54 | | var matchingContext = context.FindContextContainingBlock(reference.Id) ?? context; |
| | 124 | 55 | | return reference.GetBlock(matchingContext.Memory); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets the value of the memory reference referenced by the specified <see cref="MemoryBlockReference"/>. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="context">The <see cref="ActivityExecutionContext"/> to get the value from.</param> |
| | | 62 | | /// <typeparam name="T">The type of the value to get.</typeparam> |
| | | 63 | | /// <returns>The value of the memory reference referenced by the specified <see cref="MemoryBlockReference"/>.</ |
| | 0 | 64 | | public T? Get<T>(ActivityExecutionContext context) => reference.Get(context).ConvertTo<T>(); |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Sets the specified value on the memory referenced by the specified <see cref="MemoryBlockReference"/>. |
| | | 68 | | /// If the referenced block doesn't exist in the current scope, an attempt is made to find the block in the firs |
| | | 69 | | /// If that fails, the root scope is used. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="context">The <see cref="ActivityExecutionContext"/> to set the value on.</param> |
| | | 72 | | /// <param name="value">The value to set.</param> |
| | | 73 | | public void Set(ActivityExecutionContext context, object? value) |
| | | 74 | | { |
| | 22 | 75 | | var matchingContext = |
| | 22 | 76 | | context.ExpressionExecutionContext.FindContextContainingBlock(reference.Id) |
| | 22 | 77 | | ?? context.FindParentWithVariableContainer()?.ExpressionExecutionContext |
| | 22 | 78 | | ?? context.WorkflowExecutionContext.ExpressionExecutionContext!; |
| | | 79 | | |
| | 22 | 80 | | reference.Set(matchingContext, value); |
| | 22 | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |