| | | 1 | | using Elsa.Expressions.Contracts; |
| | | 2 | | using Elsa.Expressions.Extensions; |
| | | 3 | | using Elsa.Expressions.Helpers; |
| | | 4 | | using Elsa.Expressions.Services; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows.Memory; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Maps variables to and from <see cref="VariableModel"/> instances. |
| | | 15 | | /// </summary> |
| | | 16 | | public 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> |
| | 3870 | 26 | | public VariableMapper(IWellKnownTypeRegistry wellKnownTypeRegistry, ILogger<VariableMapper> logger) |
| | | 27 | | { |
| | 3870 | 28 | | _wellKnownTypeRegistry = wellKnownTypeRegistry; |
| | 3870 | 29 | | _logger = logger; |
| | 3870 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 0 | 33 | | public VariableMapper() : this(new WellKnownTypeRegistry(), NullLogger<VariableMapper>.Instance) |
| | | 34 | | { |
| | | 35 | | |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Maps a <see cref="Variable"/> to a <see cref="VariableModel"/>. |
| | | 40 | | /// </summary> |
| | | 41 | | public Variable Map(VariableModel source) |
| | | 42 | | { |
| | 339 | 43 | | var typeName = source.TypeName; |
| | | 44 | | |
| | 339 | 45 | | if (string.IsNullOrWhiteSpace(source.TypeName)) |
| | 0 | 46 | | typeName = _wellKnownTypeRegistry.GetAliasOrDefault(typeof(object)); |
| | | 47 | | |
| | 339 | 48 | | if (!_wellKnownTypeRegistry.TryGetTypeOrDefault(typeName, out var type)) |
| | 0 | 49 | | type = typeof(object); |
| | | 50 | | |
| | 339 | 51 | | var variableGenericType = typeof(Variable<>).MakeGenericType(type); |
| | 339 | 52 | | var variable = (Variable)Activator.CreateInstance(variableGenericType)!; |
| | | 53 | | |
| | | 54 | | // ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract |
| | 339 | 55 | | variable.Id = source.Id ?? Guid.NewGuid().ToString("N"); // Temporarily assign a new ID if the source doesn't ha |
| | 339 | 56 | | variable.Name = source.Name; |
| | | 57 | | |
| | 339 | 58 | | source.Value.TryConvertTo(type) |
| | 339 | 59 | | .OnSuccess(value => variable.Value = value) |
| | 339 | 60 | | .OnFailure(e => _logger.LogWarning("Failed to convert {SourceValue} to {TargetType}", source.Value, type.Nam |
| | | 61 | | |
| | 339 | 62 | | variable.StorageDriverType = !string.IsNullOrEmpty(source.StorageDriverTypeName) ? Type.GetType(source.StorageDr |
| | | 63 | | |
| | 339 | 64 | | 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 | | { |
| | 188 | 72 | | var variableType = source.GetType(); |
| | 188 | 73 | | var value = source.Value; |
| | 188 | 74 | | var valueType = variableType.IsConstructedGenericType ? variableType.GetGenericArguments().FirstOrDefault() ?? t |
| | 188 | 75 | | var valueTypeAlias = _wellKnownTypeRegistry.GetAliasOrDefault(valueType); |
| | 188 | 76 | | var storageDriverTypeName = source.StorageDriverType?.GetSimpleAssemblyQualifiedName(); |
| | 188 | 77 | | var serializedValue = value.Format(); |
| | | 78 | | |
| | 188 | 79 | | return new(source.Id, source.Name, valueTypeAlias, serializedValue, storageDriverTypeName); |
| | | 80 | | } |
| | | 81 | | } |