Skip to content

Commit b2f7c32

Browse files
committed
Convert data to Pcore before serialisation
This allow to pass any Puppet structure to the to_python and to_ruby functions.
1 parent 75eb833 commit b2f7c32

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

lib/puppet/functions/to_python.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
Puppet::Functions.create_function(:to_python) do
1818
dispatch :to_python do
19-
param 'Data', :object
19+
param 'Any', :object
2020
end
2121

2222
# @param object
@@ -25,13 +25,18 @@
2525
# @return [String]
2626
# The String representation of the object
2727
def to_python(object)
28-
case object
28+
serialized = Puppet::Pops::Serialization::ToDataConverter.convert(object, rich_data: true)
29+
serialized_to_python(serialized)
30+
end
31+
32+
def serialized_to_python(serialized)
33+
case serialized
2934
when true then 'True'
3035
when false then 'False'
3136
when nil 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
37+
when Array then "[#{serialized.map { |x| serialized_to_python(x) }.join(', ')}]"
38+
when Hash then "{#{serialized.map { |k, v| "#{serialized_to_python(k)}: #{serialized_to_python(v)}" }.join(', ')}}"
39+
else serialized.inspect
3540
end
3641
end
3742
end

lib/puppet/functions/to_ruby.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
Puppet::Functions.create_function(:to_ruby) do
1818
dispatch :to_ruby do
19-
param 'Data', :object
19+
param 'Any', :object
2020
end
2121

2222
# @param object
@@ -25,10 +25,15 @@
2525
# @return [String]
2626
# The String representation of the object
2727
def to_ruby(object)
28-
case object
29-
when Array then "[#{object.map { |x| to_ruby(x) }.join(', ')}]"
30-
when Hash then "{#{object.map { |k, v| "#{to_ruby(k)} => #{to_ruby(v)}" }.join(', ')}}"
31-
else object.inspect
28+
serialized = Puppet::Pops::Serialization::ToDataConverter.convert(object, rich_data: true)
29+
serialized_to_ruby(serialized)
30+
end
31+
32+
def serialized_to_ruby(serialized)
33+
case serialized
34+
when Array then "[#{serialized.map { |x| serialized_to_ruby(x) }.join(', ')}]"
35+
when Hash then "{#{serialized.map { |k, v| "#{serialized_to_ruby(k)} => #{serialized_to_ruby(v)}" }.join(', ')}}"
36+
else serialized.inspect
3237
end
3338
end
3439
end

0 commit comments

Comments
 (0)