-
Notifications
You must be signed in to change notification settings - Fork 580
Add dig function #573
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
Add dig function #573
Conversation
This already exists as |
That being said, it might make sense to extend |
with |
how would you feel about me reimplementing |
i've dropped some test cases from try_get_value but it remains soft-backwards-compatible, meaning it will produce an error instead of unexpected value in some cases. i don't think it's good idea to tolerate lookup functions being called on values that are not indexable. |
'b2', | ||
'b3' ]}} | ||
|
||
$value = try_get_value($data, ['a', 'b', '2'], 'not_found') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be dig
|
||
it 'should be able to return a single value' do | ||
is_expected.to run.with_params('test').and_return('test') | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should actually stay, to check for backwards compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the purpose of this function is to do a lookup on nested data structure. passing string where nested data structure is expected is just wrong and dig() will fail explicitly in this case.
Thanks for the work. Could you squash the commits as well. |
@tphoney do you mean squash and push --force into same branch? |
@keymone Yep! I usually do something like: http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html , then push to the same branch to update this PR with your changes. Also, would you be able to add some info in the readme for your new function? Thanks! |
@HelenCampbell @tphoney done |
f0559d5
to
1038fe5
Compare
The test cases that @hunner marked to be kept are important. The try_get_value function should still be able to function with a single parameter, otherwise it might affect how current users are using try_get_value. |
OR another possible solution to that would be to keep the try_get_value function alone, and add dig as an additional function. The worry is that the implementation in this PR may break some existing setups. |
@tphoney try_get_value promotes bad coding practices by being tolerant to bad input data. if somebody's codebase depends on bad coding practices, trying to be backward compatible with that will only make everything worse. @HelenCampbell i could revert my changes to try_get_value, but breeding duplicate functions with subtly different functionality is a recipe for disaster. i'd love to see stdlib as clean and strict as possible. |
Deprecates #try_get_value()
@hunner i've reverted spec changes and preserved original |
@keymone Thanks! I agree that bad coding practices should not be promoted, but hopefully the deprecation will get people to migrate to dig and correct their practices. |
Makes deep lookup in nested hashes(and/or arrays) easier.
Usage:
Returns
nil
or optionaldefault
arg if at any point in the chain key couldn't be looked up.