Skip to content

Commit 983f652

Browse files
committed
(PUP-9323) Add a level of indirection during unmunge
When the agent converts the resource catalog (downloaded from the server) to a RAL catalog, it metaprograms a new class for each type of resource, e.g. `Puppet::Type::File`, and corresponding classes for each parameter/property, e.g. `Puppet::Type::File::Mode`. The conversion to RAL also triggers calls to `munge`, `validate` and `unmunge` for each parameter. This gives the parameter a chance to convert from the DSL form to an internal/canonical form, e.g. "owner => root" and "owner => 0" are equivalent. However, if the parameter's value is deferred, then we won't know its value until later when the resource is evaluated. So we need to delay calls to `munge`, `validate` and `unmunge` until after the deferred value is known. It is possible to intercept calls to `munge` and `validate`, because there is a level of indirection between the transaction and the munge/validate methods implemented in a custom parameter. For example, the transaction sets the desired value on the property[1], which triggers a call to the `munge` instance method. If the custom type has defined a `munge { |value| ...}` DSL method, then it metaprograms an `munge_unsafe` method[2], which is called by the `munge` instance method[3]. However, the `unmunge` logic doesn't have this level of indirection -- the transaction calls the custom `unmunge` method directly. This commit introduces a level of indirection to `unmunge` following the same pattern used by `munge`. [1] https://github.com/puppetlabs/puppet/blob/7.16.0/lib/puppet/property.rb#L551 [2] https://github.com/puppetlabs/puppet/blob/7.16.0/lib/puppet/parameter.rb#L176 [3] https://github.com/puppetlabs/puppet/blob/7.16.0/lib/puppet/parameter.rb#L430
1 parent f58989b commit 983f652

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/puppet/parameter.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ def munge(&block)
177177
end
178178

179179
# @overload unmunge {|| ... }
180-
# Defines an optional method used to convert the parameter value to DSL/string form from an internal form.
180+
# Defines an optional method used to convert the parameter value from internal form to DSL/string form.
181181
# If an `unmunge` method is not defined, the internal form is used.
182182
# @see munge
183-
# @note This adds a method with the name `unmunge` in the created parameter class.
183+
# @note This adds a method with the name `unsafe_unmunge` in the created parameter class.
184184
# @dsl type
185185
# @api public
186186
#
187187
def unmunge(&block)
188-
define_method(:unmunge, &block)
188+
define_method(:unsafe_unmunge, &block)
189189
end
190190

191191
# Sets a marker indicating that this parameter is the _namevar_ (unique identifier) of the type
@@ -415,10 +415,19 @@ def unsafe_munge(value)
415415
# @return [Object] the unmunged value
416416
#
417417
def unmunge(value)
418+
unsafe_unmunge(value)
419+
end
420+
421+
# This is the default implementation of `unmunge` that simply produces the value (if it is valid).
422+
# The DSL method {unmunge} should be used to define an overriding method if unmunging is required.
423+
#
424+
# @api private
425+
#
426+
def unsafe_unmunge(value)
418427
value
419428
end
420429

421-
# Munges the value to internal form.
430+
# Munges the value from DSL form to internal form.
422431
# This implementation of `munge` provides exception handling around the specified munging of this parameter.
423432
# @note This method should not be overridden. Use the DSL method {munge} to define a munging method
424433
# if required.

0 commit comments

Comments
 (0)