< 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
42%
Covered lines: 23
Uncovered lines: 31
Coverable lines: 54
Total lines: 118
Line coverage: 42.5%
Branch coverage
33%
Covered branches: 18
Total branches: 54
Branch coverage: 33.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(...)50%512057.14%
Map(...)100%44100%
Map(...)0%342180%
Map(...)50%44100%
GetStorageDriverType(...)25%29830.76%

File(s)

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

#LineLine coverage
 1using Elsa.Expressions.Contracts;
 2using Elsa.Expressions.Extensions;
 3using Elsa.Expressions.Helpers;
 4using Elsa.Extensions;
 5using Elsa.Workflows.Memory;
 6using Elsa.Workflows.Models;
 7using Microsoft.Extensions.DependencyInjection;
 8using Microsoft.Extensions.Logging;
 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>
 115public class VariableDefinitionMapper(IWellKnownTypeRegistry wellKnownTypeRegistry, IServiceScopeFactory scopeFactory, I
 16{
 17    /// <summary>
 18    /// Maps a <see cref="VariableDefinition"/> to a <see cref="Variable"/>.
 19    /// </summary>
 20    public Variable? Map(VariableDefinition source)
 21    {
 3022        var aliasedType = wellKnownTypeRegistry.TryGetType(source.TypeName, out var aliasedTypeValue) ? aliasedTypeValue
 3023        var type = aliasedType ?? Type.GetType(source.TypeName);
 24
 3025        if (type == null)
 26        {
 027            logger.LogWarning("Failed to resolve the type {TypeName} of variable {VariableName}. Variable will not be ma
 028            return null;
 29        }
 30
 3031        var valueType = aliasedType is { IsArray: true } ? source.IsArray ? aliasedType.MakeArrayType() : aliasedType : 
 3032        var variableGenericType = typeof(Variable<>).MakeGenericType(valueType);
 3033        var variable = (Variable)Activator.CreateInstance(variableGenericType)!;
 34
 3035        if (!string.IsNullOrEmpty(source.Id))
 3036            variable.Id = source.Id;
 37
 3038        variable.Name = source.Name;
 39
 3040        if (!string.IsNullOrWhiteSpace(source.Value))
 41        {
 042            source.Value?.TryConvertTo(valueType).OnSuccess(value =>
 043            {
 044                variable.Value = value;
 045            }).OnFailure(ex =>
 046            {
 047                logger.LogWarning(ex, "Failed to convert the default value {DefaultValue} of variable {VariableName} to 
 048            });
 49        }
 50
 3051        variable.StorageDriverType = GetStorageDriverType(source.StorageDriverTypeName);
 52
 3053        return variable;
 54    }
 55
 56    /// <summary>
 57    /// Maps a list of <see cref="VariableDefinition"/>s to a list of <see cref="Variable"/>.
 58    /// </summary>
 59    public IEnumerable<Variable> Map(IEnumerable<VariableDefinition>? source) =>
 106560        source?
 106561            .Select(Map)
 3062            .Where(x => x != null)
 3063            .Select(x => x!)
 106564        ?? [];
 65
 66    /// <summary>
 67    /// Maps a <see cref="Variable"/> to a <see cref="VariableDefinition"/>.
 68    /// </summary>
 69    public VariableDefinition Map(Variable source)
 70    {
 071        var variableType = source.GetType();
 072        var valueType = variableType.IsConstructedGenericType ? variableType.GetGenericArguments().FirstOrDefault() ?? t
 073        var valueTypeAlias = wellKnownTypeRegistry.TryGetAlias(valueType, out var alias) ? alias : null;
 074        var value = source.Value;
 075        var serializedValue = value.Format();
 076        var storageDriverTypeName = source.StorageDriverType?.GetSimpleAssemblyQualifiedName();
 77
 78        // Handles the case where an alias exists for an array or collection type. E.g. byte[] -> ByteArray.
 079        if (valueTypeAlias != null && (valueType.IsArray || valueType.IsCollectionType()))
 080            return new(source.Id, source.Name, valueTypeAlias, false, serializedValue, storageDriverTypeName);
 81
 082        var isArray = valueType.IsArray;
 083        var isCollection = valueType.IsCollectionType();
 084        var elementValueType = isArray ? valueType.GetElementType()! : isCollection ? valueType.GenericTypeArguments[0] 
 085        var elementTypeAlias = wellKnownTypeRegistry.GetAliasOrDefault(elementValueType);
 86
 087        return new(source.Id, source.Name, elementTypeAlias, isArray, serializedValue, storageDriverTypeName);
 88    }
 89
 90    /// <summary>
 91    /// Maps a list of <see cref="Variable"/>s to a list of <see cref="VariableDefinition"/>s.
 92    /// </summary>
 193    public IEnumerable<VariableDefinition> Map(IEnumerable<Variable>? source) => source?.Select(Map) ?? [];
 94
 95    private Type? GetStorageDriverType(string? storageDriverTypeName)
 96    {
 3097        if (string.IsNullOrEmpty(storageDriverTypeName))
 098            return null;
 99
 30100        var type = Type.GetType(storageDriverTypeName);
 101
 30102        if (type != null)
 30103            return type;
 104
 105        // TODO: The following code handles backward compatibility with variable definitions referencing older .NET type
 106        // We will refactor this by storing a driver identifier rather than its full type name - which is brittle in cas
 0107        using var scope = scopeFactory.CreateScope();
 0108        var storageDrivers = scope.ServiceProvider.GetServices<IStorageDriver>();
 0109        foreach (var storageDriver in storageDrivers)
 110        {
 0111            var typeName = storageDriver.GetType().Name;
 0112            if (storageDriverTypeName.Contains(typeName, StringComparison.OrdinalIgnoreCase))
 0113                return storageDriver.GetType();
 114        }
 115
 0116        return null;
 0117    }
 118}