Okay, I see. I can't add new keys while I am iterating, which makes sense.
So, I need to create a new hash and then do a .merge when I'm done.
On Wednesday, December 10, 2014 1:26:13 PM UTC-7, Geoffrey Gardella wrote:
>
> Hi John,
> thanks for the pointer. That has gotten me a bit farther. I'm running into
> a new set of problems. The keys of a hash are frozen, so doing:
>
> munge do |value_hash|
> value_hash.inject(value_hash) do |memo,(key,value)|
> key.gsub!(/_/, '-')
> memo
> end
> end
> Gives me:
>
> Munging failed for value {...} in class properties: can't modify frozen
> String
>
> I see you are setting a new key=>value pair (and I guess I'd delete the
> old), but when I try that I get:
>
> Munging failed for value {...} in class properties: can't add a new key
> into hash during iteration
>
> I can't imagine this is the first time somebody has tried this, but
> haven't found anything in the code base.
>
> Geoffrey
>
> On Wednesday, December 3, 2014 1:39:32 PM UTC-7, John Bollinger wrote:
>>
>>
>>
>> On Tuesday, December 2, 2014 4:53:42 PM UTC-6, Geoffrey Gardella wrote:
>>>
>>> Hey Josh, thanks for the reply.
>>>
>>> I'm still struggling with this. I have a hash of the values passed in
>>> from the manifest with the keys including '_' which need to be re-written.
>>>
>>> properties => {'john_wayne' => '1', 'liberty_valance' => '0', ...}
>>>
>>> this needs to become:
>>>
>>> properties => {'john-wayne' => '1', 'liberty-valance' => '0', ...}
>>>
>>> I believe this is where they are defined in my type:
>>>
>>> newproperty(:properties) do
>>> desc "A hash table of propname=propvalue entries to apply to the link"
>>> end
>>>
>>> So, I should write a method in the type to re-write the keys? Something
>>> like (forgive my ruby too):
>>>
>>> def modify_keys(h)
>>> h.keys.each do |k|
>>> if(k.include? '_') then
>>> h[k.gsub(/_/, '-')] = h[k]
>>> h.delete(k)
>>> end
>>> end
>>> end
>>>
>>> Can I add a method like this to my property? How is such a method then
>>> called in the provider?
>>>
>>>
>>
>> It looks like you want to use the munge hook. Though the docs
>> <https://docs.puppetlabs.com/guides/custom_types.html> are a bit
>> unclear, I think it works for property definitions (not just for parameter
>> definitions). You would put something like this inside your property
>> definition:
>>
>> munge do |value|
>> value.inject({}) do |xlated, kv|
>> k, v = kv
>> xlated[k.gsub(/_/, '-')] = v
>> end
>> end
>>
>>
>> Note that that assumes the value is a hash; you may want to use the
>> validation hook as well to ensure that (or else munge more flexibly).
>>
>>
>> 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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-dev/7c575bd1-ef44-4bd3-abf3-b223505ec8f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.