| | | 1 | | using System.Dynamic; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Expressions.Liquid.Helpers; |
| | | 5 | | using Elsa.Expressions.Liquid.Notifications; |
| | | 6 | | using Elsa.Expressions.Liquid.Options; |
| | | 7 | | using Elsa.Mediator.Contracts; |
| | | 8 | | using Elsa.Workflows.Management.Options; |
| | | 9 | | using Elsa.Workflows.Memory; |
| | | 10 | | using Fluid; |
| | | 11 | | using Fluid.Values; |
| | | 12 | | using Microsoft.Extensions.Configuration; |
| | | 13 | | using Microsoft.Extensions.Options; |
| | | 14 | | |
| | | 15 | | namespace Elsa.Expressions.Liquid.Handlers; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Configures the liquid templating engine before evaluating a liquid expression. |
| | | 19 | | /// </summary> |
| | | 20 | | internal class ConfigureLiquidEngine : INotificationHandler<RenderingLiquidTemplate> |
| | | 21 | | { |
| | | 22 | | private readonly IConfiguration _configuration; |
| | | 23 | | private readonly ManagementOptions _managementOptions; |
| | | 24 | | private readonly FluidOptions _fluidOptions; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Constructor. |
| | | 28 | | /// </summary> |
| | 319 | 29 | | public ConfigureLiquidEngine(IConfiguration configuration, IOptions<FluidOptions> fluidOptions, IOptions<ManagementO |
| | | 30 | | { |
| | 319 | 31 | | _configuration = configuration; |
| | 319 | 32 | | _managementOptions = managementOptions.Value; |
| | 319 | 33 | | _fluidOptions = fluidOptions.Value; |
| | 319 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public Task HandleAsync(RenderingLiquidTemplate notification, CancellationToken cancellationToken) |
| | | 38 | | { |
| | 0 | 39 | | var context = notification.TemplateContext; |
| | 0 | 40 | | var options = context.Options; |
| | 0 | 41 | | var memberAccessStrategy = options.MemberAccessStrategy; |
| | | 42 | | |
| | 0 | 43 | | memberAccessStrategy.Register<ExpandoObject>(); |
| | 0 | 44 | | memberAccessStrategy.Register<LiquidPropertyAccessor, FluidValue>((x, name) => x.GetValueAsync(name)); |
| | 0 | 45 | | memberAccessStrategy.Register<ExpandoObject, object>((x, name) => ((IDictionary<string, object>)x!)[name]); |
| | 0 | 46 | | memberAccessStrategy.Register<ExpressionExecutionContext, LiquidPropertyAccessor>("Variables", x => new LiquidPr |
| | 0 | 47 | | memberAccessStrategy.Register<ExpressionExecutionContext, LiquidPropertyAccessor>("Input", x => new LiquidProper |
| | 0 | 48 | | memberAccessStrategy.Register<ExpressionExecutionContext, string?>("CorrelationId", x => x.GetWorkflowExecutionC |
| | 0 | 49 | | memberAccessStrategy.Register<ExpressionExecutionContext, string>("WorkflowDefinitionId", x => x.GetWorkflowExec |
| | 0 | 50 | | memberAccessStrategy.Register<ExpressionExecutionContext, string>("WorkflowDefinitionVersionId", x => x.GetWorkf |
| | 0 | 51 | | memberAccessStrategy.Register<ExpressionExecutionContext, int>("WorkflowDefinitionVersion", x => x.GetWorkflowEx |
| | 0 | 52 | | memberAccessStrategy.Register<ExpressionExecutionContext, string>("WorkflowInstanceId", x => x.GetActivityExecut |
| | | 53 | | |
| | 0 | 54 | | if (_fluidOptions.AllowConfigurationAccess) |
| | | 55 | | { |
| | 0 | 56 | | memberAccessStrategy.Register<ExpressionExecutionContext, LiquidPropertyAccessor>("Configuration", x => new |
| | 0 | 57 | | memberAccessStrategy.Register<ConfigurationSectionWrapper, ConfigurationSectionWrapper?>((source, name) => s |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | // Register all variable types. |
| | 0 | 61 | | foreach (var variableDescriptor in _managementOptions.VariableDescriptors.Where(x => x.Type is { IsClass: true, |
| | 0 | 62 | | memberAccessStrategy.Register(variableDescriptor.Type); |
| | | 63 | | |
| | 0 | 64 | | return Task.CompletedTask; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | private ConfigurationSectionWrapper GetConfigurationValue(string name) => new(_configuration.GetSection(name)); |
| | 0 | 68 | | private Task<FluidValue> ToFluidValue(object? input, TemplateOptions options) => Task.FromResult(FluidValue.Create(i |
| | | 69 | | |
| | | 70 | | private Task<FluidValue> GetVariable(ExpressionExecutionContext context, string key, TemplateOptions options) |
| | | 71 | | { |
| | 0 | 72 | | var value = GetVariableInScope(context, key); |
| | 0 | 73 | | return Task.FromResult(value == null ? NilValue.Instance : FluidValue.Create(value, options)); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | private Task<FluidValue> GetInput(ExpressionExecutionContext context, string key, TemplateOptions options) |
| | | 77 | | { |
| | | 78 | | // First, check if the current activity has inputs |
| | 0 | 79 | | if (context.TryGetActivityExecutionContext(out var activityExecutionContext) && |
| | 0 | 80 | | activityExecutionContext.ActivityInput.TryGetValue(key, out var activityValue)) |
| | | 81 | | { |
| | 0 | 82 | | return Task.FromResult(activityValue == null ? NilValue.Instance : FluidValue.Create(activityValue, options) |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // Fall back to workflow inputs if activity inputs don't contain the key |
| | 0 | 86 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 0 | 87 | | var input = workflowExecutionContext.Input.TryGetValue(key, out var workflowValue) ? workflowValue : default; |
| | | 88 | | |
| | 0 | 89 | | return Task.FromResult(input == null ? NilValue.Instance : FluidValue.Create(workflowValue, options)); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private static object? GetVariableInScope(ExpressionExecutionContext context, string variableName) |
| | | 93 | | { |
| | 0 | 94 | | var q = from variable in context.EnumerateVariablesInScope() |
| | 0 | 95 | | where variable.Name == variableName |
| | 0 | 96 | | where variable.TryGet(context, out _) |
| | 0 | 97 | | select variable.Get(context); |
| | | 98 | | |
| | 0 | 99 | | return q.FirstOrDefault(); |
| | | 100 | | } |
| | | 101 | | } |