summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunter Haugen <hunter@puppet.com>2017-08-30 13:56:14 -0700
committerHunter Haugen <hunter@puppet.com>2017-08-30 13:56:14 -0700
commit7404ed3029b726ae9abbb9a4fc698090374bdf98 (patch)
tree4ee37d815cda7b1662dd692fa7113b217fa64e5b
parent1db550a16c49fb7f2f086d2a46bc666c477ee50d (diff)
parentbd324f9d86a79eb3a7a1a781c82eede8a4a447b8 (diff)
Merge pull request #809 from WhatsARanjit/to_json_yaml
Added to_json, to_json_pretty, and to_yaml functions
-rw-r--r--README.md24
-rw-r--r--lib/puppet/functions/to_json.rb21
-rw-r--r--lib/puppet/functions/to_json_pretty.rb21
-rw-r--r--lib/puppet/functions/to_yaml.rb21
-rwxr-xr-xspec/functions/to_json_pretty_spec.rb11
-rwxr-xr-xspec/functions/to_json_spec.rb19
-rwxr-xr-xspec/functions/to_yaml_spec.rb17
7 files changed, 134 insertions, 0 deletions
diff --git a/README.md b/README.md
index 7813f19..1b32396 100644
--- a/README.md
+++ b/README.md
@@ -1754,6 +1754,30 @@ Arguments: A single string.
*Type*: rvalue.
+#### `to_json`
+
+Converts input into a JSON String.
+
+For example, `{ "key" => "value" }` becomes `{"key":"value"}`.
+
+*Type*: rvalue.
+
+#### `to_json_pretty`
+
+Converts input into a pretty JSON String.
+
+For example, `{ "key" => "value" }` becomes `{\n \"key\": \"value\"\n}`.
+
+*Type*: rvalue.
+
+#### `to_yaml`
+
+Converts input into a YAML String.
+
+For example, `{ "key" => "value" }` becomes `"---\nkey: value\n"`.
+
+*Type*: rvalue.
+
#### `try_get_value`
**DEPRECATED:** replaced by `dig()`.
diff --git a/lib/puppet/functions/to_json.rb b/lib/puppet/functions/to_json.rb
new file mode 100644
index 0000000..782d695
--- /dev/null
+++ b/lib/puppet/functions/to_json.rb
@@ -0,0 +1,21 @@
+# Take a data structure and output it as JSON
+#
+# @example how to output JSON
+# # output json to a file
+# file { '/tmp/my.json':
+# ensure => file,
+# content => to_json($myhash),
+# }
+#
+#
+require 'json'
+
+Puppet::Functions.create_function(:to_json) do
+ dispatch :to_json do
+ param 'Any', :data
+ end
+
+ def to_json(data)
+ data.to_json
+ end
+end
diff --git a/lib/puppet/functions/to_json_pretty.rb b/lib/puppet/functions/to_json_pretty.rb
new file mode 100644
index 0000000..4c28539
--- /dev/null
+++ b/lib/puppet/functions/to_json_pretty.rb
@@ -0,0 +1,21 @@
+# 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),
+# }
+#
+#
+require 'json'
+
+Puppet::Functions.create_function(:to_json_pretty) do
+ dispatch :to_json_pretty do
+ param 'Variant[Hash, Array]', :data
+ end
+
+ def to_json_pretty(data)
+ JSON.pretty_generate(data)
+ end
+end
diff --git a/lib/puppet/functions/to_yaml.rb b/lib/puppet/functions/to_yaml.rb
new file mode 100644
index 0000000..fdd7370
--- /dev/null
+++ b/lib/puppet/functions/to_yaml.rb
@@ -0,0 +1,21 @@
+# Take a data structure and output it as YAML
+#
+# @example how to output YAML
+# # output yaml to a file
+# file { '/tmp/my.yaml':
+# ensure => file,
+# content => to_yaml($myhash),
+# }
+#
+#
+require 'yaml'
+
+Puppet::Functions.create_function(:to_yaml) do
+ dispatch :to_yaml do
+ param 'Any', :data
+ end
+
+ def to_yaml(data)
+ data.to_yaml
+ end
+end
diff --git a/spec/functions/to_json_pretty_spec.rb b/spec/functions/to_json_pretty_spec.rb
new file mode 100755
index 0000000..abbcc2c
--- /dev/null
+++ b/spec/functions/to_json_pretty_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe 'to_json_pretty' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params([]).and_return("[\n\n]") }
+ it { is_expected.to run.with_params(['one']).and_return("[\n \"one\"\n]") }
+ it { is_expected.to run.with_params(['one', 'two']).and_return("[\n \"one\",\n \"two\"\n]") }
+ it { is_expected.to run.with_params({}).and_return("{\n}") }
+ it { is_expected.to run.with_params({ 'key' => 'value' }).and_return("{\n \"key\": \"value\"\n}") }
+ it { is_expected.to run.with_params({"one" => {"oneA" => "A","oneB" => {"oneB1" => "1","oneB2" => "2"}},"two" => ["twoA","twoB"]}).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}") }
+end
diff --git a/spec/functions/to_json_spec.rb b/spec/functions/to_json_spec.rb
new file mode 100755
index 0000000..925523c
--- /dev/null
+++ b/spec/functions/to_json_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe 'to_json' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params('').and_return("\"\"") }
+ it { is_expected.to run.with_params(true).and_return("true") }
+ it { is_expected.to run.with_params('one').and_return("\"one\"") }
+ it { is_expected.to run.with_params([]).and_return("[]") }
+ it { is_expected.to run.with_params(['one']).and_return("[\"one\"]") }
+ it { is_expected.to run.with_params(['one', 'two']).and_return("[\"one\",\"two\"]") }
+ it { is_expected.to run.with_params({}).and_return("{}") }
+ it { is_expected.to run.with_params({ 'key' => 'value' }).and_return("{\"key\":\"value\"}") }
+ it { is_expected.to run.with_params({"one" => {"oneA" => "A","oneB" => {"oneB1" => "1","oneB2" => "2"}},"two" => ["twoA","twoB"]}).and_return("{\"one\":{\"oneA\":\"A\",\"oneB\":{\"oneB1\":\"1\",\"oneB2\":\"2\"}},\"two\":[\"twoA\",\"twoB\"]}") }
+
+ it { is_expected.to run.with_params('‰').and_return('"‰"') }
+ it { is_expected.to run.with_params('竹').and_return('"竹"') }
+ it { is_expected.to run.with_params('Ü').and_return('"Ü"') }
+ it { is_expected.to run.with_params('∇').and_return('"∇"') }
+end
diff --git a/spec/functions/to_yaml_spec.rb b/spec/functions/to_yaml_spec.rb
new file mode 100755
index 0000000..3f69a70
--- /dev/null
+++ b/spec/functions/to_yaml_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe 'to_yaml' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params('').and_return("--- ''\n") }
+ it { is_expected.to run.with_params(true).and_return("--- true\n...\n") }
+ it { is_expected.to run.with_params('one').and_return("--- one\n...\n") }
+ it { is_expected.to run.with_params([]).and_return("--- []\n") }
+ it { is_expected.to run.with_params(['one']).and_return("---\n- one\n") }
+ it { is_expected.to run.with_params(['one', 'two']).and_return("---\n- one\n- two\n") }
+ it { is_expected.to run.with_params({}).and_return("--- {}\n") }
+ it { is_expected.to run.with_params({ 'key' => 'value' }).and_return("---\nkey: value\n") }
+ it { is_expected.to run.with_params({"one" => {"oneA" => "A","oneB" => {"oneB1" => "1","oneB2" => "2"}},"two" => ["twoA","twoB"]}).and_return("---\none:\n oneA: A\n oneB:\n oneB1: '1'\n oneB2: '2'\ntwo:\n- twoA\n- twoB\n") }
+
+ it { is_expected.to run.with_params('‰').and_return("--- \"‰\"\n") }
+ it { is_expected.to run.with_params('∇').and_return("--- \"∇\"\n") }
+end