Skip to content

Commit

Permalink
Deal with nested :undef values in nomad_sorted_json
Browse files Browse the repository at this point in the history
  • Loading branch information
RiANOl committed Dec 1, 2016
1 parent 913db85 commit 37a6474
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
12 changes: 5 additions & 7 deletions lib/puppet/parser/functions/nomad_sorted_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class << self

def sorted_generate(obj)
case obj
when NilClass,Fixnum, Float, TrueClass, FalseClass,String
when NilClass, :undef, Fixnum, Float, TrueClass, FalseClass, String
return simple_generate(obj)
when Array
arrayRet = []
Expand All @@ -21,7 +21,7 @@ def sorted_generate(obj)
end
return "{" << ret.join(",") << "}";
else
raise Exception("Unable to handle object of type <%s>" % obj.class.to_s)
raise Exception.new("Unable to handle object of type #{obj.class.name} with value #{obj.inspect}")
end
end

Expand All @@ -31,7 +31,7 @@ def sorted_pretty_generate(obj, indent_len=4)
indent = " " * indent_len

case obj
when NilClass,Fixnum, Float, TrueClass, FalseClass,String
when NilClass, :undef, Fixnum, Float, TrueClass, FalseClass, String
return simple_generate(obj)
when Array
arrayRet = []
Expand Down Expand Up @@ -67,15 +67,15 @@ def sorted_pretty_generate(obj, indent_len=4)

return "{\n" << ret.join(",\n") << "\n#{indent * @@loop}}";
else
raise Exception("Unable to handle object of type <%s>" % obj.class.to_s)
raise Exception.new("Unable to handle object of type #{obj.class.name} with value #{obj.inspect}")
end

end # end def
private
# simplify jsonification of standard types
def simple_generate(obj)
case obj
when NilClass
when NilClass, :undef
'null'
when Fixnum, Float, TrueClass, FalseClass
"#{obj}"
Expand Down Expand Up @@ -155,8 +155,6 @@ module Puppet::Parser::Functions
pretty = args[1] || false
indent_len = args[2].to_i || 4

unsorted_hash.reject! {|key, value| value == :undef }

if pretty
return JSON.sorted_pretty_generate(unsorted_hash, indent_len) << "\n"
else
Expand Down
17 changes: 16 additions & 1 deletion spec/functions/nomad_sorted_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
it 'handles nil' do
expect(subject.call([ {'key' => nil }],pretty)).to eql('{"key":null}')
end
it 'handles :undef' do
expect(subject.call([ {'key' => :undef }],pretty)).to eql('{"key":null}')
end
it 'handles true' do
expect(subject.call([{'key' => true }],pretty)).to eql('{"key":true}')
end
it 'handles nil' do
it 'handles false' do
expect(subject.call([{'key' => false }],pretty)).to eql('{"key":false}')
end
it 'handles positive integer' do
Expand Down Expand Up @@ -58,6 +61,18 @@
expect(json).to match("{\"a\":1,\"p\":2,\"s\":-7,\"z\":3}")
end

it "handles nested :undef values" do
nested_undef_hash = {
'key' => 'value',
'undef' => :undef,
'nested_undef' => {
'undef' => :undef
}
}
json = subject.call([nested_undef_hash])
expect(json).to match("{\"key\":\"value\",\"nested_undef\":{\"undef\":null},\"undef\":null}")
end

context 'nesting' do

let(:nested_test_hash){ { 'z' => [{'l' => 3, 'k' => '2', 'j'=> '1'}],
Expand Down

0 comments on commit 37a6474

Please sign in to comment.