cobalt-org / liquid-rust

Liquid templating for Rust
docs.rs/liquid
MIT License
467 stars 79 forks source link

EmptyDrop and Object indexing #477

Open rydrman opened 1 year ago

rydrman commented 1 year ago

liquid-rust version: 0.26.0 rust version: 1.63.0 OS: linux

I've been referencing this page of the liquid docs, and my understanding of the nil and EmptyDrop type is that both of the following should be possible:

{% assign var = value | default: 42 %}
{% assign var = data.value | default: 42 %}

In using liquid-rust, however, I'm finding that the second case fails for me if data.value does not already exist. I get an error like:

liquid: Unknown index
  with:
    variable=data
    requested index=value
    available indexes=other, more_data
from: {% assign var = data["value"] | default : 42%}

I was looking for an existing issue for this but didn't find anything, am I missing something?

epage commented 1 year ago

Its been a while since I've dug into the meaning of this on both sides but I think we were aiming for strict_variables and strict_filters exclusively

From https://github.com/Shopify/liquid

By default, the renderer doesn't raise or in any other way notify you if some variables or filters are missing, i.e. not passed to the render method. You can improve this situation by passing strict_variables: true and/or strict_filters: true options to the render method. When one of these options is set to true, all errors about undefined variables and undefined filters will be stored in errors array of a Liquid::Template instance. Here are some examples:

rydrman commented 1 year ago

I think that's a fair goal for values leaving a filter chain, but it seems like the inconsistent behaviour between the two cases above is surprising. I'm trying to figure out if there's even a way to handle this case in the template, because as far as I can tell there's no way to check if some value is set without getting this error

epage commented 1 year ago

it seems like the inconsistent behaviour between the two cases above is surprisin

Yes, I had missed the inconsistency in my first read through. Thats something that needs looking into.

I'm trying to figure out if there's even a way to handle this case in the template, because as far as I can tell there's no way to check if some value is set without getting this error

You can do {% if value %} (test) or `{% data contains "value" %} (test).

rydrman commented 1 year ago

Thanks, if you have any inclinations on where I can poke at this issue in the codebase I'd be happy to see if I can resolve the inconsistency

rydrman commented 1 year ago

Any thoughts or follow-up on this one?

epage commented 12 months ago

The question for any of this is what does Ruby's implementation do with those strict flags set. Any discussion or PR should be focused on that.

rydrman commented 12 months ago

Okay, I've done a repl test against the latest ruby gem and found that filters are not allowed to start with undefined variables when strict_variables is set - which means the former case is incorrect for this implementation, ie {% assign var = value | default: 42 %} does not currently error, but should