Skip to content

Commit bdf9e36

Browse files
committed
(FEAT) Add function parsejson
This commit adds a new function to parse specifically PSON formatted JSON. This functionality was formally covered by `parsejson` which defaulted to JSON. However this has now been changed so that the function now accurately uses JSON with this function covering the potential need for PSON too specifically be used.
1 parent 1306ed5 commit bdf9e36

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
#
4+
# parsepson.rb
5+
#
6+
module Puppet::Parser::Functions
7+
newfunction(:parsepson, type: :rvalue, doc: <<-DOC
8+
@summary
9+
This function accepts PSON, a Puppet variant of JSON, as a string and then converts it into the correct
10+
Puppet structure
11+
12+
@return
13+
convert PSON into Puppet structure
14+
15+
> *Note:*
16+
The optional second argument can be used to pass a default value that will
17+
be returned if the parsing of YAML string have failed.
18+
For more information on PSON please see the following link:
19+
https://puppet.com/docs/puppet/7/http_api/pson.html
20+
DOC
21+
) do |arguments|
22+
raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1
23+
24+
begin
25+
PSON.load(arguments[0]) || arguments[1]
26+
rescue StandardError => e
27+
raise e unless arguments[1]
28+
arguments[1]
29+
end
30+
end
31+
end
32+
33+
# vim: set ts=2 sw=2 et :

spec/functions/parsepson_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'parsepson' do
6+
it 'exists' do
7+
is_expected.not_to eq(nil)
8+
end
9+
10+
it 'raises an error if called without any arguments' do
11+
is_expected.to run.with_params
12+
.and_raise_error(%r{wrong number of arguments}i)
13+
end
14+
15+
context 'with correct PSON data' do
16+
it 'is able to parse PSON data with a Hash' do
17+
is_expected.to run.with_params('{"a":"1","b":"2"}')
18+
.and_return('a' => '1', 'b' => '2')
19+
end
20+
21+
it 'is able to parse PSON data with an Array' do
22+
is_expected.to run.with_params('["a","b","c"]')
23+
.and_return(['a', 'b', 'c'])
24+
end
25+
26+
it 'is able to parse empty PSON values' do
27+
actual_array = ['[]', '{}']
28+
expected = [[], {}]
29+
actual_array.each_with_index do |actual, index|
30+
is_expected.to run.with_params(actual).and_return(expected[index])
31+
end
32+
end
33+
34+
it 'is able to parse PSON data with a mixed structure' do
35+
is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}')
36+
.and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] })
37+
end
38+
39+
it 'is able to parse PSON data with a UTF8 and double byte characters' do
40+
is_expected.to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}')
41+
.and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => ['Á', 'ß'] })
42+
end
43+
44+
it 'does not return the default value if the data was parsed correctly' do
45+
is_expected.to run.with_params('{"a":"1"}', 'default_value')
46+
.and_return('a' => '1')
47+
end
48+
end
49+
50+
context 'with incorrect PSON data' do
51+
it 'raises an error with invalid PSON and no default' do
52+
is_expected.to run.with_params('')
53+
.and_raise_error(PSON::ParserError)
54+
end
55+
56+
it 'supports a structure for a default value' do
57+
is_expected.to run.with_params('', 'a' => '1')
58+
.and_return('a' => '1')
59+
end
60+
61+
['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
62+
it "returns the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
63+
is_expected.to run.with_params(value, 'default_value')
64+
.and_return('default_value')
65+
end
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)