Skip to content

Convert data to Pcore before serialisation in to_ruby/to_python #1237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/puppet/functions/to_python.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@
# @return [String]
# The String representation of the object
def to_python(object)
case object
serialized = Puppet::Pops::Serialization::ToDataConverter.convert(object, rich_data: true)
serialized_to_python(serialized)
end

def serialized_to_python(serialized)
case serialized
when true then 'True'
when false then 'False'
when :undef then 'None'
when Array then "[#{object.map { |x| to_python(x) }.join(', ')}]"
when Hash then "{#{object.map { |k, v| "#{to_python(k)}: #{to_python(v)}" }.join(', ')}}"
else object.inspect
when nil then 'None'
when Array then "[#{serialized.map { |x| serialized_to_python(x) }.join(', ')}]"
when Hash then "{#{serialized.map { |k, v| "#{serialized_to_python(k)}: #{serialized_to_python(v)}" }.join(', ')}}"
else serialized.inspect
end
end
end
14 changes: 9 additions & 5 deletions lib/puppet/functions/to_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
# @return [String]
# The String representation of the object
def to_ruby(object)
case object
when :undef then 'nil'
when Array then "[#{object.map { |x| to_ruby(x) }.join(', ')}]"
when Hash then "{#{object.map { |k, v| "#{to_ruby(k)} => #{to_ruby(v)}" }.join(', ')}}"
else object.inspect
serialized = Puppet::Pops::Serialization::ToDataConverter.convert(object, rich_data: true)
serialized_to_ruby(serialized)
end

def serialized_to_ruby(serialized)
case serialized
when Array then "[#{serialized.map { |x| serialized_to_ruby(x) }.join(', ')}]"
when Hash then "{#{serialized.map { |k, v| "#{serialized_to_ruby(k)} => #{serialized_to_ruby(v)}" }.join(', ')}}"
else serialized.inspect
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/to_python_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe 'to_python' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params('').and_return('""') }
it { is_expected.to run.with_params(:undef).and_return('None') }
it { is_expected.to run.with_params(nil).and_return('None') }
it { is_expected.to run.with_params(true).and_return('True') }
it { is_expected.to run.with_params(false).and_return('False') }
it { is_expected.to run.with_params('one').and_return('"one"') }
Expand Down
2 changes: 1 addition & 1 deletion spec/functions/to_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe 'to_ruby' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params('').and_return('""') }
it { is_expected.to run.with_params(:undef).and_return('nil') }
it { is_expected.to run.with_params(nil).and_return('nil') }
it { is_expected.to run.with_params(true).and_return('true') }
it { is_expected.to run.with_params('one').and_return('"one"') }
it { is_expected.to run.with_params(42).and_return('42') }
Expand Down