< Summary

Information
Class: Elsa.Extensions.VariableExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/VariableExtensions.cs
Line coverage
80%
Covered lines: 25
Uncovered lines: 6
Coverable lines: 31
Total lines: 123
Line coverage: 80.6%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_SerializerOptions()100%22100%
WithWorkflowStorage(...)100%210%
WithWorkflowStorage(...)100%11100%
WithMemoryStorage(...)100%210%
WithMemoryStorage(...)100%210%
WithStorage(...)100%11100%
WithStorage(...)100%11100%
Set(...)100%11100%
ParseValue(...)100%11100%
ParseValue(...)100%44100%
TryParseValue(...)100%1140%
GetVariableType(...)50%22100%

File(s)

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

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Text.Encodings.Web;
 3using System.Text.Json;
 4using System.Text.Json.Serialization;
 5using System.Text.Unicode;
 6using Elsa.Expressions.Helpers;
 7using Elsa.Expressions.Models;
 8using Elsa.Workflows;
 9using Elsa.Workflows.Memory;
 10using Elsa.Workflows.Serialization.Converters;
 11
 12// ReSharper disable once CheckNamespace
 13namespace Elsa.Extensions;
 14
 15/// <summary>
 16/// Adds extension methods to <see cref="Variable"/> for configuring storage.
 17/// </summary>
 18public static class VariableExtensions
 19{
 20    private static JsonSerializerOptions? _serializerOptions;
 21
 22    private static JsonSerializerOptions SerializerOptions =>
 17223        _serializerOptions ??= new JsonSerializerOptions
 17224        {
 17225            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 17226            ReferenceHandler = ReferenceHandler.Preserve,
 17227            PropertyNameCaseInsensitive = true,
 17228            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
 17229        }.WithConverters(
 17230            new JsonStringEnumConverter(),
 17231            new ExpandoObjectConverterFactory());
 32
 33    /// <summary>
 34    /// Configures the variable to use the <see cref="WorkflowInstanceStorageDriver"/>.
 35    /// </summary>
 036    public static Variable WithWorkflowStorage(this Variable variable) => variable.WithStorage<WorkflowInstanceStorageDr
 37
 38    /// <summary>
 39    /// Configures the variable to use the <see cref="WorkflowInstanceStorageDriver"/>.
 40    /// </summary>
 19341    public static Variable<T> WithWorkflowStorage<T>(this Variable<T> variable) => (Variable<T>)variable.WithStorage<Wor
 42
 43    /// <summary>
 44    /// Configures the variable to use the <see cref="MemoryStorageDriver"/>.
 45    /// </summary>
 046    public static Variable WithMemoryStorage(this Variable variable) => variable.WithStorage<MemoryStorageDriver>();
 47
 48    /// <summary>
 49    /// Configures the variable to use the <see cref="MemoryStorageDriver"/>.
 50    /// </summary>
 051    public static Variable<T> WithMemoryStorage<T>(this Variable<T> variable) => (Variable<T>)variable.WithStorage<Memor
 52
 53    extension(Variable variable)
 54    {
 55        /// <summary>
 56        /// Configures the variable to use the specified <see cref="IStorageDriver"/> type.
 57        /// </summary>
 19358        public Variable WithStorage<T>() => variable.WithStorage(typeof(T));
 59
 60        /// <summary>
 61        /// Configures the variable to use the specified <see cref="IStorageDriver"/> type.
 62        /// </summary>
 63        public Variable WithStorage(Type storageDriverType)
 64        {
 19365            variable.StorageDriverType = storageDriverType;
 19366            return variable;
 67        }
 68
 69        public void Set(ActivityExecutionContext context, object? value)
 70        {
 2371            var parsedValue = variable.ParseValue(value);
 72            // Set the value.
 2273            ((MemoryBlockReference)variable).Set(context, parsedValue);
 2274        }
 75
 76        /// <summary>
 77        /// Converts the specified value into a type that is compatible with the variable.
 78        /// </summary>
 79        [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions
 80        public object? ParseValue(object? value)
 81        {
 14282            var genericType = variable.GetType();
 14183            return ParseValue(genericType, value);
 84        }
 85    }
 86
 87    public static object? ParseValue(Type type, object? value)
 88    {
 17289        var genericType = type.GenericTypeArguments.FirstOrDefault();
 17290        var converterOptions = new ObjectConverterOptions(SerializerOptions);
 17291        return genericType == null ? value : value?.ConvertTo(genericType, converterOptions);
 92    }
 93
 94    extension(Variable variable)
 95    {
 96        /// <summary>
 97        /// Converts the specified value into a type that is compatible with the variable.
 98        /// </summary>
 99        [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions
 100        public bool TryParseValue(object? value, out object? parsedValue)
 101        {
 102            try
 103            {
 15104                parsedValue = variable.ParseValue(value);
 15105                return true;
 106            }
 0107            catch
 108            {
 0109                parsedValue = null;
 0110                return false;
 111            }
 112        }
 113
 114        /// <summary>
 115        /// Return the type of the variable.
 116        /// </summary>
 117        public Type GetVariableType()
 118        {
 15119            var variableType = variable.GetType();
 15120            return variableType.GenericTypeArguments.Any() ? variableType.GetGenericArguments().First() : typeof(object)
 121        }
 122    }
 123}