Skip to content

Commit aafe98f

Browse files
committed
1 parent 23fdf23 commit aafe98f

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

REFERENCE.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ the provided regular expression.
167167
last Period).
168168
* [`stdlib::ip_in_range`](#stdlibip_in_range): Returns true if the ipaddress is within the given CIDRs
169169
* [`stdlib::start_with`](#stdlibstart_with): Returns true if str starts with one of the prefixes given. Each of the prefixes should be a String.
170+
* [`stdlib::xml_encode`](#stdlibxml_encode): Encode strings for XML files
170171
* [`str2bool`](#str2bool): This converts a string to a boolean.
171172
* [`str2saltedpbkdf2`](#str2saltedpbkdf2): Convert a string into a salted SHA512 PBKDF2 password hash like requred for OS X / macOS 10.8+
172173
* [`str2saltedsha512`](#str2saltedsha512): This converts a string to a salted-SHA512 password hash (which is used for
@@ -4300,6 +4301,64 @@ Data type: `Variant[String[1],Array[String[1], 1]]`
43004301

43014302
The prefixes to check.
43024303

4304+
### <a name="stdlibxml_encode"></a>`stdlib::xml_encode`
4305+
4306+
Type: Ruby 4.x API
4307+
4308+
This function can encode strings such that they can be used directly in XML files.
4309+
It supports encoding for both XML text (CharData) or attribute values (AttValue).
4310+
4311+
#### Examples
4312+
4313+
##### Creating an XML file from a template
4314+
4315+
```puppet
4316+
file { '/path/to/config.xml':
4317+
ensure => file,
4318+
content => epp(
4319+
'mymodule/config.xml.epp',
4320+
{
4321+
password => $password.stdlib::xml_encode,
4322+
},
4323+
),
4324+
}
4325+
```
4326+
4327+
#### `stdlib::xml_encode(String $str, Optional[Enum['text','attr']] $type)`
4328+
4329+
This function can encode strings such that they can be used directly in XML files.
4330+
It supports encoding for both XML text (CharData) or attribute values (AttValue).
4331+
4332+
Returns: `String` Returns the encoded CharData or AttValue string suitable for use in XML
4333+
4334+
##### Examples
4335+
4336+
###### Creating an XML file from a template
4337+
4338+
```puppet
4339+
file { '/path/to/config.xml':
4340+
ensure => file,
4341+
content => epp(
4342+
'mymodule/config.xml.epp',
4343+
{
4344+
password => $password.stdlib::xml_encode,
4345+
},
4346+
),
4347+
}
4348+
```
4349+
4350+
##### `str`
4351+
4352+
Data type: `String`
4353+
4354+
The string to encode
4355+
4356+
##### `type`
4357+
4358+
Data type: `Optional[Enum['text','attr']]`
4359+
4360+
Whether to encode for text or an attribute
4361+
43034362
### <a name="str2bool"></a>`str2bool`
43044363

43054364
Type: Ruby 3.x API
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# @summary Encode strings for XML files
4+
#
5+
# This function can encode strings such that they can be used directly in XML files.
6+
# It supports encoding for both XML text (CharData) or attribute values (AttValue).
7+
Puppet::Functions.create_function(:'stdlib::xml_encode') do
8+
# @param str The string to encode
9+
# @param type Whether to encode for text or an attribute
10+
# @return Returns the encoded CharData or AttValue string suitable for use in XML
11+
# @example Creating an XML file from a template
12+
# file { '/path/to/config.xml':
13+
# ensure => file,
14+
# content => epp(
15+
# 'mymodule/config.xml.epp',
16+
# {
17+
# password => $password.stdlib::xml_encode,
18+
# },
19+
# ),
20+
# }
21+
dispatch :xml_encode do
22+
param 'String', :str
23+
optional_param "Enum['text','attr']", :type
24+
return_type 'String'
25+
end
26+
27+
def xml_encode(str, type = 'text')
28+
str.encode(xml: type.to_sym)
29+
end
30+
end

spec/functions/xml_encode_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'stdlib::xml_encode' do
6+
let(:string) { 'this is "my" complicated <String>' }
7+
8+
it 'exists' do
9+
is_expected.not_to be_nil
10+
end
11+
12+
describe 'for XML text' do
13+
let(:expected_result) { 'this is "my" complicated &lt;String&gt;' }
14+
15+
context 'with `type` parameter not explicity set' do
16+
it { is_expected.to run.with_params(string).and_return(expected_result) }
17+
end
18+
19+
context 'with `type` parameter set to `text`' do
20+
it { is_expected.to run.with_params(string, 'text').and_return(expected_result) }
21+
end
22+
end
23+
24+
describe 'for XML attributes' do
25+
it { is_expected.to run.with_params(string, 'attr').and_return('"this is &quot;my&quot; complicated &lt;String&gt;"') }
26+
end
27+
end

0 commit comments

Comments
 (0)