diff --git a/lib/puppet/functions/mysql/normalise_and_deepmerge.rb b/lib/puppet/functions/mysql/normalise_and_deepmerge.rb index c2f5880..bb5f884 100644 --- a/lib/puppet/functions/mysql/normalise_and_deepmerge.rb +++ b/lib/puppet/functions/mysql/normalise_and_deepmerge.rb @@ -1,69 +1,74 @@ # frozen_string_literal: true -# @summary Recursively merges two or more hashes together, normalises keys with differing use of dashesh and underscores, -# then returns the resulting hash. +# @summary Recursively merges two or more hashes together, normalises keys with differing use of dashes and underscores. # # @example # $hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } } # $hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } } # $merged_hash = mysql::normalise_and_deepmerge($hash1, $hash2) # # The resulting hash is equivalent to: # # $merged_hash = { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } } # # - When there is a duplicate key that is a hash, they are recursively merged. # - When there is a duplicate key that is not a hash, the key in the rightmost hash will "win." # - When there are conficting uses of dashes and underscores in two keys (which mysql would otherwise equate), the rightmost style will win. # Puppet::Functions.create_function(:'mysql::normalise_and_deepmerge') do + # @param args + # Hash to be normalised + # + # @return hash + # The given hash normalised + # def normalise_and_deepmerge(*args) if args.length < 2 raise Puppet::ParseError, _('mysql::normalise_and_deepmerge(): wrong number of arguments (%{args_length}; must be at least 2)') % { args_length: args.length } end result = {} args.each do |arg| next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef # If the argument was not a hash, skip it. unless arg.is_a?(Hash) raise Puppet::ParseError, _('mysql::normalise_and_deepmerge: unexpected argument type %{arg_class}, only expects hash arguments.') % { args_class: args.class } end # We need to make a copy of the hash since it is frozen by puppet current = deep_copy(arg) # Now we have to traverse our hash assigning our non-hash values # to the matching keys in our result while following our hash values # and repeating the process. overlay(result, current) end result end def normalized?(hash, key) return true if hash.key?(key) return false unless %r{-|_}.match?(key) other_key = key.include?('-') ? key.tr('-', '_') : key.tr('_', '-') return false unless hash.key?(other_key) hash[key] = hash.delete(other_key) true end def overlay(hash1, hash2) hash2.each do |key, value| if normalized?(hash1, key) && value.is_a?(Hash) && hash1[key].is_a?(Hash) overlay(hash1[key], value) else hash1[key] = value end end end def deep_copy(inputhash) return inputhash unless inputhash.is_a? Hash hash = {} inputhash.each do |k, v| hash.store(k, deep_copy(v)) end hash end end diff --git a/manifests/client.pp b/manifests/client.pp index e1bd10e..402d30e 100644 --- a/manifests/client.pp +++ b/manifests/client.pp @@ -1,52 +1,48 @@ # @summary # Installs and configures the MySQL client. # # @example Install the MySQL client # class {'::mysql::client': # package_name => 'mysql-client', # package_ensure => 'present', # bindings_enable => true, # } # # @param bindings_enable # Whether to automatically install all bindings. Valid values are `true`, `false`. Default to `false`. # @param install_options # Array of install options for managed package resources. You must pass the appropriate options for the package manager. # @param package_ensure # Whether the MySQL package should be present, absent, or a specific version. Valid values are 'present', 'absent', or 'x.y.z'. # @param package_manage # Whether to manage the MySQL client package. Defaults to `true`. -# @param service_name -# The name of the MySQL server service. Defaults are OS dependent, defined in 'params.pp'. -# @param service_provider -# The provider to use to manage the service. For Ubuntu, defaults to 'upstart'; otherwise, default is undefined. # @param package_name # The name of the MySQL client package to install. # class mysql::client ( $bindings_enable = $mysql::params::bindings_enable, $install_options = undef, $package_ensure = $mysql::params::client_package_ensure, $package_manage = $mysql::params::client_package_manage, $package_name = $mysql::params::client_package_name, $package_provider = undef, $package_source = undef, ) inherits mysql::params { include 'mysql::client::install' if $bindings_enable { class { 'mysql::bindings': java_enable => true, perl_enable => true, php_enable => true, python_enable => true, ruby_enable => true, } } # Anchor pattern workaround to avoid resources of mysql::client::install to # "float off" outside mysql::client anchor { 'mysql::client::start': } -> Class['mysql::client::install'] -> anchor { 'mysql::client::end': } } diff --git a/types/options.pp b/types/options.pp index ab08945..71a8f59 100644 --- a/types/options.pp +++ b/types/options.pp @@ -1,4 +1,6 @@ +# @summary A hash of options structured like the override_options, but not merged with the default options. +# Use this if you don’t want your options merged with the default options. type Mysql::Options = Hash[ String, Hash, ]