< Summary

Information
Class: Elsa.Workflows.VariableMapper
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/VariableMapper.cs
Line coverage
85%
Covered lines: 23
Uncovered lines: 4
Coverable lines: 27
Total lines: 81
Line coverage: 85.1%
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
.ctor(...)100%11100%
.ctor()100%210%
Map(...)62.5%8885.71%
Map(...)83.33%66100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/VariableMapper.cs

#LineLine coverage
 1using Elsa.Expressions.Contracts;
 2using Elsa.Expressions.Extensions;
 3using Elsa.Expressions.Helpers;
 4using Elsa.Expressions.Services;
 5using Elsa.Extensions;
 6using Elsa.Workflows.Memory;
 7using Elsa.Workflows.Models;
 8using Microsoft.Extensions.Logging;
 9using Microsoft.Extensions.Logging.Abstractions;
 10
 11namespace Elsa.Workflows;
 12
 13/// <summary>
 14/// Maps variables to and from <see cref="VariableModel"/> instances.
 15/// </summary>
 16public class VariableMapper
 17{
 18    private readonly ILogger<VariableMapper> _logger;
 19    private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry;
 20
 21    /// <summary>
 22    /// Initializes a new instance of the <see cref="VariableMapper"/> class.
 23    /// </summary>
 24    /// <param name="wellKnownTypeRegistry">The well-known type registry.</param>
 25    /// <param name="logger">The logger.</param>
 387026    public VariableMapper(IWellKnownTypeRegistry wellKnownTypeRegistry, ILogger<VariableMapper> logger)
 27    {
 387028        _wellKnownTypeRegistry = wellKnownTypeRegistry;
 387029        _logger = logger;
 387030    }
 31
 32    /// <inheritdoc />
 033    public VariableMapper() : this(new WellKnownTypeRegistry(), NullLogger<VariableMapper>.Instance)
 34    {
 35
 036    }
 37
 38    /// <summary>
 39    /// Maps a <see cref="Variable"/> to a <see cref="VariableModel"/>.
 40    /// </summary>
 41    public Variable Map(VariableModel source)
 42    {
 33943        var typeName = source.TypeName;
 44
 33945        if (string.IsNullOrWhiteSpace(source.TypeName))
 046            typeName = _wellKnownTypeRegistry.GetAliasOrDefault(typeof(object));
 47
 33948        if (!_wellKnownTypeRegistry.TryGetTypeOrDefault(typeName, out var type))
 049            type = typeof(object);
 50
 33951        var variableGenericType = typeof(Variable<>).MakeGenericType(type);
 33952        var variable = (Variable)Activator.CreateInstance(variableGenericType)!;
 53
 54        // ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
 33955        variable.Id = source.Id ?? Guid.NewGuid().ToString("N"); // Temporarily assign a new ID if the source doesn't ha
 33956        variable.Name = source.Name;
 57
 33958        source.Value.TryConvertTo(type)
 33959            .OnSuccess(value => variable.Value = value)
 33960            .OnFailure(e => _logger.LogWarning("Failed to convert {SourceValue} to {TargetType}", source.Value, type.Nam
 61
 33962        variable.StorageDriverType = !string.IsNullOrEmpty(source.StorageDriverTypeName) ? Type.GetType(source.StorageDr
 63
 33964        return variable;
 65    }
 66
 67    /// <summary>
 68    /// Maps a <see cref="VariableModel"/> to a <see cref="Variable"/>.
 69    /// </summary>
 70    public VariableModel Map(Variable source)
 71    {
 18872        var variableType = source.GetType();
 18873        var value = source.Value;
 18874        var valueType = variableType.IsConstructedGenericType ? variableType.GetGenericArguments().FirstOrDefault() ?? t
 18875        var valueTypeAlias = _wellKnownTypeRegistry.GetAliasOrDefault(valueType);
 18876        var storageDriverTypeName = source.StorageDriverType?.GetSimpleAssemblyQualifiedName();
 18877        var serializedValue = value.Format();
 78
 18879        return new(source.Id, source.Name, valueTypeAlias, serializedValue, storageDriverTypeName);
 80    }
 81}