-
Notifications
You must be signed in to change notification settings - Fork 583
Add a new function "try_get_value" #513
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
Conversation
@@ -1023,6 +1023,40 @@ Versions | Puppet 2.6 | Puppet 2.7 | Puppet 3.x | Puppet 4.x | | |||
|
|||
**stdlib 5.x**: When released, stdlib 5.x will drop support for Puppet 2.7.x. Please see [this discussion](https://github.com/puppetlabs/puppetlabs-stdlib/pull/176#issuecomment-30251414). | |||
|
|||
#### `fetch` |
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.
Please put this in the right alphabetical order of the function list.
Hi, thank you for your contribution! I've added a few inline tests about possible improvements for the code. Cheers, David |
Updated request with fixes |
} | ||
} | ||
is_expected.to run.with_params(data, 'a/b/c/d', 'default').and_return('default') | ||
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.
You can shorten your tests even more by sharing data between examples:
context 'with nested hashes' do
let(:data) { { 'a' => { 'b' => 'c' } } }
it 'should return the default value if the path is not found' do
is_expected.to run.with_params(data, 'missing', 'default').and_return('default')
end
it 'should return the default value if the path is too long' do
is_expected.to run.with_params(data, 'a/b/c/d', 'default').and_return('default')
end
end
These changes look great. I've added another small comment to the tests. We've been discussing names internally and came up with |
* Extracts a value from a deeply-nested data structure * Returns default if a value could not be extracted
Add a new function "try_get_value"
Thank you for your contribution and patience! |
Extracts a value from a deeply-nested data structure
$data = {
'a' => {
'b' => [
'b1',
'b2',
'b3',
]
}
}
$value = fetch($data, 'a/b/2')
=> $value = 'b3'