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