Merge "Add undef2nil function"
This commit is contained in:
commit
8abe8a95c7
|
@ -59,6 +59,24 @@ Puppet::Type.type(:merge_yaml_settings).provide(:ruby) do
|
|||
result
|
||||
end
|
||||
|
||||
# Purge the :undef values which have been introduced with Puppet4
|
||||
def undef2nil(structure)
|
||||
if structure.is_a? Array
|
||||
structure.map do |element|
|
||||
undef2nil element
|
||||
end
|
||||
elsif structure.is_a? Hash
|
||||
hash = {}
|
||||
structure.each do |key, value|
|
||||
hash[key] = undef2nil value
|
||||
end
|
||||
hash
|
||||
else
|
||||
return nil if structure == :undef
|
||||
structure
|
||||
end
|
||||
end
|
||||
|
||||
# Produce the merged data structure by merging
|
||||
# the original data data with the override data.
|
||||
# @return [Hash]
|
||||
|
@ -68,6 +86,7 @@ Puppet::Type.type(:merge_yaml_settings).provide(:ruby) do
|
|||
debug "Merge override: #{override_data.inspect}" if merge_debug
|
||||
original_data_clone = Marshal.load Marshal.dump original_data
|
||||
YamlDeepMerge.deep_merge! override_data, original_data_clone, deep_merge_options
|
||||
original_data_clone = undef2nil original_data_clone
|
||||
debug "Result: #{original_data_clone.inspect}" if merge_debug
|
||||
original_data_clone
|
||||
end
|
||||
|
|
|
@ -105,6 +105,12 @@ describe Puppet::Type.type(:merge_yaml_settings).provider(:ruby) do
|
|||
expect(provider.merged_data).to eq('a' => %w(2))
|
||||
end
|
||||
|
||||
it 'will replace the :undef values with nil values' do
|
||||
expect(provider).to receive(:original_data).and_return('a' => :undef, 'b' => '1').at_least(:once)
|
||||
expect(provider).to receive(:override_data).and_return('c' => '2').at_least(:once)
|
||||
expect(provider.merged_data).to eq('a' => nil, 'b' => '1', 'c' => '2')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'transaction' do
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
module Puppet::Parser::Functions
|
||||
newfunction(
|
||||
:undef2nil,
|
||||
:type => :rvalue,
|
||||
:doc => <<-EOS
|
||||
Replaces all :undef values with the "nil" values.
|
||||
Useful for Puppet 4 compatibility problems.
|
||||
EOS
|
||||
) do |argument|
|
||||
argument = argument.first
|
||||
|
||||
undef2nil = lambda do |structure|
|
||||
if structure.is_a? Array
|
||||
structure.map do |element|
|
||||
undef2nil.call element
|
||||
end
|
||||
elsif structure.is_a? Hash
|
||||
hash = {}
|
||||
structure.each do |key, value|
|
||||
hash.store key, undef2nil.call(value)
|
||||
end
|
||||
hash
|
||||
else
|
||||
break nil if structure == :undef
|
||||
structure
|
||||
end
|
||||
end
|
||||
|
||||
undef2nil.call argument
|
||||
end
|
||||
end
|
|
@ -0,0 +1,48 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'undef2nil' do
|
||||
|
||||
it 'should exist' do
|
||||
is_expected.not_to be_nil
|
||||
end
|
||||
|
||||
it 'should not modify normal values' do
|
||||
is_expected.to run.with_params('test').and_return('test')
|
||||
is_expected.to run.with_params(nil).and_return(nil)
|
||||
end
|
||||
|
||||
it 'should change :undef to nil in a simple value' do
|
||||
is_expected.to run.with_params(:undef).and_return(nil)
|
||||
end
|
||||
|
||||
it 'should be able to process arrays' do
|
||||
is_expected.to run.with_params(['1', 2, :undef, nil, {}, [1]]).and_return(['1', 2, nil, nil, {}, [1]])
|
||||
end
|
||||
|
||||
it 'should be able to process hashes' do
|
||||
is_expected.to run.with_params(
|
||||
{
|
||||
'a' => 'b',
|
||||
'c' => ['d'],
|
||||
'e' => :undef,
|
||||
:undef => 'f',
|
||||
'g' => {
|
||||
'a' => :undef,
|
||||
'b' => [1, :undef],
|
||||
}
|
||||
}
|
||||
).and_return(
|
||||
{
|
||||
'a' => 'b',
|
||||
'c' => ['d'],
|
||||
'e' => nil,
|
||||
:undef => 'f',
|
||||
'g' => {
|
||||
'a' => nil,
|
||||
'b' => [1, nil],
|
||||
}
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue