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

Conversation

jarthod
Copy link
Contributor

@jarthod jarthod commented Apr 4, 2023

Here's the fix for #498

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.

@@ -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.

@@ -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 👍

@dentarg dentarg merged commit c00d58b into sporkmonger:main Apr 4, 2023
33 checks passed
@jarthod jarthod deleted the fix-template-level2 branch April 6, 2023 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants