Skip to content

Commit f04feb8

Browse files
authored
Merge pull request #1237 from puppetlabs/to_ruby-to_python-pcore
Convert data to Pcore before serialisation in to_ruby/to_python
2 parents 35bb2df + a338d7b commit f04feb8

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/puppet/functions/to_python.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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)