Skip to content

Commit 7404ed3

Browse files
committed
Merge pull request #809 from WhatsARanjit/to_json_yaml
Added to_json, to_json_pretty, and to_yaml functions
2 parents 1db550a + bd324f9 commit 7404ed3

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,30 @@ Arguments: A single string.
17541754
17551755
*Type*: rvalue.
17561756
1757+
#### `to_json`
1758+
1759+
Converts input into a JSON String.
1760+
1761+
For example, `{ "key" => "value" }` becomes `{"key":"value"}`.
1762+
1763+
*Type*: rvalue.
1764+
1765+
#### `to_json_pretty`
1766+
1767+
Converts input into a pretty JSON String.
1768+
1769+
For example, `{ "key" => "value" }` becomes `{\n \"key\": \"value\"\n}`.
1770+
1771+
*Type*: rvalue.
1772+
1773+
#### `to_yaml`
1774+
1775+
Converts input into a YAML String.
1776+
1777+
For example, `{ "key" => "value" }` becomes `"---\nkey: value\n"`.
1778+
1779+
*Type*: rvalue.
1780+
17571781
#### `try_get_value`
17581782
17591783
**DEPRECATED:** replaced by `dig()`.

lib/puppet/functions/to_json.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Take a data structure and output it as JSON
2+
#
3+
# @example how to output JSON
4+
# # output json to a file
5+
# file { '/tmp/my.json':
6+
# ensure => file,
7+
# content => to_json($myhash),
8+
# }
9+
#
10+
#
11+
require 'json'
12+
13+
Puppet::Functions.create_function(:to_json) do
14+
dispatch :to_json do
15+
param 'Any', :data
16+
end
17+
18+
def to_json(data)
19+
data.to_json
20+
end
21+
end
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Take a data structure and output it as pretty JSON
2+
#
3+
# @example how to output pretty JSON
4+
# # output pretty json to a file
5+
# file { '/tmp/my.json':
6+
# ensure => file,
7+
# content => to_json_pretty($myhash),
8+
# }
9+
#
10+
#
11+
require 'json'
12+
13+
Puppet::Functions.create_function(:to_json_pretty) do
14+
dispatch :to_json_pretty do
15+
param 'Variant[Hash, Array]', :data
16+
end
17+
18+
def to_json_pretty(data)
19+
JSON.pretty_generate(data)
20+
end
21+
end

lib/puppet/functions/to_yaml.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Take a data structure and output it as YAML
2+
#
3+
# @example how to output YAML
4+
# # output yaml to a file
5+
# file { '/tmp/my.yaml':
6+
# ensure => file,
7+
# content => to_yaml($myhash),
8+
# }
9+
#
10+
#
11+
require 'yaml'
12+
13+
Puppet::Functions.create_function(:to_yaml) do
14+
dispatch :to_yaml do
15+
param 'Any', :data
16+
end
17+
18+
def to_yaml(data)
19+
data.to_yaml
20+
end
21+
end

spec/functions/to_json_pretty_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'to_json_pretty' do
4+
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(['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}") }
10+
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}") }
11+
end

spec/functions/to_json_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
3+
describe 'to_json' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params('').and_return("\"\"") }
6+
it { is_expected.to run.with_params(true).and_return("true") }
7+
it { is_expected.to run.with_params('one').and_return("\"one\"") }
8+
it { is_expected.to run.with_params([]).and_return("[]") }
9+
it { is_expected.to run.with_params(['one']).and_return("[\"one\"]") }
10+
it { is_expected.to run.with_params(['one', 'two']).and_return("[\"one\",\"two\"]") }
11+
it { is_expected.to run.with_params({}).and_return("{}") }
12+
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return("{\"key\":\"value\"}") }
13+
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\"]}") }
14+
15+
it { is_expected.to run.with_params('‰').and_return('"‰"') }
16+
it { is_expected.to run.with_params('竹').and_return('"竹"') }
17+
it { is_expected.to run.with_params('Ü').and_return('"Ü"') }
18+
it { is_expected.to run.with_params('∇').and_return('"∇"') }
19+
end

spec/functions/to_yaml_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
describe 'to_yaml' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params('').and_return("--- ''\n") }
6+
it { is_expected.to run.with_params(true).and_return("--- true\n...\n") }
7+
it { is_expected.to run.with_params('one').and_return("--- one\n...\n") }
8+
it { is_expected.to run.with_params([]).and_return("--- []\n") }
9+
it { is_expected.to run.with_params(['one']).and_return("---\n- one\n") }
10+
it { is_expected.to run.with_params(['one', 'two']).and_return("---\n- one\n- two\n") }
11+
it { is_expected.to run.with_params({}).and_return("--- {}\n") }
12+
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return("---\nkey: value\n") }
13+
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") }
14+
15+
it { is_expected.to run.with_params('‰').and_return("--- \"\"\n") }
16+
it { is_expected.to run.with_params('∇').and_return("--- \"\"\n") }
17+
end

0 commit comments

Comments
 (0)