On Friday, July 14, 2017 at 9:52:37 AM UTC-5, Mariusz Gronczewski wrote: > > Hi, > > I've been slowly converting old 3.x codebase (which has seen days of > 0.24...) to 4.x and there is a lot of following pattern used for hashes:' > > $hash = { > "some" => "defaults" > } > > if $thing_one == "is_true" { > $hash["option1"] = $thing_one, > } > > if $thing_two == "is_something" { > $hash["option2"] = "something" > } > else { > $hash["option2"] = "something else" > } > > etc. Now with immutable hashes I'm forced either to do:' > > $hash1 = { > "some" => "defaults" > } > > if $thing_one == "is_true" { > $hash2 = { > "option1" => $thing_one, > } > } > else { $hash2 = {} } > > > if $thing_two == "is_something" { > $hash3 = { > "option2" => "something" > } > } > else { > $hash3 = { > "option2" => "something else" > } > $hash4 = merge($hash1, $hash2, $hash3, $hash3) > > or go the hacky route and go to erb and back to use ruby language to do it. > >
> Am I missing something here ? is there a better way to do it? > Well, the overall approach seems a bit questionable to me. You've made the question too generic for me to offer any specific ways to alter your code to avoid hash use altogether, but in some cases that may be the best route. You may also find that you can get a lot of mileage out of converting some of your code base to rely on Hiera for data, instead of building hashes inside a class and passing them around. As for the actual code you presented, however, yes, there is a better way to do that specific thing, one that works in every Puppet version from 5.0 back to 2.6: $hash = { 'some' => 'defaults', 'option1' => ( $thing_one ? { 'is_true' => $thing_one, default => undef } ), 'option2' => ( $thing_two ? { 'is_something' => 'something', default => 'something else' } ) } That relies on selectors, a longtime Puppet language feature resembling a turbocharged ternary operator, and also on the trick of setting a value to undef, which has an effect equivalent to not setting it at all. No multiple hashes or hash merging are required, and it's concise and fairly readable, too. > We have a lot of "get a hash, munge it, feed it to either puppet resource > or to external config" pattern used in the code and immutability so far > doesn't seem like a very useful property > > If "munge it" means more than "add new entries" then I'm inclined to suggest scrapping the infected code altogether. In particular, if hashes are used to try to work around the immutability of Puppet variables then I have no regard for the code at all. Nevertheless, you seem already to be adept with the merge() function, and I'm sure you can see how that could be combined with the above approach to forming hash values in order to perform a wide variety of munging operations. John -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/fdc6a180-ecf0-45c0-861b-0f62fcca03de%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.