diff --git a/src/scripting/Elsa.Scripting.JavaScript/Converters/TruncatingNumberJsonConverter.cs b/src/scripting/Elsa.Scripting.JavaScript/Converters/TruncatingNumberJsonConverter.cs index b6e06f850..a6569d2e2 100644 --- a/src/scripting/Elsa.Scripting.JavaScript/Converters/TruncatingNumberJsonConverter.cs +++ b/src/scripting/Elsa.Scripting.JavaScript/Converters/TruncatingNumberJsonConverter.cs @@ -26,6 +26,8 @@ namespace Elsa.Scripting.JavaScript.Converters case double: { var doubleValue = Convert.ToDouble(value); + + // ReSharper disable once CompareOfFloatsByEqualityOperator return doubleValue == Math.Truncate(doubleValue); } default: diff --git a/src/scripting/Elsa.Scripting.JavaScript/Handlers/ConfigureJavaScriptEngine.cs b/src/scripting/Elsa.Scripting.JavaScript/Handlers/ConfigureJavaScriptEngine.cs index 563e3de97..3b93cf316 100644 --- a/src/scripting/Elsa.Scripting.JavaScript/Handlers/ConfigureJavaScriptEngine.cs +++ b/src/scripting/Elsa.Scripting.JavaScript/Handlers/ConfigureJavaScriptEngine.cs @@ -33,6 +33,7 @@ namespace Elsa.Scripting.JavaScript.Handlers // Global functions. engine.SetValue("guid", (Func) (() => Guid.NewGuid().ToString())); + engine.SetValue("parseGuid", (Func) (Guid.Parse)); engine.SetValue("setVariable", (Action) ((name, value) => activityExecutionContext.SetVariable(name, value))); engine.SetValue("getVariable", (Func) (name => activityExecutionContext.GetVariable(name))); engine.SetValue("getConfig", (Func) (name => _configuration.GetSection(name).Value)); diff --git a/src/scripting/Elsa.Scripting.JavaScript/Services/JintEvaluationResultConverterFactory.cs b/src/scripting/Elsa.Scripting.JavaScript/Services/JintEvaluationResultConverterFactory.cs index 5c4b9adcd..9fdf8b22b 100644 --- a/src/scripting/Elsa.Scripting.JavaScript/Services/JintEvaluationResultConverterFactory.cs +++ b/src/scripting/Elsa.Scripting.JavaScript/Services/JintEvaluationResultConverterFactory.cs @@ -5,7 +5,9 @@ namespace Elsa.Scripting.JavaScript.Services { public class JintEvaluationResultConverterFactory { - readonly IServiceProvider serviceProvider; + private readonly IServiceProvider _serviceProvider; + + public JintEvaluationResultConverterFactory(IServiceProvider serviceProvider) => this._serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); public IConvertsJintEvaluationResult GetConverter() { @@ -13,7 +15,7 @@ namespace Elsa.Scripting.JavaScript.Services // Builds a chain-of-responsibility service // Note: The order in which these classes execute is bottom-to-top - + service = GetConvertChangeTypeService(); service = GetPlainObjectService(service); service = GetEnumerableConvertingService(service); @@ -24,30 +26,20 @@ namespace Elsa.Scripting.JavaScript.Services return service; } - static IConvertsJintEvaluationResult GetConvertChangeTypeService() - => new ConvertChangeTypeResultConverter(); + static IConvertsJintEvaluationResult GetConvertChangeTypeService() => new ConvertChangeTypeResultConverter(); - static IConvertsJintEvaluationResult GetPlainObjectService(IConvertsJintEvaluationResult wrapped) - => new PlainObjectResultConverter(wrapped); + static IConvertsJintEvaluationResult GetPlainObjectService(IConvertsJintEvaluationResult wrapped) => new PlainObjectResultConverter(wrapped); - IConvertsJintEvaluationResult GetEnumerableConvertingService(IConvertsJintEvaluationResult wrapped) - => new EnumerableResultConverter(wrapped); + IConvertsJintEvaluationResult GetEnumerableConvertingService(IConvertsJintEvaluationResult wrapped) => new EnumerableResultConverter(wrapped); IConvertsJintEvaluationResult GetExpandoConvertingService(IConvertsJintEvaluationResult wrapped) { - var enumerableConverter = serviceProvider.GetRequiredService(); + var enumerableConverter = _serviceProvider.GetRequiredService(); return new ExpandoObjectToDictionaryWhenNoDesiredTypeResultConverter(enumerableConverter, wrapped); } - - static IConvertsJintEvaluationResult GetTypeConverterConvertingService(IConvertsJintEvaluationResult wrapped) - => new TypeConverterResultConverter(wrapped); - - static IConvertsJintEvaluationResult GetNullConvertingService(IConvertsJintEvaluationResult wrapped) - => new NullResultConverter(wrapped); - public JintEvaluationResultConverterFactory(IServiceProvider serviceProvider) - { - this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); - } + static IConvertsJintEvaluationResult GetTypeConverterConvertingService(IConvertsJintEvaluationResult wrapped) => new TypeConverterResultConverter(wrapped); + + static IConvertsJintEvaluationResult GetNullConvertingService(IConvertsJintEvaluationResult wrapped) => new NullResultConverter(wrapped); } } \ No newline at end of file diff --git a/src/scripting/Elsa.Scripting.JavaScript/Services/TypeConverterResultConverter.cs b/src/scripting/Elsa.Scripting.JavaScript/Services/TypeConverterResultConverter.cs index bf8a59d80..c9181216d 100644 --- a/src/scripting/Elsa.Scripting.JavaScript/Services/TypeConverterResultConverter.cs +++ b/src/scripting/Elsa.Scripting.JavaScript/Services/TypeConverterResultConverter.cs @@ -9,10 +9,15 @@ namespace Elsa.Scripting.JavaScript.Services public object? ConvertToDesiredType(object? evaluationResult, Type desiredType) { - var converter = TypeDescriptor.GetConverter(evaluationResult); + var converter = TypeDescriptor.GetConverter(evaluationResult!); if (converter.CanConvertTo(desiredType)) - return converter.ConvertTo(evaluationResult, desiredType); + return converter.ConvertTo(evaluationResult!, desiredType); + + var targetConverter = TypeDescriptor.GetConverter(desiredType); + + if (targetConverter.CanConvertFrom(evaluationResult!.GetType())) + return targetConverter.ConvertFrom(evaluationResult!); return wrapped.ConvertToDesiredType(evaluationResult, desiredType); }