| | | 1 | | using Elsa.Workflows.Exceptions; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc /> |
| | 14 | 8 | | public class OutputConverterInvoker( |
| | 14 | 9 | | IOutputConverterRegistry registry, |
| | 14 | 10 | | IOutputConverterSettingsValidator settingsValidator) : IOutputConverterInvoker |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc /> |
| | | 13 | | public object? Invoke( |
| | | 14 | | ActivityExecutionContext activityExecutionContext, |
| | | 15 | | Output output, |
| | | 16 | | string outputName, |
| | | 17 | | Type sourceType, |
| | | 18 | | object value, |
| | | 19 | | OutputBindingDestination destination) |
| | | 20 | | { |
| | 16 | 21 | | var configuration = output.Converter ?? throw new InvalidOperationException("The output binding has no converter |
| | 16 | 22 | | var descriptor = registry.Find(configuration.Id); |
| | | 23 | | |
| | 16 | 24 | | if (descriptor == null) |
| | 1 | 25 | | throw CreateException(OutputConversionFailureStage.Resolution); |
| | | 26 | | |
| | 15 | 27 | | if (!IsRuntimeValueCompatible(value, sourceType) || !descriptor.SourceType.IsAssignableFrom(sourceType)) |
| | 1 | 28 | | throw CreateException(OutputConversionFailureStage.SourceCompatibility); |
| | | 29 | | |
| | 14 | 30 | | if (!OutputConverterRegistry.IsAssignableToDestination(descriptor.ResultType, destination.Type)) |
| | 2 | 31 | | throw CreateException(OutputConversionFailureStage.ResultValidation); |
| | | 32 | | |
| | | 33 | | IOutputConverter converter; |
| | | 34 | | |
| | | 35 | | try |
| | | 36 | | { |
| | 12 | 37 | | converter = activityExecutionContext.WorkflowExecutionContext.ServiceProvider |
| | 12 | 38 | | .GetRequiredKeyedService<IOutputConverter>(configuration.Id); |
| | 12 | 39 | | } |
| | 0 | 40 | | catch (Exception e) |
| | | 41 | | { |
| | 0 | 42 | | throw CreateException(OutputConversionFailureStage.Resolution, e); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | IReadOnlyCollection<string> settingsErrors; |
| | | 46 | | |
| | | 47 | | try |
| | | 48 | | { |
| | 12 | 49 | | settingsErrors = settingsValidator.Validate(descriptor, converter, configuration.Settings); |
| | 12 | 50 | | } |
| | 0 | 51 | | catch (Exception e) |
| | | 52 | | { |
| | 0 | 53 | | throw CreateException(OutputConversionFailureStage.SettingsValidation, e); |
| | | 54 | | } |
| | | 55 | | |
| | 12 | 56 | | if (settingsErrors.Count > 0) |
| | 2 | 57 | | throw CreateException(OutputConversionFailureStage.SettingsValidation); |
| | | 58 | | |
| | | 59 | | object? convertedValue; |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 10 | 63 | | convertedValue = converter.Convert(new(value, sourceType, destination.Type, configuration.Settings)); |
| | 9 | 64 | | } |
| | 1 | 65 | | catch (Exception e) |
| | | 66 | | { |
| | 1 | 67 | | throw CreateException(OutputConversionFailureStage.Invocation, e); |
| | | 68 | | } |
| | | 69 | | |
| | 9 | 70 | | if (convertedValue == null) |
| | | 71 | | { |
| | 0 | 72 | | if (!destination.AllowsNull) |
| | 0 | 73 | | throw CreateException(OutputConversionFailureStage.ResultValidation); |
| | | 74 | | |
| | 0 | 75 | | return null; |
| | | 76 | | } |
| | | 77 | | |
| | 9 | 78 | | if (!IsRuntimeValueCompatible(convertedValue, descriptor.ResultType) || |
| | 9 | 79 | | !IsRuntimeValueCompatible(convertedValue, destination.Type)) |
| | | 80 | | { |
| | 1 | 81 | | throw CreateException(OutputConversionFailureStage.ResultValidation); |
| | | 82 | | } |
| | | 83 | | |
| | 8 | 84 | | return convertedValue; |
| | | 85 | | |
| | | 86 | | OutputConversionException CreateException(OutputConversionFailureStage stage, Exception? innerException = null) |
| | 8 | 87 | | new( |
| | 8 | 88 | | configuration.Id, |
| | 8 | 89 | | stage, |
| | 8 | 90 | | activityExecutionContext.Activity.Id, |
| | 8 | 91 | | activityExecutionContext.Activity.Type, |
| | 8 | 92 | | outputName, |
| | 8 | 93 | | destination.Id, |
| | 8 | 94 | | sourceType, |
| | 8 | 95 | | destination.Type, |
| | 8 | 96 | | innerException); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static bool IsRuntimeValueCompatible(object value, Type declaredType) |
| | | 100 | | { |
| | 32 | 101 | | if (declaredType.IsInstanceOfType(value)) |
| | 31 | 102 | | return true; |
| | | 103 | | |
| | 1 | 104 | | return Nullable.GetUnderlyingType(declaredType)?.IsInstanceOfType(value) == true; |
| | | 105 | | } |
| | | 106 | | } |