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

Fix template expand level 2 hash support for non-string objects #499

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/addressable/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -896,19 +896,16 @@ def join_values(operator, return_value)
#
# @return [Hash, Array, String] The normalized values
def normalize_value(value)
unless value.is_a?(Hash)
value = value.respond_to?(:to_ary) ? value.to_ary : value.to_str
end

# Handle unicode normalization
if value.kind_of?(Array)
value.map! { |val| normalize_value(val) }
if value.respond_to?(:to_ary)
value.to_ary.map! { |val| normalize_value(val) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I just simplified a bit the code, treating all array-like object in the same place instead of having the first like (900) to convert first.

elsif value.kind_of?(Hash)
value = value.inject({}) { |acc, (k, v)|
acc[normalize_value(k)] = normalize_value(v)
acc
}
else
value = value.to_s if !value.kind_of?(String)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is the fix basically, restoring the to_s behavior which I accidentally changed for to_str when I modified this method to make it recursive.

if value.encoding != Encoding::UTF_8
value = value.dup.force_encoding(Encoding::UTF_8)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/addressable/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
:path => "/foo/bar",
:semi => ";",
:list => %w(red green blue),
:keys => {"semi" => ';', "dot" => '.', "comma" => ','}
:keys => {"semi" => ';', "dot" => '.', :comma => ','}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this specs are weird to write (you need to write a big array of expected outputs in different orders, probably legacy ruby 1.8 stuff when hash where unordered). So I just modified existing specs here to make it simple.

I'll probably make another PR to simplify those specs after I confirm it was only for ruby 1.8.

}
}
context "Expansion with value modifiers" do
Expand Down Expand Up @@ -404,7 +404,7 @@
{
:var => "value",
:semi => ";",
:year => %w(1965 2000 2012),
:year => [1965, 2000, 2012],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this was a good existing example to test integers too 👍

:dom => %w(example com)
}
}
Expand Down Expand Up @@ -437,7 +437,7 @@
:base => "http://example.com/home/",
:path => "/foo/bar",
:list => ["red", "green", "blue"],
:keys => {"semi" => ";","dot" => ".","comma" => ","},
:keys => {"semi" => ";","dot" => ".",:comma => ","},
:v => "6",
:x => "1024",
:y => "768",
Expand Down