-
Notifications
You must be signed in to change notification settings - Fork 582
Fix serialization of undef in to_python() #1205
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
Conversation
to_python is a functionthat may have no external impact to Forge modules. to_ruby is a functionthat may have no external impact to Forge modules. This module is declared in 319 of 578 indexed public
|
In general, the to_ruby, and to_python are quite naive in the data types that they handle. Puppet has a rich type system (Pcore). Read about it here: https://github.com/puppetlabs/puppet-specifications/blob/master/language/data-types/pcore-data-representation.md In order to implement transformations of Pcore data to other representations, the easiest is probably to use the "Generic Pcore Data Format" in JSON and then transform it to the target encoding. That format is described here: https://github.com/puppetlabs/puppet-specifications/blob/master/language/data-types/pcore-generic-data.md (this format is used for the catalog compilation result sent to agents). For examples using the built in serializer in Puppet see https://github.com/hlindberg/tahu/blob/master/lib/puppet/functions/tahu/convert_to_rich_data.rb Example of stuff the naive implementation will probably choke on (or produce some strange string representation of):
|
So could this be considered a bug fix or it's just an improvement? |
@hlindberg may I ask you to check the last commit to confirm it is what you expected? It looks a bit useless to me to have support for this so I might have missed something? Maybe accepting romain@zappy ~/Projects/puppetlabs/puppetlabs-stdlib % cat tset.pp
type Car = Object[attributes => {regnbr => String}]
$x = [default, Timestamp(), /.*/, Car(regnbr => 'abc123'), Integer[0,10], Deferred('f', [1,2,3]), Resource['file', '/tmp/tmp.txt'],3]
warning($x.to_python)
warning($x.to_ruby)
romain@zappy ~/Projects/puppetlabs/puppetlabs-stdlib % puppet apply -t --modulepath spec/fixtures/modules tset.pp
Info: Loading facts
Warning: Scope(Class[main]): [{"__ptype": "Default"}, {"__ptype": "Timestamp", "__pvalue": "2021-10-11T20:15:28.626940753 UTC"}, {"__ptype": "Regexp", "__pvalue": ".*"}, {"__ptype": "Car", "regnbr": "abc123"}, {"__ptype": "Pcore::IntegerType", "from": 0, "to": 10}, {"__ptype": "Deferred", "name": "f", "arguments": [1, 2, 3]}, {"__ptype": "Pcore::ResourceType", "type_name": "File", "title": "/tmp/tmp.txt"}, 3]
Warning: Scope(Class[main]): [{"__ptype" => "Default"}, {"__ptype" => "Timestamp", "__pvalue" => "2021-10-11T20:15:28.626940753 UTC"}, {"__ptype" => "Regexp", "__pvalue" => ".*"}, {"__ptype" => "Car", "regnbr" => "abc123"}, {"__ptype" => "Pcore::IntegerType", "from" => 0, "to" => 10}, {"__ptype" => "Deferred", "name" => "f", "arguments" => [1, 2, 3]}, {"__ptype" => "Pcore::ResourceType", "type_name" => "File", "title" => "/tmp/tmp.txt"}, 3]
Notice: Compiled catalog for zappy.blogreen.org in environment production in 0.02 seconds
Info: Using environment 'production'
Info: Applying configuration version '1633983328'
Notice: Applied catalog in 0.02 seconds
romain@zappy ~/Projects/puppetlabs/puppetlabs-stdlib % I will then rework the branch to take your feedback into account and split the Thanks! |
This PR has been marked as stale because it has been open for a while and has had no recent activity. If this PR is still important to you please drop a comment below and we will add this to our backlog to complete. Otherwise, it will be closed in 7 days. |
Dear 🤖, this is still relevant:
|
Hey @smortex , thanks for letting us know. Am I right that we are waiting on @hlindberg 's response to your question? |
By using Clearly, offering the full support for |
:undef was used in old Puppet functions, but modern Puppet 4 functions receive `undef` Puppet values as `nil` Ruby values. This cause to_python() to serialize `undef` as `nil` instead of `None`, which is not valid Python code. Fix the to_python() behavior by comparing with `nil`. Update the test suite accordingly. While here, also adjust the to_ruby() function which had the same wrong logic but was not causing trouble because in Ruby, we want to serialize `nil` to `nil` :-)
Thanks @hlindberg for the clarification. I moved the commits for Pcore serialization to #1237 and rebased the fix on top of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything look's good to me so I'm gonna go ahead and merge.
Thank you @smortex for the work and @hlindberg for sharing your wise advice.
:undef
was used in old Puppet functions, but modern Puppet 4 functions receiveundef
Puppet values asnil
Ruby values. This cause to_python() to serializeundef
asnil
instead ofNone
, which is not valid Python code.Fix the to_python() behavior by comparing with
nil
. Update the test suite accordingly.While here, also adjust the to_ruby() function which had the same wrong logic but was not causing trouble because in Ruby, we want to serialize
nil
tonil
:-)