Skip to content

Commit 8b91a30

Browse files
authored
Merge pull request #1200 from smortex/to_lang
New function to_python() / to_ruby()
2 parents 842992e + f9cd545 commit 8b91a30

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

lib/puppet/functions/to_python.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Convert an object into a String containing its Python representation
5+
#
6+
# @example how to output Python
7+
# # output Python to a file
8+
# $listen = '0.0.0.0'
9+
# $port = 8000
10+
# file { '/opt/acme/etc/settings.py':
11+
# content => inline_epp(@("SETTINGS")),
12+
# LISTEN = <%= $listen.to_python %>
13+
# PORT = <%= $mailserver.to_python %>
14+
# | SETTINGS
15+
# }
16+
17+
Puppet::Functions.create_function(:to_python) do
18+
dispatch :to_python do
19+
param 'Any', :object
20+
end
21+
22+
# @param object
23+
# The object to be converted
24+
#
25+
# @return [String]
26+
# The String representation of the object
27+
def to_python(object)
28+
case object
29+
when true then 'True'
30+
when false then 'False'
31+
when :undef then 'None'
32+
when Array then "[#{object.map { |x| to_python(x) }.join(', ')}]"
33+
when Hash then "{#{object.map { |k, v| "#{to_python(k)}: #{to_python(v)}" }.join(', ')}}"
34+
else object.inspect
35+
end
36+
end
37+
end

lib/puppet/functions/to_ruby.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Convert an object into a String containing its Ruby representation
5+
#
6+
# @example how to output Ruby
7+
# # output Ruby to a file
8+
# $listen = '0.0.0.0'
9+
# $port = 8000
10+
# file { '/opt/acme/etc/settings.rb':
11+
# content => inline_epp(@("SETTINGS")),
12+
# LISTEN = <%= $listen.to_ruby %>
13+
# PORT = <%= $mailserver.to_ruby %>
14+
# | SETTINGS
15+
# }
16+
17+
Puppet::Functions.create_function(:to_ruby) do
18+
dispatch :to_ruby do
19+
param 'Any', :object
20+
end
21+
22+
# @param object
23+
# The object to be converted
24+
#
25+
# @return [String]
26+
# The String representation of the object
27+
def to_ruby(object)
28+
case object
29+
when :undef then 'nil'
30+
when Array then "[#{object.map { |x| to_ruby(x) }.join(', ')}]"
31+
when Hash then "{#{object.map { |k, v| "#{to_ruby(k)} => #{to_ruby(v)}" }.join(', ')}}"
32+
else object.inspect
33+
end
34+
end
35+
end

spec/functions/to_python_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 'to_python' do
6+
it { is_expected.not_to eq(nil) }
7+
it { is_expected.to run.with_params('').and_return('""') }
8+
it { is_expected.to run.with_params(:undef).and_return('None') }
9+
it { is_expected.to run.with_params(true).and_return('True') }
10+
it { is_expected.to run.with_params(false).and_return('False') }
11+
it { is_expected.to run.with_params('one').and_return('"one"') }
12+
it { is_expected.to run.with_params(42).and_return('42') }
13+
it { is_expected.to run.with_params([]).and_return('[]') }
14+
it { is_expected.to run.with_params(['one']).and_return('["one"]') }
15+
it { is_expected.to run.with_params(['one', 'two']).and_return('["one", "two"]') }
16+
it { is_expected.to run.with_params({}).and_return('{}') }
17+
it { is_expected.to run.with_params('key' => 'value').and_return('{"key": "value"}') }
18+
it {
19+
is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB'])
20+
.and_return('{"one": {"oneA": "A", "oneB": {"oneB1": "1", "oneB2": "2"}}, "two": ["twoA", "twoB"]}')
21+
}
22+
23+
it { is_expected.to run.with_params('‰').and_return('"‰"') }
24+
it { is_expected.to run.with_params('竹').and_return('"竹"') }
25+
it { is_expected.to run.with_params('Ü').and_return('"Ü"') }
26+
it { is_expected.to run.with_params('∇').and_return('"∇"') }
27+
end

spec/functions/to_ruby_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'to_ruby' do
6+
it { is_expected.not_to eq(nil) }
7+
it { is_expected.to run.with_params('').and_return('""') }
8+
it { is_expected.to run.with_params(:undef).and_return('nil') }
9+
it { is_expected.to run.with_params(true).and_return('true') }
10+
it { is_expected.to run.with_params('one').and_return('"one"') }
11+
it { is_expected.to run.with_params(42).and_return('42') }
12+
it { is_expected.to run.with_params([]).and_return('[]') }
13+
it { is_expected.to run.with_params(['one']).and_return('["one"]') }
14+
it { is_expected.to run.with_params(['one', 'two']).and_return('["one", "two"]') }
15+
it { is_expected.to run.with_params({}).and_return('{}') }
16+
it { is_expected.to run.with_params('key' => 'value').and_return('{"key" => "value"}') }
17+
it {
18+
is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => ['twoA', 'twoB'])
19+
.and_return('{"one" => {"oneA" => "A", "oneB" => {"oneB1" => "1", "oneB2" => "2"}}, "two" => ["twoA", "twoB"]}')
20+
}
21+
22+
it { is_expected.to run.with_params('‰').and_return('"‰"') }
23+
it { is_expected.to run.with_params('竹').and_return('"竹"') }
24+
it { is_expected.to run.with_params('Ü').and_return('"Ü"') }
25+
it { is_expected.to run.with_params('∇').and_return('"∇"') }
26+
end

0 commit comments

Comments
 (0)