Skip to content

Commit 70e753d

Browse files
committed
New function: any_puppet_to_python()
This function return a String representation of the value passed as parameter.
1 parent fd345d8 commit 70e753d

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Return the Python representation of the passed variable
5+
Puppet::Functions.create_function(:any_puppet_to_python) do
6+
dispatch :any_puppet_to_python do
7+
param 'Any', :value
8+
end
9+
10+
# @param value
11+
# The value to be converted
12+
#
13+
# @return [String]
14+
# The String representation of value
15+
def any_puppet_to_python(value)
16+
case value
17+
when true then 'True'
18+
when false then 'False'
19+
when :undef then 'None'
20+
when Array then "[#{value.map { |x| any_puppet_to_python(x) }.join(', ')}]"
21+
when Hash then "{#{value.map { |k, v| "#{any_puppet_to_python(k)}: #{any_puppet_to_python(v)}" }.join(', ')}}"
22+
else value.inspect
23+
end
24+
end
25+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'any_puppet_to_python function' do
4+
it 'works with no errors' do
5+
pp = <<-EOS
6+
class { 'python':
7+
ensure => 'present',
8+
version => '3',
9+
}
10+
11+
$var = {
12+
1 => 2,
13+
3 => [
14+
5,
15+
7,
16+
'bar'
17+
],
18+
'foo' => 11,
19+
}
20+
file { '/tmp/foo.py':
21+
ensure => file,
22+
mode => '0755',
23+
content => inline_epp(@(PYTHON), { var => $var }),
24+
<%- |Any $var| -%>
25+
#!/usr/bin/env python3
26+
var = <%= $var.any_puppet_to_python() %>
27+
print(var[1] + var[3][1] + var["foo"])
28+
print(var[3][2] * var[3][0])
29+
| PYTHON
30+
}
31+
EOS
32+
33+
apply_manifest(pp, catch_failures: true)
34+
apply_manifest(pp, catch_changes: true)
35+
36+
expect(shell('/tmp/foo.py').stdout).to eq("20\nbarbarbarbarbar\n")
37+
end
38+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'spec_helper'
2+
3+
describe 'any_puppet_to_python' do
4+
context 'with Array[Any]' do
5+
it { is_expected.to run.with_params([]).and_return('[]') }
6+
it { is_expected.to run.with_params([[[[], 1], '2'], 3]).and_return('[[[[], 1], "2"], 3]') }
7+
it { is_expected.to run.with_params([42, 'foo', true, {}]).and_return('[42, "foo", True, {}]') }
8+
end
9+
context 'with Hash[Any]' do
10+
it { is_expected.to run.with_params({}).and_return('{}') }
11+
it { is_expected.to run.with_params({ '1' => { 2 => { '3' => {} } } }).and_return('{"1": {2: {"3": {}}}}') }
12+
it { is_expected.to run.with_params({ 42 => 42, 'foo' => 'bar', '6 * 9' => { 'answer' => [42] } }).and_return('{42: 42, "foo": "bar", "6 * 9": {"answer": [42]}}') }
13+
end
14+
context 'with Boolean' do
15+
it { is_expected.to run.with_params(true).and_return('True') }
16+
it { is_expected.to run.with_params(false).and_return('False') }
17+
end
18+
context 'with String' do
19+
it { is_expected.to run.with_params('').and_return('""') }
20+
it { is_expected.to run.with_params('foo').and_return('"foo"') }
21+
it { is_expected.to run.with_params("foo\nbar").and_return('"foo\nbar"') }
22+
end
23+
context 'with Undef' do
24+
it { is_expected.to run.with_params(:undef).and_return('None') }
25+
end
26+
end

0 commit comments

Comments
 (0)