Skip to content

Commit 0430ebc

Browse files
committed
Namespace function parsepson()
1 parent f11d0fb commit 0430ebc

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

lib/puppet/functions/parsepson.rb

+8-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary
4-
# This function accepts PSON, a Puppet variant of JSON, as a string and converts
5-
# it into the correct Puppet structure
6-
#
7-
# @example How to parse pson
8-
# $data = parsepson('{"a":"1","b":"2"}')
9-
#
10-
# For more information on PSON please see the following link:
11-
# https://puppet.com/docs/puppet/7/http_api/pson.html
12-
#
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::parsepson`](#stdlibparsepson) instead.
136
Puppet::Functions.create_function(:parsepson) do
14-
# @param pson_string A valid PSON string
15-
# @param default An optional default to return if parsing the pson_string fails
16-
# @return [Data]
17-
dispatch :parsepson do
18-
param 'String[1]', :pson_string
19-
optional_param 'Any', :default
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
209
end
21-
22-
def parsepson(pson_string, default = :no_default_provided)
23-
PSON.load(pson_string)
24-
rescue StandardError => err
25-
Puppet.debug("Parsing PSON failed with error: #{err.message}")
26-
raise err if default == :no_default_provided
27-
default
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'parsepson', 'This method is deprecated, please use stdlib::parsepson instead.')
12+
call_function('stdlib::parsepson', *args)
2813
end
2914
end
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# This function accepts PSON, a Puppet variant of JSON, as a string and converts
5+
# it into the correct Puppet structure
6+
#
7+
# @example How to parse pson
8+
# $data = stdlib::parsepson('{"a":"1","b":"2"}')
9+
#
10+
# For more information on PSON please see the following link:
11+
# https://puppet.com/docs/puppet/7/http_api/pson.html
12+
#
13+
Puppet::Functions.create_function(:'stdlib::parsepson') do
14+
# @param pson_string A valid PSON string
15+
# @param default An optional default to return if parsing the pson_string fails
16+
# @return [Data]
17+
dispatch :parsepson do
18+
param 'String[1]', :pson_string
19+
optional_param 'Any', :default
20+
end
21+
22+
def parsepson(pson_string, default = :no_default_provided)
23+
PSON.load(pson_string)
24+
rescue StandardError => err
25+
Puppet.debug("Parsing PSON failed with error: #{err.message}")
26+
raise err if default == :no_default_provided
27+
default
28+
end
29+
end

spec/functions/parsepson_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
require 'spec_helper'
44

5-
describe 'parsepson' do
5+
describe 'stdlib::parsepson' do
66
it 'exists' do
77
is_expected.not_to eq(nil)
88
end
99

1010
it 'raises an error if called without any arguments' do
1111
is_expected.to run.with_params
12-
.and_raise_error(%r{'parsepson' expects between 1 and 2 arguments, got none}i)
12+
.and_raise_error(%r{'stdlib::parsepson' expects between 1 and 2 arguments, got none}i)
1313
end
1414

1515
context 'with correct PSON data' do

0 commit comments

Comments
 (0)