From 37a6474df8a68417d5ddee9be92d42aae3efec77 Mon Sep 17 00:00:00 2001 From: Rianol Jou Date: Thu, 1 Dec 2016 18:18:55 +0800 Subject: [PATCH] Deal with nested :undef values in nomad_sorted_json --- .../parser/functions/nomad_sorted_json.rb | 12 +++++------- spec/functions/nomad_sorted_json_spec.rb | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/puppet/parser/functions/nomad_sorted_json.rb b/lib/puppet/parser/functions/nomad_sorted_json.rb index 58a1f64..ba3190f 100644 --- a/lib/puppet/parser/functions/nomad_sorted_json.rb +++ b/lib/puppet/parser/functions/nomad_sorted_json.rb @@ -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 = [] @@ -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 @@ -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 = [] @@ -67,7 +67,7 @@ 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 @@ -75,7 +75,7 @@ def sorted_pretty_generate(obj, indent_len=4) # simplify jsonification of standard types def simple_generate(obj) case obj - when NilClass + when NilClass, :undef 'null' when Fixnum, Float, TrueClass, FalseClass "#{obj}" @@ -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 diff --git a/spec/functions/nomad_sorted_json_spec.rb b/spec/functions/nomad_sorted_json_spec.rb index 2acfa39..6d95c85 100644 --- a/spec/functions/nomad_sorted_json_spec.rb +++ b/spec/functions/nomad_sorted_json_spec.rb @@ -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 @@ -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'}],