Skip to content

Commit eefd770

Browse files
authored
Merge pull request #855 from pegasd/to_json_pretty_update
Ability to skip undef values in to_json_pretty()
2 parents 731716c + 2936cf1 commit eefd770

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

lib/puppet/functions/to_json_pretty.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,32 @@
77
# content => to_json_pretty($myhash),
88
# }
99
#
10+
# @example how to output pretty JSON skipping over keys with undef values
11+
# # output pretty JSON to a file skipping over undef values
12+
# file { '/tmp/my.json':
13+
# ensure => file,
14+
# content => to_json_pretty({
15+
# param_one => 'value',
16+
# param_two => undef,
17+
# }),
18+
# }
1019
#
1120
require 'json'
1221

1322
Puppet::Functions.create_function(:to_json_pretty) do
1423
dispatch :to_json_pretty do
1524
param 'Variant[Hash, Array]', :data
25+
optional_param 'Boolean', :skip_undef
1626
end
1727

18-
def to_json_pretty(data)
19-
JSON.pretty_generate(data)
28+
def to_json_pretty(data, skip_undef = false)
29+
if skip_undef
30+
if data.is_a? Array
31+
data = data.reject { |value| value.nil? }
32+
elsif data.is_a? Hash
33+
data = data.reject { |_, value| value.nil? }
34+
end
35+
end
36+
JSON.pretty_generate(data) << "\n"
2037
end
2138
end

spec/functions/to_json_pretty_spec.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
describe 'to_json_pretty' do
44
it { is_expected.not_to eq(nil) }
5-
it { is_expected.to run.with_params([]).and_return("[\n\n]") }
6-
it { is_expected.to run.with_params(['one']).and_return("[\n \"one\"\n]") }
7-
it { is_expected.to run.with_params(%w[one two]).and_return("[\n \"one\",\n \"two\"\n]") }
8-
it { is_expected.to run.with_params({}).and_return("{\n}") }
9-
it { is_expected.to run.with_params('key' => 'value').and_return("{\n \"key\": \"value\"\n}") }
5+
it { is_expected.to run.with_params([]).and_return("[\n\n]\n") }
6+
it { is_expected.to run.with_params(['one']).and_return("[\n \"one\"\n]\n") }
7+
it { is_expected.to run.with_params(%w[one two]).and_return("[\n \"one\",\n \"two\"\n]\n") }
8+
it { is_expected.to run.with_params({}).and_return("{\n}\n") }
9+
it { is_expected.to run.with_params('key' => 'value').and_return("{\n \"key\": \"value\"\n}\n") }
1010
it {
1111
is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => %w[twoA twoB])
12-
.and_return("{\n \"one\": {\n \"oneA\": \"A\",\n \"oneB\": {\n \"oneB1\": \"1\",\n \"oneB2\": \"2\"\n }\n },\n \"two\": [\n \"twoA\",\n \"twoB\"\n ]\n}") # rubocop:disable Metrics/LineLength : Unable to reduce line to required length
12+
.and_return("{\n \"one\": {\n \"oneA\": \"A\",\n \"oneB\": {\n \"oneB1\": \"1\",\n \"oneB2\": \"2\"\n }\n },\n \"two\": [\n \"twoA\",\n \"twoB\"\n ]\n}\n") # rubocop:disable Metrics/LineLength : Unable to reduce line to required length
1313
}
14+
it { is_expected.to run.with_params({ 'one' => '1', 'two' => nil }, true).and_return("{\n \"one\": \"1\"\n}\n") }
15+
it { is_expected.to run.with_params(['one', 'two', nil, 'three'], true).and_return("[\n \"one\",\n \"two\",\n \"three\"\n]\n") }
1416
end

0 commit comments

Comments
 (0)