| | | 1 | | using System.Dynamic; |
| | | 2 | | using Elsa.Expressions.Contracts; |
| | | 3 | | using Elsa.Expressions.Helpers; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Resilience.Models; |
| | | 6 | | using Elsa.Resilience.Serialization; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Resilience; |
| | | 9 | | |
| | 0 | 10 | | public class ResilienceStrategyConfigEvaluator(IResilienceStrategyCatalog catalog, IExpressionEvaluator expressionEvalua |
| | | 11 | | { |
| | | 12 | | public async Task<IResilienceStrategy?> EvaluateAsync(ResilienceStrategyConfig? config, ExpressionExecutionContext c |
| | | 13 | | { |
| | 0 | 14 | | if (config == null) return null; |
| | | 15 | | |
| | 0 | 16 | | var mode = config.Mode; |
| | | 17 | | |
| | 0 | 18 | | return mode switch |
| | 0 | 19 | | { |
| | 0 | 20 | | ResilienceStrategyConfigMode.Identifier => await ResolveByIdentifierAsync(config, cancellationToken), |
| | 0 | 21 | | ResilienceStrategyConfigMode.Expression => await ResolveByExpressionAsync(config, context, cancellationToken |
| | 0 | 22 | | _ => null |
| | 0 | 23 | | }; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | private async Task<IResilienceStrategy?> ResolveByIdentifierAsync(ResilienceStrategyConfig config, CancellationToken |
| | | 27 | | { |
| | 0 | 28 | | var strategyId = config.StrategyId; |
| | 0 | 29 | | return string.IsNullOrWhiteSpace(strategyId) |
| | 0 | 30 | | ? null |
| | 0 | 31 | | : await catalog.GetAsync(strategyId, cancellationToken); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | private async Task<IResilienceStrategy?> ResolveByExpressionAsync(ResilienceStrategyConfig config, ExpressionExecuti |
| | | 35 | | { |
| | 0 | 36 | | var expression = config.Expression; |
| | | 37 | | |
| | 0 | 38 | | if (expression == null) |
| | 0 | 39 | | return null; |
| | | 40 | | |
| | 0 | 41 | | var result = await expressionEvaluator.EvaluateAsync<object>(expression, context, ExpressionEvaluatorOptions.Emp |
| | | 42 | | |
| | 0 | 43 | | return result switch |
| | 0 | 44 | | { |
| | 0 | 45 | | string strategyId => await catalog.GetAsync(strategyId, cancellationToken), |
| | 0 | 46 | | IResilienceStrategy strategy => strategy, |
| | 0 | 47 | | ExpandoObject expando => expando.ConvertTo<IResilienceStrategy>(new(strategySerializer.SerializerOptions)), |
| | 0 | 48 | | _ => null |
| | 0 | 49 | | }; |
| | 0 | 50 | | } |
| | | 51 | | } |