2022-06-05 20:49:47 +00:00
|
|
|
using Elsa.Expressions.Helpers;
|
|
|
|
|
using Elsa.Workflows.Core.Models;
|
|
|
|
|
using Elsa.Workflows.Core.Services;
|
2022-07-21 14:18:42 +00:00
|
|
|
using Elsa.Workflows.Management.Models;
|
2022-06-05 20:49:47 +00:00
|
|
|
|
2022-07-21 14:18:42 +00:00
|
|
|
namespace Elsa.Workflows.Management.Mappers;
|
2022-06-05 20:49:47 +00:00
|
|
|
|
|
|
|
|
public class VariableDefinitionMapper
|
|
|
|
|
{
|
|
|
|
|
private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry;
|
|
|
|
|
|
|
|
|
|
public VariableDefinitionMapper(IWellKnownTypeRegistry wellKnownTypeRegistry)
|
|
|
|
|
{
|
|
|
|
|
_wellKnownTypeRegistry = wellKnownTypeRegistry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Variable? Map(VariableDefinition source)
|
|
|
|
|
{
|
|
|
|
|
if (!_wellKnownTypeRegistry.TryGetTypeOrDefault(source.Type, out var type))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var variableGenericType = typeof(Variable<>).MakeGenericType(type);
|
|
|
|
|
var variable = (Variable)Activator.CreateInstance(variableGenericType)!;
|
|
|
|
|
|
|
|
|
|
variable.Name = source.Name;
|
|
|
|
|
variable.Value = source.Value.ConvertTo(type);
|
2022-06-07 13:48:52 +00:00
|
|
|
variable.StorageDriverId = source.StorageDriverId;
|
2022-06-05 20:49:47 +00:00
|
|
|
|
|
|
|
|
return variable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Variable> Map(IEnumerable<VariableDefinition>? source) =>
|
|
|
|
|
source?
|
|
|
|
|
.Select(Map)
|
|
|
|
|
.Where(x => x != null)
|
|
|
|
|
.Select(x => x!)
|
|
|
|
|
?? Enumerable.Empty<Variable>();
|
|
|
|
|
|
|
|
|
|
public VariableDefinition Map(Variable source)
|
|
|
|
|
{
|
|
|
|
|
var variableType = source.GetType();
|
|
|
|
|
var value = source.Value;
|
|
|
|
|
var valueType = source.Value?.GetType() ?? (variableType.IsConstructedGenericType ? variableType.GetGenericArguments().FirstOrDefault() ?? typeof(object) : typeof(object));
|
|
|
|
|
var valueTypeAlias = _wellKnownTypeRegistry.GetAliasOrDefault(valueType);
|
2022-06-07 13:48:52 +00:00
|
|
|
var driverId = source.StorageDriverId;
|
2022-06-05 20:49:47 +00:00
|
|
|
|
|
|
|
|
var serializedValue = value.Format();
|
|
|
|
|
|
2022-06-07 13:48:52 +00:00
|
|
|
return new VariableDefinition(source.Name, valueTypeAlias, serializedValue, driverId);
|
2022-06-05 20:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<VariableDefinition> Map(IEnumerable<Variable>? source) => source?.Select(Map) ?? Enumerable.Empty<VariableDefinition>();
|
|
|
|
|
}
|