| | | 1 | | using Elsa.Expressions.Contracts; |
| | | 2 | | using Elsa.Expressions.Extensions; |
| | | 3 | | using Elsa.Expressions.Helpers; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Memory; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Management.Mappers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Maps <see cref="Variable"/>s to <see cref="VariableDefinition"/>s and vice versa. |
| | | 14 | | /// </summary> |
| | 1 | 15 | | public 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 | | { |
| | 30 | 22 | | var aliasedType = wellKnownTypeRegistry.TryGetType(source.TypeName, out var aliasedTypeValue) ? aliasedTypeValue |
| | 30 | 23 | | var type = aliasedType ?? Type.GetType(source.TypeName); |
| | | 24 | | |
| | 30 | 25 | | if (type == null) |
| | | 26 | | { |
| | 0 | 27 | | logger.LogWarning("Failed to resolve the type {TypeName} of variable {VariableName}. Variable will not be ma |
| | 0 | 28 | | return null; |
| | | 29 | | } |
| | | 30 | | |
| | 30 | 31 | | var valueType = aliasedType is { IsArray: true } ? source.IsArray ? aliasedType.MakeArrayType() : aliasedType : |
| | 30 | 32 | | var variableGenericType = typeof(Variable<>).MakeGenericType(valueType); |
| | 30 | 33 | | var variable = (Variable)Activator.CreateInstance(variableGenericType)!; |
| | | 34 | | |
| | 30 | 35 | | if (!string.IsNullOrEmpty(source.Id)) |
| | 30 | 36 | | variable.Id = source.Id; |
| | | 37 | | |
| | 30 | 38 | | variable.Name = source.Name; |
| | | 39 | | |
| | 30 | 40 | | if (!string.IsNullOrWhiteSpace(source.Value)) |
| | | 41 | | { |
| | 0 | 42 | | source.Value?.TryConvertTo(valueType).OnSuccess(value => |
| | 0 | 43 | | { |
| | 0 | 44 | | variable.Value = value; |
| | 0 | 45 | | }).OnFailure(ex => |
| | 0 | 46 | | { |
| | 0 | 47 | | logger.LogWarning(ex, "Failed to convert the default value {DefaultValue} of variable {VariableName} to |
| | 0 | 48 | | }); |
| | | 49 | | } |
| | | 50 | | |
| | 30 | 51 | | variable.StorageDriverType = GetStorageDriverType(source.StorageDriverTypeName); |
| | | 52 | | |
| | 30 | 53 | | 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) => |
| | 1065 | 60 | | source? |
| | 1065 | 61 | | .Select(Map) |
| | 30 | 62 | | .Where(x => x != null) |
| | 30 | 63 | | .Select(x => x!) |
| | 1065 | 64 | | ?? []; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Maps a <see cref="Variable"/> to a <see cref="VariableDefinition"/>. |
| | | 68 | | /// </summary> |
| | | 69 | | public VariableDefinition Map(Variable source) |
| | | 70 | | { |
| | 0 | 71 | | var variableType = source.GetType(); |
| | 0 | 72 | | var valueType = variableType.IsConstructedGenericType ? variableType.GetGenericArguments().FirstOrDefault() ?? t |
| | 0 | 73 | | var valueTypeAlias = wellKnownTypeRegistry.TryGetAlias(valueType, out var alias) ? alias : null; |
| | 0 | 74 | | var value = source.Value; |
| | 0 | 75 | | var serializedValue = value.Format(); |
| | 0 | 76 | | var storageDriverTypeName = source.StorageDriverType?.GetSimpleAssemblyQualifiedName(); |
| | | 77 | | |
| | | 78 | | // Handles the case where an alias exists for an array or collection type. E.g. byte[] -> ByteArray. |
| | 0 | 79 | | if (valueTypeAlias != null && (valueType.IsArray || valueType.IsCollectionType())) |
| | 0 | 80 | | return new(source.Id, source.Name, valueTypeAlias, false, serializedValue, storageDriverTypeName); |
| | | 81 | | |
| | 0 | 82 | | var isArray = valueType.IsArray; |
| | 0 | 83 | | var isCollection = valueType.IsCollectionType(); |
| | 0 | 84 | | var elementValueType = isArray ? valueType.GetElementType()! : isCollection ? valueType.GenericTypeArguments[0] |
| | 0 | 85 | | var elementTypeAlias = wellKnownTypeRegistry.GetAliasOrDefault(elementValueType); |
| | | 86 | | |
| | 0 | 87 | | 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> |
| | 1 | 93 | | public IEnumerable<VariableDefinition> Map(IEnumerable<Variable>? source) => source?.Select(Map) ?? []; |
| | | 94 | | |
| | | 95 | | private Type? GetStorageDriverType(string? storageDriverTypeName) |
| | | 96 | | { |
| | 30 | 97 | | if (string.IsNullOrEmpty(storageDriverTypeName)) |
| | 0 | 98 | | return null; |
| | | 99 | | |
| | 30 | 100 | | var type = Type.GetType(storageDriverTypeName); |
| | | 101 | | |
| | 30 | 102 | | if (type != null) |
| | 30 | 103 | | 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 |
| | 0 | 107 | | using var scope = scopeFactory.CreateScope(); |
| | 0 | 108 | | var storageDrivers = scope.ServiceProvider.GetServices<IStorageDriver>(); |
| | 0 | 109 | | foreach (var storageDriver in storageDrivers) |
| | | 110 | | { |
| | 0 | 111 | | var typeName = storageDriver.GetType().Name; |
| | 0 | 112 | | if (storageDriverTypeName.Contains(typeName, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 113 | | return storageDriver.GetType(); |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | return null; |
| | 0 | 117 | | } |
| | | 118 | | } |