< Summary

Information
Class: Elsa.Expressions.Liquid.Services.LiquidTemplateManager
Assembly: Elsa.Expressions.Liquid
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Liquid/Services/LiquidTemplateManager.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 39
Coverable lines: 39
Total lines: 80
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
RenderAsync()0%620%
GetCachedTemplate(...)0%620%
Validate(...)100%210%
TryParse(...)100%210%
CreateTemplateContextAsync()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Liquid/Services/LiquidTemplateManager.cs

#LineLine coverage
 1using Elsa.Expressions.Models;
 2using Elsa.Extensions;
 3using Elsa.Expressions.Liquid.Contracts;
 4using Elsa.Expressions.Liquid.Notifications;
 5using Elsa.Expressions.Liquid.Options;
 6using Elsa.Mediator.Contracts;
 7using Fluid;
 8using Microsoft.Extensions.Caching.Memory;
 9using Microsoft.Extensions.Options;
 10
 11namespace Elsa.Expressions.Liquid.Services;
 12
 13/// <inheritdoc />
 14public class LiquidTemplateManager : ILiquidTemplateManager
 15{
 16    private readonly LiquidParser _parser;
 17    private readonly IMemoryCache _memoryCache;
 18    private readonly INotificationSender _notificationSender;
 19    private readonly FluidOptions _options;
 20
 21    /// <summary>
 22    /// Constructor.
 23    /// </summary>
 024    public LiquidTemplateManager(LiquidParser parser, IMemoryCache memoryCache, INotificationSender notificationSender, 
 25    {
 026        _parser = parser;
 027        _memoryCache = memoryCache;
 028        _notificationSender = notificationSender;
 029        _options = options.Value;
 030    }
 31
 32    /// <inheritdoc />
 33    public async Task<string?> RenderAsync(string template, ExpressionExecutionContext expressionExecutionContext, Cance
 34    {
 035        if (string.IsNullOrWhiteSpace(template))
 036            return default!;
 37
 038        var result = GetCachedTemplate(template);
 039        var templateContext = await CreateTemplateContextAsync(expressionExecutionContext, cancellationToken);
 040        var encoder = _options.Encoder;
 041        templateContext.AddFilters(_options, expressionExecutionContext.ServiceProvider);
 42
 043        return await result.RenderAsync(templateContext, encoder);
 044    }
 45
 46    private IFluidTemplate GetCachedTemplate(string source)
 47    {
 048        var result = _memoryCache.GetOrCreate(
 049            source,
 050            e =>
 051            {
 052                if (!TryParse(source, out var parsed, out var error))
 053                {
 054                    error = "{% raw %}\n" + error + "\n{% endraw %}";
 055                    TryParse(error, out parsed, out error);
 056
 057                    e.SetSlidingExpiration(TimeSpan.FromMilliseconds(100));
 058                    return parsed;
 059                }
 060
 061                // TODO: add signal based cache invalidation.
 062                e.SetSlidingExpiration(TimeSpan.FromSeconds(30));
 063                return parsed;
 064            });
 065        return result!;
 66    }
 67
 68    /// <inheritdoc />
 069    public bool Validate(string template, out string error) => TryParse(template, out _, out error);
 70
 071    private bool TryParse(string template, out IFluidTemplate result, out string error) => _parser.TryParse(template, ou
 72
 73    private async Task<TemplateContext> CreateTemplateContextAsync(ExpressionExecutionContext expressionExecutionContext
 74    {
 075        var context = new TemplateContext(expressionExecutionContext, new TemplateOptions());
 076        await _notificationSender.SendAsync(new RenderingLiquidTemplate(context, expressionExecutionContext), cancellati
 077        context.SetValue("ExpressionExecutionContext", expressionExecutionContext);
 078        return context;
 079    }
 80}