< Summary

Information
Class: Elsa.Workflows.WorkflowInstanceStorageDriver
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/VariableStorageDrivers/WorkflowInstanceStorageDriver.cs
Line coverage
82%
Covered lines: 32
Uncovered lines: 7
Coverable lines: 39
Total lines: 84
Line coverage: 82%
Branch coverage
16%
Covered branches: 1
Total branches: 6
Branch coverage: 16.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Priority()100%210%
get_Tags()100%210%
WriteAsync(...)0%4468.75%
ReadAsync(...)50%22100%
DeleteAsync(...)100%11100%
GetVariablesDictionary(...)100%11100%
SetVariablesDictionary(...)100%11100%
UpdateVariablesDictionary(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/VariableStorageDrivers/WorkflowInstanceStorageDriver.cs

#LineLine coverage
 1using System.ComponentModel.DataAnnotations;
 2using System.Text.Json;
 3using System.Text.Json.Nodes;
 4using Elsa.Expressions.Helpers;
 5using Elsa.Extensions;
 6using JetBrains.Annotations;
 7using Microsoft.Extensions.Logging;
 8
 9namespace Elsa.Workflows;
 10
 11/// <summary>
 12/// A storage driver that stores objects in the workflow state itself.
 13/// </summary>
 14[Display(Name = "Workflow Instance")]
 15[UsedImplicitly]
 42116public class WorkflowInstanceStorageDriver(IPayloadSerializer payloadSerializer, ILogger<WorkflowInstanceStorageDriver> 
 17{
 18    /// <summary>
 19    /// The key used to store the variables in the workflow state.
 20    /// </summary>
 21    public const string VariablesDictionaryStateKey = "Variables";
 22
 23    /// <inheritdoc />
 024    public double Priority => 5;
 25    /// <inheritdoc />
 026    public IEnumerable<string> Tags => [];
 27
 28    /// <inheritdoc />
 29    public ValueTask WriteAsync(string id, object value, StorageDriverContext context)
 30    {
 8731        UpdateVariablesDictionary(context, dictionary =>
 8732        {
 8733            try
 8734            {
 8735                var node = JsonSerializer.SerializeToNode(value);
 8736                dictionary[id] = node!;
 8737            }
 038            catch (Exception ex) when (ex is JsonException or NotSupportedException or ObjectDisposedException)
 8739            {
 040                logger.LogWarning(ex, "Failed to serialize variable '{VariableId}' of type '{VariableType}' for workflow
 041                    id, value?.GetType().FullName ?? "null");
 8742
 043                dictionary.Remove(id);
 044            }
 17445        });
 8746        return ValueTask.CompletedTask;
 47    }
 48
 49    /// <inheritdoc />
 50    public ValueTask<object?> ReadAsync(string id, StorageDriverContext context)
 51    {
 1552        var dictionary = GetVariablesDictionary(context);
 1553        var node = dictionary.GetValueOrDefault(id);
 1554        var variable = context.Variable;
 1555        var variableType = variable.GetVariableType();
 1556        var options = new ObjectConverterOptions
 1557        {
 1558            DeserializeJsonObjectToObject = true,
 1559            SerializerOptions = payloadSerializer.GetOptions()
 1560        };
 1561        var result = node.TryConvertTo(variableType, options);
 1562        var parsedValue = result.Success ? result.Value : node;
 1563        return new (parsedValue);
 64    }
 65
 66    /// <inheritdoc />
 67    public ValueTask DeleteAsync(string id, StorageDriverContext context)
 68    {
 6269        UpdateVariablesDictionary(context, dictionary => dictionary.Remove(id));
 3170        return ValueTask.CompletedTask;
 71    }
 72
 18873    private VariablesDictionary GetVariablesDictionary(StorageDriverContext context) => context.ExecutionContext.Propert
 11874    private void SetVariablesDictionary(StorageDriverContext context, VariablesDictionary dictionary) => context.Executi
 75
 76    private void UpdateVariablesDictionary(StorageDriverContext context, Action<VariablesDictionary> update)
 77    {
 11878        var dictionary = GetVariablesDictionary(context);
 11879        update(dictionary);
 11880        SetVariablesDictionary(context, dictionary);
 11881    }
 82}
 83
 84public class VariablesDictionary : Dictionary<string, JsonNode>;