Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Thus making it directly usable with the values from facter.
* [`extlib::read_url`](#extlibread_url): Fetch a string from a URL (should only be used with 'small' remote files). This function should only be used with trusted/internal sources.
* [`extlib::resources_deep_merge`](#extlibresources_deep_merge): Deeply merge a "defaults" hash into a "resources" hash like the ones expected by `create_resources()`.
* [`extlib::sort_by_version`](#extlibsort_by_version): A function that sorts an array of version numbers.
* [`extlib::xml_encode`](#extlibxml_encode): Encode strings for XML files

## Functions

Expand Down Expand Up @@ -1012,3 +1013,61 @@ Data type: `Array[String]`

An array of version strings you want sorted.

### <a name="extlibxml_encode"></a>`extlib::xml_encode`

Type: Ruby 4.x API

This function can encode strings such that they can be used directly in XML files.
It supports encoding for both XML text (CharData) or attribute values (AttValue).

#### Examples

##### Creating an XML file from a template

```puppet
file { '/path/to/config.xml':
ensure => file,
content => epp(
'mymodule/config.xml.epp',
{
password => $password.extlib::xml_encode,
},
),
}
```

#### `extlib::xml_encode(String $str, Optional[Enum['text','attr']] $type)`

This function can encode strings such that they can be used directly in XML files.
It supports encoding for both XML text (CharData) or attribute values (AttValue).

Returns: `String` Returns the encoded CharData or AttValue string suitable for use in XML

##### Examples

###### Creating an XML file from a template

```puppet
file { '/path/to/config.xml':
ensure => file,
content => epp(
'mymodule/config.xml.epp',
{
password => $password.extlib::xml_encode,
},
),
}
```

##### `str`

Data type: `String`

The string to encode

##### `type`

Data type: `Optional[Enum['text','attr']]`

Whether to encode for text or an attribute

30 changes: 30 additions & 0 deletions lib/puppet/functions/extlib/xml_encode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# @summary Encode strings for XML files
#
# This function can encode strings such that they can be used directly in XML files.
# It supports encoding for both XML text (CharData) or attribute values (AttValue).
Puppet::Functions.create_function(:'extlib::xml_encode') do
# @param str The string to encode
# @param type Whether to encode for text or an attribute
# @return Returns the encoded CharData or AttValue string suitable for use in XML
# @example Creating an XML file from a template
# file { '/path/to/config.xml':
# ensure => file,
# content => epp(
# 'mymodule/config.xml.epp',
# {
# password => $password.extlib::xml_encode,
# },
# ),
# }
dispatch :xml_encode do
param 'String', :str
optional_param "Enum['text','attr']", :type
return_type 'String'
end

def xml_encode(str, type = 'text')
str.encode(xml: type.to_sym)
end
end
27 changes: 27 additions & 0 deletions spec/functions/extlib/xml_encode_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'extlib::xml_encode' do
let(:string) { 'this is "my" complicated <String>' }

it 'exists' do
is_expected.not_to be_nil
end

describe 'for XML text' do
let(:expected_result) { 'this is "my" complicated &lt;String&gt;' }

context 'with `type` parameter not explicity set' do
it { is_expected.to run.with_params(string).and_return(expected_result) }
end

context 'with `type` parameter set to `text`' do
it { is_expected.to run.with_params(string, 'text').and_return(expected_result) }
end
end

describe 'for XML attributes' do
it { is_expected.to run.with_params(string, 'attr').and_return('"this is &quot;my&quot; complicated &lt;String&gt;"') }
end
end