Add missing new files from commit 131e09855e06
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / functions / to_json_pretty.rb
diff --git a/3rdparty/modules/stdlib/lib/puppet/functions/to_json_pretty.rb b/3rdparty/modules/stdlib/lib/puppet/functions/to_json_pretty.rb
new file mode 100644 (file)
index 0000000..a7a1458
--- /dev/null
@@ -0,0 +1,38 @@
+# Take a data structure and output it as pretty JSON
+#
+# @example how to output pretty JSON
+#   # output pretty json to a file
+#     file { '/tmp/my.json':
+#       ensure  => file,
+#       content => to_json_pretty($myhash),
+#     }
+#
+# @example how to output pretty JSON skipping over keys with undef values
+#   # output pretty JSON to a file skipping over undef values
+#     file { '/tmp/my.json':
+#       ensure  => file,
+#       content => to_json_pretty({
+#         param_one => 'value',
+#         param_two => undef,
+#       }),
+#     }
+#
+require 'json'
+
+Puppet::Functions.create_function(:to_json_pretty) do
+  dispatch :to_json_pretty do
+    param 'Variant[Hash, Array]', :data
+    optional_param 'Boolean', :skip_undef
+  end
+
+  def to_json_pretty(data, skip_undef = false)
+    if skip_undef
+      if data.is_a? Array
+        data = data.reject { |value| value.nil? }
+      elsif data.is_a? Hash
+        data = data.reject { |_, value| value.nil? }
+      end
+    end
+    JSON.pretty_generate(data) << "\n"
+  end
+end