Skip to content

Commit

Permalink
Use VariableLookup instead of dot notation
Browse files Browse the repository at this point in the history
  • Loading branch information
karreiro committed Jan 17, 2025
1 parent 6372289 commit e429c94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,10 @@ def fetch_property(drop, property_or_keys)

return value if !value.nil? || !property_or_keys.is_a?(String)

keys = property_or_keys.split('.')
keys.reduce(drop) do |drop, key|
drop.respond_to?(:[]) ? drop[key] : drop
end
variable_lookup = Liquid::VariableLookup.parse("drop.#{property_or_keys}")
variable_lookup.evaluate(
Liquid::Context.new("drop" => drop),
)
end

def raise_property_error(property)
Expand Down
24 changes: 24 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,30 @@ def test_sum_with_deep_enumerables
assert_template_result(expected_output, template, { "products" => TestDeepEnumerable.new })
end

def test_access_arrays
template = <<~LIQUID
{{- products | map: 'colors[0]["name"]' | join: ", " -}}
LIQUID
expected_output = "red, red, blue"

assert_template_result(expected_output, template, {
"products" => [
{ "title" => { "content" => "Pro goggles" }, "colors" => [{ "name" => "red", "hex" => "#ff0000" }] },
{ "title" => { "content" => "Thermal gloves" }, "colors" => [{ "name" => "red", "hex" => "#ff0000" }] },
{ "title" => { "content" => "Alpine jacket" }, "colors" => [{ "name" => "blue", "hex" => "#0000ff" }] },
],
})
end

def test_access_lookup_with_brackets
template = <<~LIQUID
{{- products | map: 'title["content"]' | join: ', ' -}}
LIQUID
expected_output = "Pro goggles, Thermal gloves, Alpine jacket, Mountain boots, Safety helmet"

assert_template_result(expected_output, template, { "products" => TestDeepEnumerable.new })
end

private

def with_timezone(tz)
Expand Down

0 comments on commit e429c94

Please sign in to comment.