< Summary

Information
Class: Elsa.Extensions.MemoryBlockReferenceExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/MemoryBlockReferenceExtensions.cs
Line coverage
92%
Covered lines: 13
Uncovered lines: 1
Coverable lines: 14
Total lines: 83
Line coverage: 92.8%
Branch coverage
71%
Covered branches: 10
Total branches: 14
Branch coverage: 71.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Get(...)75%44100%
Get(...)100%11100%
Get(...)50%22100%
GetBlock(...)100%22100%
Get(...)100%210%
Set(...)66.66%66100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/MemoryBlockReferenceExtensions.cs

#LineLine coverage
 1using Elsa.Expressions.Helpers;
 2using Elsa.Expressions.Models;
 3using Elsa.Workflows;
 4
 5// ReSharper disable once CheckNamespace
 6namespace Elsa.Extensions;
 7
 8/// <summary>
 9/// Provides extension methods for <see cref="MemoryBlockReference"/>.
 10/// </summary>
 11public 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    {
 7321        var matchingContext = context.FindContextContainingBlock(blockId) ?? context;
 7322        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>
 7331    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        {
 143            var matchingContext = context.ExpressionExecutionContext.FindContextContainingBlock(reference.Id) ?? context
 144            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        {
 12454            var matchingContext = context.FindContextContainingBlock(reference.Id) ?? context;
 12455            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"/>.</
 064        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        {
 2275            var matchingContext =
 2276                context.ExpressionExecutionContext.FindContextContainingBlock(reference.Id)
 2277                ?? context.FindParentWithVariableContainer()?.ExpressionExecutionContext
 2278                ?? context.WorkflowExecutionContext.ExpressionExecutionContext!;
 79
 2280            reference.Set(matchingContext, value);
 2281        }
 82    }
 83}