< Summary

Information
Class: Elsa.Workflows.Management.Mappers.VariableDefinitionMapper
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Mappers/VariableDefinitionMapper.cs
Line coverage
72%
Covered lines: 42
Uncovered lines: 16
Coverable lines: 58
Total lines: 121
Line coverage: 72.4%
Branch coverage
55%
Covered branches: 31
Total branches: 56
Branch coverage: 55.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Map(...)56.25%271665%
Map(...)100%44100%
Map(...)59.09%222294.73%
Map(...)50%44100%
GetStorageDriverType(...)30%521025%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Mappers/VariableDefinitionMapper.cs

#LineLine coverage
 1using Elsa.Expressions.Helpers;
 2using Elsa.Extensions;
 3using Elsa.Workflows.Memory;
 4using Elsa.Workflows.Models;
 5using Elsa.Workflows.Services;
 6using Microsoft.Extensions.DependencyInjection;
 7using Microsoft.Extensions.Logging;
 8using Elsa.Common.Serialization;
 9
 10namespace Elsa.Workflows.Management.Mappers;
 11
 12/// <summary>
 13/// Maps <see cref="Variable"/>s to <see cref="VariableDefinition"/>s and vice versa.
 14/// </summary>
 1015public class VariableDefinitionMapper(ISerializationTypeRegistry workflowJsonTypeRegistry, IServiceScopeFactory scopeFac
 16{
 17    /// <summary>
 18    /// Maps a <see cref="VariableDefinition"/> to a <see cref="Variable"/>.
 19    /// </summary>
 20    public Variable? Map(VariableDefinition source)
 21    {
 9422        var type = SerializationTypeResolver.TryResolveType(workflowJsonTypeRegistry, source.TypeName, out var resolvedT
 23
 9424        if (type == null)
 25        {
 126            logger.LogWarning("Failed to resolve the type {TypeName} of variable {VariableName}. Variable will not be ma
 127            return null;
 28        }
 29
 9330        var valueType = type.IsArray ? source.IsArray ? type.MakeArrayType() : type : source.IsArray ? type.MakeArrayTyp
 9331        var variableGenericType = typeof(Variable<>).MakeGenericType(valueType);
 9332        var variable = (Variable)Activator.CreateInstance(variableGenericType)!;
 33
 9334        if (!string.IsNullOrEmpty(source.Id))
 9335            variable.Id = source.Id;
 36
 9337        variable.Name = source.Name;
 38
 9339        if (!string.IsNullOrWhiteSpace(source.Value))
 40        {
 041            source.Value?.TryConvertTo(valueType).OnSuccess(value =>
 042            {
 043                variable.Value = value;
 044            }).OnFailure(ex =>
 045            {
 046                logger.LogWarning(ex, "Failed to convert the default value {DefaultValue} of variable {VariableName} to 
 047            });
 48        }
 49
 9350        variable.StorageDriverType = GetStorageDriverType(source.StorageDriverTypeName);
 51
 9352        return variable;
 53    }
 54
 55    /// <summary>
 56    /// Maps a list of <see cref="VariableDefinition"/>s to a list of <see cref="Variable"/>.
 57    /// </summary>
 58    public IEnumerable<Variable> Map(IEnumerable<VariableDefinition>? source) =>
 191259        source?
 191260            .Select(Map)
 9261            .Where(x => x != null)
 9262            .Select(x => x!)
 191263        ?? [];
 64
 65    /// <summary>
 66    /// Maps a <see cref="Variable"/> to a <see cref="VariableDefinition"/>.
 67    /// </summary>
 68    public VariableDefinition Map(Variable source)
 69    {
 270        var variableType = source.GetType();
 271        var valueType = variableType.IsConstructedGenericType ? variableType.GetGenericArguments().FirstOrDefault() ?? t
 272        var valueTypeAlias = SerializationTypeResolver.TryGetAlias(workflowJsonTypeRegistry, valueType, out var alias) ?
 273        var value = source.Value;
 274        var serializedValue = value.Format();
 275        var storageDriverTypeName = source.StorageDriverType != null
 276            ? SerializationTypeResolver.TryGetAlias(workflowJsonTypeRegistry, source.StorageDriverType, out var storageD
 277                ? storageDriverAlias
 278                : source.StorageDriverType.GetSimpleAssemblyQualifiedName()
 279            : null;
 80
 81        // Handles the case where an alias exists for an array or collection type. E.g. byte[] -> ByteArray.
 282        if (valueTypeAlias != null && (valueType.IsArray || valueType.IsCollectionType()))
 083            return new(source.Id, source.Name, valueTypeAlias, false, serializedValue, storageDriverTypeName);
 84
 285        var isArray = valueType.IsArray;
 286        var isCollection = valueType.IsCollectionType();
 287        var elementValueType = isArray ? valueType.GetElementType()! : isCollection ? valueType.GenericTypeArguments[0] 
 288        var elementTypeAlias = SerializationTypeResolver.TryGetAlias(workflowJsonTypeRegistry, elementValueType, out var
 289            ? elementAlias
 290            : elementValueType.GetSimpleAssemblyQualifiedName();
 91
 292        return new(source.Id, source.Name, elementTypeAlias, isArray, serializedValue, storageDriverTypeName);
 93    }
 94
 95    /// <summary>
 96    /// Maps a list of <see cref="Variable"/>s to a list of <see cref="VariableDefinition"/>s.
 97    /// </summary>
 1198    public IEnumerable<VariableDefinition> Map(IEnumerable<Variable>? source) => source?.Select(Map) ?? [];
 99
 100    private Type? GetStorageDriverType(string? storageDriverTypeName)
 101    {
 93102        if (string.IsNullOrEmpty(storageDriverTypeName))
 1103            return null;
 104
 92105        if (SerializationTypeResolver.TryResolveType(workflowJsonTypeRegistry, storageDriverTypeName, out var type) && t
 92106            return type;
 107
 108        // TODO: The following code handles backward compatibility with variable definitions referencing older .NET type
 109        // We will refactor this by storing a driver identifier rather than its full type name - which is brittle in cas
 0110        using var scope = scopeFactory.CreateScope();
 0111        var storageDrivers = scope.ServiceProvider.GetServices<IStorageDriver>();
 0112        foreach (var storageDriver in storageDrivers)
 113        {
 0114            var typeName = storageDriver.GetType().Name;
 0115            if (storageDriverTypeName.Contains(typeName, StringComparison.OrdinalIgnoreCase))
 0116                return storageDriver.GetType();
 117        }
 118
 0119        return null;
 0120    }
 121}