Skip to content

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

Merged
merged 1 commit into from
Sep 2, 2015
Merged

Conversation

dmitryilyin
Copy link

Extracts a value from a deeply-nested data structure

$data = {
'a' => {
'b' => [
'b1',
'b2',
'b3',
]
}
}

$value = fetch($data, 'a/b/2')
=> $value = 'b3'

@@ -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`
Copy link
Contributor

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.

@DavidS
Copy link
Contributor

DavidS commented Aug 28, 2015

Hi, thank you for your contribution! I've added a few inline tests about possible improvements for the code.

Cheers, David

@dmitryilyin dmitryilyin changed the title Add a new function "fetch" Add a new function "getpath" Sep 1, 2015
@dmitryilyin
Copy link
Author

Updated request with fixes

}
}
is_expected.to run.with_params(data, 'a/b/c/d', 'default').and_return('default')
end
Copy link
Contributor

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

@DavidS
Copy link
Contributor

DavidS commented Sep 1, 2015

These changes look great. I've added another small comment to the tests.

We've been discussing names internally and came up with try_get_value to avoid confusion with filesystem paths and to indicate that it will not throw any exceptions.

@dmitryilyin dmitryilyin changed the title Add a new function "getpath" Add a new function "try_get_value" Sep 1, 2015
* Extracts a value from a deeply-nested data structure
* Returns default if a value could not be extracted
DavidS added a commit that referenced this pull request Sep 2, 2015
Add a new function "try_get_value"
@DavidS DavidS merged commit 64267eb into puppetlabs:master Sep 2, 2015
@DavidS
Copy link
Contributor

DavidS commented Sep 2, 2015

Thank you for your contribution and patience!

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.

3 participants