Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where filter does not handle truthy values correctly #620

Open
williamb1024 opened this issue Jan 25, 2024 · 2 comments
Open

Where filter does not handle truthy values correctly #620

williamb1024 opened this issue Jan 25, 2024 · 2 comments
Labels

Comments

@williamb1024
Copy link

When using the where filter with a single argument, the filter does not perform truthy comparisons. The equality comparison is performed with the property value controlling.

if (itemValue.Equals(targetValue))

Switching itemValue and targetValue, such that targetValue is controlling would result in truthy comparisons.

@MoazAlkharfan
Copy link

Here's a workaround with adding a new filter named twhere

Usage twhere instead of where:

{% assign filteredItems = items | twhere: "property" %}

Register in the filters:

builder.AddFluid(x => {
    x.TemplateOptions.Filters.AddFilter(WhereFilter.Name, WhereFilter.Where);
});

Filter implementation with workaround:

internal static class WhereFilter
{
    internal const string Name = "twhere";

    // Track issue: https://github.com/sebastienros/fluid/issues/620
    internal static async ValueTask<FluidValue> Where(FluidValue input, FilterArguments arguments, TemplateContext context)
    {
        if (input.Type != FluidValues.Array)
        {
            return input;
        }

        // First argument is the property name to match
        var member = arguments.At(0).ToStringValue();

        // Second argument is the value to match, or 'true' if none is defined
        var targetValue = arguments.At(1).Or(BooleanValue.True);

        var list = new List<FluidValue>();

        foreach (var item in input.Enumerate(context))
        {
            var itemValue = await item.GetValueAsync(member, context);

            if (itemValue.Equals(targetValue) ||
                // Issue workaround for equality inconsistencies: to check if the other equals self
                targetValue.Equals(itemValue))
            {
                list.Add(item);
            }
        }

        return new ArrayValue(list);
    }
}

@sebastienros
Copy link
Owner

So instead of comparing to true we actually need to handle the single argument separately and convert it using ToBooleanValue(). Can someone create a PR and a test for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants