Skip to content

Commit 934328e

Browse files
authored
Merge pull request #1231 from alexjfisher/fix_to_yaml
Fix `to_yaml` `options` parameter
2 parents e04e7bd + 3f94a70 commit 934328e

File tree

4 files changed

+89
-27
lines changed

4 files changed

+89
-27
lines changed

REFERENCE.md

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ in a hash.
178178
* [`swapcase`](#swapcase): This function will swap the existing case of a string.
179179
* [`time`](#time): This function will return the current time since epoch as an integer.
180180
* [`to_bytes`](#to_bytes): Converts the argument into bytes, for example 4 kB becomes 4096.
181-
* [`to_json`](#to_json): }
181+
* [`to_json`](#to_json): Convert a data structure and output to JSON
182182
* [`to_json_pretty`](#to_json_pretty): Convert data structure and output to pretty JSON
183183
* [`to_python`](#to_python): Convert an object into a String containing its Python representation
184184
* [`to_ruby`](#to_ruby): Convert an object into a String containing its Ruby representation
185185
* [`to_toml`](#to_toml): Convert a data structure and output to TOML.
186-
* [`to_yaml`](#to_yaml): }
186+
* [`to_yaml`](#to_yaml): Convert a data structure and output it as YAML
187187
* [`try_get_value`](#try_get_value)
188188
* [`type`](#type): **DEPRECATED:** This function will cease to function on Puppet 4;
189189
* [`type3x`](#type3x): **DEPRECATED:** This function will be removed when Puppet 3 support is dropped; please migrate to the new parser's typing system.
@@ -4569,19 +4569,41 @@ Returns: `Any` converted value into bytes
45694569

45704570
Type: Ruby 4.x API
45714571

4572+
Convert a data structure and output to JSON
4573+
4574+
#### Examples
4575+
4576+
##### Output JSON to a file
4577+
4578+
```puppet
4579+
file { '/tmp/my.json':
4580+
ensure => file,
4581+
content => to_json($myhash),
45724582
}
4583+
```
45734584

45744585
#### `to_json(Any $data)`
45754586

4576-
}
4587+
The to_json function.
4588+
4589+
Returns: `String` Converted data to JSON
45774590

4578-
Returns: `Any` converted data to json
4591+
##### Examples
4592+
4593+
###### Output JSON to a file
4594+
4595+
```puppet
4596+
file { '/tmp/my.json':
4597+
ensure => file,
4598+
content => to_json($myhash),
4599+
}
4600+
```
45794601

45804602
##### `data`
45814603

45824604
Data type: `Any`
45834605

4584-
data structure which needs to be converted into JSON
4606+
Data structure which needs to be converted into JSON
45854607

45864608
### <a name="to_json_pretty"></a>`to_json_pretty`
45874609

@@ -4839,25 +4861,65 @@ Data structure which needs to be converted into TOML
48394861

48404862
Type: Ruby 4.x API
48414863

4864+
Convert a data structure and output it as YAML
4865+
4866+
#### Examples
4867+
4868+
##### Output YAML to a file
4869+
4870+
```puppet
4871+
file { '/tmp/my.yaml':
4872+
ensure => file,
4873+
content => to_yaml($myhash),
48424874
}
4875+
```
4876+
4877+
##### Use options to control the output format
4878+
4879+
```puppet
4880+
file { '/tmp/my.yaml':
4881+
ensure => file,
4882+
content => to_yaml($myhash, {indentation => 4})
4883+
}
4884+
```
48434885

48444886
#### `to_yaml(Any $data, Optional[Hash] $options)`
48454887

4888+
The to_yaml function.
4889+
4890+
Returns: `String` The YAML document
4891+
4892+
##### Examples
4893+
4894+
###### Output YAML to a file
4895+
4896+
```puppet
4897+
file { '/tmp/my.yaml':
4898+
ensure => file,
4899+
content => to_yaml($myhash),
48464900
}
4901+
```
48474902

4848-
Returns: `String`
4903+
###### Use options to control the output format
4904+
4905+
```puppet
4906+
file { '/tmp/my.yaml':
4907+
ensure => file,
4908+
content => to_yaml($myhash, {indentation => 4})
4909+
}
4910+
```
48494911

48504912
##### `data`
48514913

48524914
Data type: `Any`
48534915

4854-
4916+
The data you want to convert to YAML
48554917

48564918
##### `options`
48574919

48584920
Data type: `Optional[Hash]`
48594921

4860-
4922+
A hash of options that will be passed to Ruby's Psych library. Note, this could change between Puppet versions, but at time of writing these are `line_width`, `indentation`, and `canonical`.
48614923

48624924
### <a name="try_get_value"></a>`try_get_value`
48634925

lib/puppet/functions/to_json.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
# @summary
55
# Convert a data structure and output to JSON
66
#
7-
# @example how to output JSON
8-
# # output json to a file
9-
# file { '/tmp/my.json':
10-
# ensure => file,
11-
# content => to_json($myhash),
12-
# }
7+
# @example Output JSON to a file
8+
# file { '/tmp/my.json':
9+
# ensure => file,
10+
# content => to_json($myhash),
11+
# }
1312
#
1413
Puppet::Functions.create_function(:to_json) do
1514
# @param data
16-
# data structure which needs to be converted into JSON
17-
# @return converted data to json
15+
# Data structure which needs to be converted into JSON
16+
# @return [String] Converted data to JSON
1817
dispatch :to_json do
1918
param 'Any', :data
2019
end

lib/puppet/functions/to_yaml.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@
44
# @summary
55
# Convert a data structure and output it as YAML
66
#
7-
# @example How to output YAML
8-
# # output yaml to a file
9-
# file { '/tmp/my.yaml':
10-
# ensure => file,
11-
# content => to_yaml($myhash),
12-
# }
13-
# @example Use options control the output format
7+
# @example Output YAML to a file
148
# file { '/tmp/my.yaml':
159
# ensure => file,
16-
# content => to_yaml($myhash, {indentation: 4})
10+
# content => to_yaml($myhash),
11+
# }
12+
# @example Use options to control the output format
13+
# file { '/tmp/my.yaml':
14+
# ensure => file,
15+
# content => to_yaml($myhash, {indentation => 4})
1716
# }
1817
Puppet::Functions.create_function(:to_yaml) do
1918
# @param data
19+
# The data you want to convert to YAML
2020
# @param options
21+
# A hash of options that will be passed to Ruby's Psych library. Note, this could change between Puppet versions, but at time of writing these are `line_width`, `indentation`, and `canonical`.
2122
#
22-
# @return [String]
23+
# @return [String] The YAML document
2324
dispatch :to_yaml do
2425
param 'Any', :data
2526
optional_param 'Hash', :options
2627
end
2728

2829
def to_yaml(data, options = {})
29-
data.to_yaml(options)
30+
data.to_yaml(options.transform_keys(&:to_sym))
3031
end
3132
end

spec/functions/to_yaml_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
it { is_expected.to run.with_params('‰').and_return("--- \"\"\n") }
2121
it { is_expected.to run.with_params('∇').and_return("--- \"\"\n") }
2222

23-
it { is_expected.to run.with_params({ 'foo' => { 'bar' => true, 'baz' => false } }, indentation: 4).and_return("---\nfoo:\n bar: true\n baz: false\n") }
23+
it { is_expected.to run.with_params({ 'foo' => { 'bar' => true, 'baz' => false } }, 'indentation' => 4).and_return("---\nfoo:\n bar: true\n baz: false\n") }
2424
end

0 commit comments

Comments
 (0)