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

(FEAT) Ensure hash key casing methods work on arrays #15

Merged
merged 3 commits into from
Dec 3, 2019
Merged

(FEAT) Ensure hash key casing methods work on arrays #15

merged 3 commits into from
Dec 3, 2019

Conversation

michaeltlombardi
Copy link
Contributor

Prior to this commit the hash key casing methods, snake_case_hash_keys and pascal_case_hash_keys, only properly handled hashes, not hashes inside arrays or other objects.

Now, if passed an array, the methods will iterate over each item in the array, recursively casing any keys discovered in hashes. Additionally, they will not error if passed any objects which are not hashes or arrays; instead, they will return that object as-is.

@michaeltlombardi michaeltlombardi requested a review from a team as a code owner December 3, 2019 04:24
@michaeltlombardi
Copy link
Contributor Author

Added a maint commit to clarify the parameter names for the hash methods.

lib/pwsh/util.rb Outdated
if hash.is_a?(Hash)
hash.inject({}) do |memo, (k, v)| # rubocop:disable Style/EachWithObject
def symbolize_hash_keys(object)
if object.is_a?(Hash)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bit weird that this function has a different code pattern than the pascal_case and snake_case variants. Makes it hard to see whether they're all three doing the same or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

and if they're actually all the same shape, then you could replace them with a apply_key_mutator(f, o) function so that

def pascal_case_hash_keys(object)
  apply_key_mutator(&:pascal_case, o)
end

etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced with a private method.

Prior to this commit the hash key casing methods,
snake_case_hash_keys and pascal_case_hash_keys,
only properly handled hashes, not hashes inside
arrays or other objects.

Now, if passed an array, the methods will iterate
over each item in the array, recursively casing
any keys discovered in hashes. Additionally, they
will not error if passed any objects which are not
hashes or arrays; instead, they will return that
object as-is.
This commit standardizes the hash util methods on taking object
as a parameter as they handle more data types than just hashes.

It also conforms the hash util methods to use the same underlying
structure by utilizing a new private method, apply_key_mutator,
to do the heavy lifting. The other hash utility methods now create
a proc for the conforming of the keys and pass that to the mutator.
This commit updates the casing methods to handle symbols
if passed them - they will return a string if given a
string and a symbol if given a symbol, properly cased.

This does away with the weird requirement to cast the
keys to strings, case them, and resymbolize them in the
hash mutation procs.
@RandomNoun7 RandomNoun7 merged commit 0c3ea52 into puppetlabs:master Dec 3, 2019
@michaeltlombardi michaeltlombardi deleted the bugfix/master/casing_methods_arrays branch February 2, 2021 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants