2021-03-17 12:07:14 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Dynamic;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Elsa.Models;
|
|
|
|
|
|
using Elsa.Scripting.Liquid.Helpers;
|
|
|
|
|
|
using Elsa.Scripting.Liquid.Messages;
|
|
|
|
|
|
using Elsa.Services.Models;
|
|
|
|
|
|
using Fluid;
|
|
|
|
|
|
using Fluid.Values;
|
|
|
|
|
|
using MediatR;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Scripting.Liquid.Handlers
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ConfigureLiquidEngine : INotificationHandler<EvaluatingLiquidExpression>
|
|
|
|
|
|
{
|
|
|
|
|
|
public Task Handle(EvaluatingLiquidExpression notification, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
var context = notification.TemplateContext;
|
2021-04-07 12:48:30 +00:00
|
|
|
|
var options = context.Options;
|
|
|
|
|
|
|
|
|
|
|
|
options.MemberAccessStrategy.Register<ExpandoObject>();
|
|
|
|
|
|
options.MemberAccessStrategy.Register<JObject>();
|
2021-04-07 19:07:19 +00:00
|
|
|
|
options.MemberAccessStrategy.Register<JValue>(o => o.Value);
|
2021-03-17 12:07:14 +00:00
|
|
|
|
|
2021-04-07 12:48:30 +00:00
|
|
|
|
options.MemberAccessStrategy.Register<LiquidActivityModel>();
|
|
|
|
|
|
options.MemberAccessStrategy.Register<LiquidPropertyAccessor, FluidValue>((x, name) => x.GetValueAsync(name));
|
|
|
|
|
|
options.MemberAccessStrategy.Register<ActivityExecutionContext, FluidValue>("Input", x => ToFluidValue(x.Input, options));
|
|
|
|
|
|
options.MemberAccessStrategy.Register<ActivityExecutionContext, FluidValue>("CorrelationId", x => ToFluidValue(x.CorrelationId, options));
|
|
|
|
|
|
options.MemberAccessStrategy.Register<ActivityExecutionContext, LiquidPropertyAccessor>("Variables", x => new LiquidPropertyAccessor(name => ToFluidValue(x.WorkflowExecutionContext.GetMergedVariables(), name, options)));
|
|
|
|
|
|
options.MemberAccessStrategy.Register<ActivityExecutionContext, LiquidPropertyAccessor>("Activities", x => new LiquidPropertyAccessor(name => ToFluidValue(GetActivityModelAsync(x, name), options)!));
|
|
|
|
|
|
options.MemberAccessStrategy.Register<LiquidActivityModel, object?>("Output", GetActivityOutput);
|
|
|
|
|
|
options.MemberAccessStrategy.Register<LiquidObjectAccessor<object>, object>((x, name) => x.GetValueAsync(name));
|
|
|
|
|
|
options.MemberAccessStrategy.Register<ExpandoObject, object>((x, name) => ((IDictionary<string, object>) x)[name]);
|
|
|
|
|
|
options.MemberAccessStrategy.Register<JObject, object?>((source, name) => source[name]);
|
2021-03-17 12:07:14 +00:00
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-07 12:48:30 +00:00
|
|
|
|
private Task<FluidValue> ToFluidValue(object? input, TemplateOptions options) => Task.FromResult(FluidValue.Create(input, options));
|
|
|
|
|
|
private Task<FluidValue?> ToFluidValue(Variables dictionary, string key, TemplateOptions options) => Task.FromResult(!dictionary.Has(key) ? default : FluidValue.Create(dictionary.Get(key), options));
|
2021-03-17 12:07:14 +00:00
|
|
|
|
private LiquidActivityModel GetActivityModelAsync(ActivityExecutionContext context, string name) => new(context, name);
|
|
|
|
|
|
|
|
|
|
|
|
private Task<object?> GetActivityOutput(LiquidActivityModel activityModel)
|
|
|
|
|
|
{
|
|
|
|
|
|
var output = activityModel.ActivityExecutionContext.GetOutputFrom(activityModel.ActivityName);
|
|
|
|
|
|
return Task.FromResult(output);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|