On 10/08/17 20:48, ggun wrote:
> Thanks, but the main point to merge the hash is that the sub hash header
> value is not fixed in the example I have mentioned as
> esa-user-profile-service or esa-group-service. It can be anything. I
> need to make the code free from using direct Key value .
>
So 'was_dmgr_data' is a hash of unknown hashes. Did we understand
correctly that the "common part" is some kind of default?
Can you change the original structure, or are you stuck with this design?
In puppet 5.0.0 there is a function named 'tree_each' that can be used
together with 'map()' and the result of that fed into a Hash.new with an
option of 'tree', or 'tree_hash'. This is a very powerful function but
requires a little bit of setup.
I also included a simpler variant using the reduce() function.
That works well here since the task is to map one level of data in a
known way, so using tree_each is a bit of an overkill.
Using tree_each
===============
---------example.pp
# Sample data for illustration
$hash = {
common => { x => 10, y => 20},
was_data => {
thing => {a => 'in thing'},
other_thing => {a => 'in other thing', x => 1000 }
}
}
# The common to part to merge as defaults
#
$common = $hash[common]
# Map the tree to a flat structure. Skip the root
# (we want data one level down),
# and skip all values as they are included in the hash containers (and
# we do not need to transform them)
#
$flat_tree = $hash[was_data]
.tree_each({include_root => false, include_values => false} )
.map |$entry| {
# assign the path and value in $entry to separate variables
[$path, $val] = $entry
# only map top level keys (the path has length == 1) to a new value
# and make all other entries undef.
# The mapped entries is the common part with the specific data
# merged ($common + $val)
#
if $path =~ Array[1,1] {
[ $path, $common + $val ]
}
}.filter |$x| { $x != undef } # filter out the undef entries
# create a new hash and notice it
notice Hash($flat_tree, 'hash_tree')
--------------<eof>
When running this the output would be:
{thing => {x => 10, y => 20, a => in thing},
other_thing => {x => 1000, y => 20, a => in other thing}}
As you can see the result is a hash with the keys under 'was_data'
with the expected merged data.
Using reduce
============
If you are on a version before Puppet 5.0.0 it can be done with the
reduce function.
-------------example.pp
# Sample data for illustration
$hash = {
common => { x => 10, y => 20},
was_data => {
thing => {a => 'in thing'},
other_thing => {a => 'in other thing', x => 1000 }
}
}
$common = $hash[common]
$mapped = $hash[was_data].reduce({}) |$memo, $entry| {
[$k, $v] = $entry
$memo + { $k => $common + $v}
}
notice $mapped
-------------<eof>
I hope these two examples help.
Regards
- henrik
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
puppet-dev+...@googlegroups.com
> <mailto:
puppet-dev+...@googlegroups.com>.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/puppet-dev/a7e5b925-2c85-4cb6-beaa-892ca1c1c80e%40googlegroups.com
> <
https://groups.google.com/d/msgid/puppet-dev/a7e5b925-2c85-4cb6-beaa-892ca1c1c80e%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit
https://groups.google.com/d/optout.