< Summary

Information
Class: Elsa.Expressions.Liquid.Filters.Base64Filter
Assembly: Elsa.Expressions.Liquid
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Liquid/Filters/Base64Filter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 42
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ProcessAsync(...)0%620%
InputToString(...)0%156120%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Liquid/Filters/Base64Filter.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text;
 3using System.Text.Json;
 4using Elsa.Expressions.Liquid.Contracts;
 5using Fluid;
 6using Fluid.Values;
 7
 8namespace Elsa.Expressions.Liquid.Filters;
 9
 10/// <summary>
 11/// A liquid filter that converts a value into a base 64 string representation.
 12/// </summary>
 13public class Base64Filter : ILiquidFilter
 14{
 15    /// <inheritdoc />
 16    public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext context)
 17    {
 018        var text = InputToString(input, context);
 19
 020        if (text == null)
 021            return new ValueTask<FluidValue>(NilValue.Instance);
 22
 023        var bytes = Encoding.UTF8.GetBytes(text);
 024        var base64 = Convert.ToBase64String(bytes);
 25
 026        return new ValueTask<FluidValue>(new StringValue(base64));
 27    }
 28
 29    private static string? InputToString(FluidValue input, TemplateContext context)
 30    {
 031        return input.Type switch
 032        {
 033            FluidValues.Array => JsonSerializer.Serialize(input.Enumerate(context).Select(o => o.ToObjectValue())),
 034            FluidValues.Boolean => input.ToBooleanValue().ToString(),
 035            FluidValues.Nil => null,
 036            FluidValues.Number => input.ToNumberValue().ToString(CultureInfo.InvariantCulture),
 037            FluidValues.DateTime or FluidValues.Dictionary or FluidValues.Object => JsonSerializer.Serialize(input.ToObj
 038            FluidValues.String => input.ToStringValue(),
 039            _ => throw new ArgumentOutOfRangeException(nameof(input), "Unrecognized FluidValue")
 040        };
 41    }
 42}