Skip to content
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

delete_undef_values function fix bug #20681 #184

Merged

Conversation

lmello
Copy link
Contributor

@lmello lmello commented Sep 16, 2013

(#20681) fix behaviour of delete_undef_values

The issue #20681 describe the error of delete() function
removing the elements from the origin array/hash/string.

This issue affected other delete functions. Because
ruby delete and delete_if functions make destructive
changes to the origin array/hash.

The delete_undef_values removed elements from the
origin array/hash and this is not the desired behaviour.

To solve this, we should dup or clone the array/hash
before using the delete or delete_if ruby functions.

We should also check if args[0] is not nil before using
dup, since dup on nil raises exception.

This fix the problem and add unit tests, so we could
enforce this behaviour and prevent regressions.

@adrienthebo
Copy link
Contributor

Thank you very much for this contribution! Could this have the commit fixed up like GH-182 and GH-183 before I merge it?

@lmello
Copy link
Contributor Author

lmello commented Sep 16, 2013

sure! :-)

if result.is_a?(Hash)
result = args[0].dup unless args[0].nil?
if args[0].is_a?(Hash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one last comment - why do we alternate between result and args[0] here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is necessary because we can't use .dup on nil .
as a side effect we only attribute result value if args[0] is not nil.

If the function receives a empty or nil argument,
we won't have the result value attributed, but args[0] will always exists, so
it would be better to check the type of arg[0] instead of result.

when i implemented this i had thought about other ways of making this change:

basically we could:

  1. do .dup on the result when its value is attributed (method i used)
    or
  2. before running the delete or delete_if commands (in this case we would need to have
    .dup in two different places. )

as see by the following example :
result = args[0]
if result.is_a?(hash)
result = result.dup.delete_if {|key, val| val.equal? :undef}
elsif result.is_a?(Array)
result = result.dup.delete :undef
else
raise(Puppet::ParseError, .....REDACTED....

if you prefer method 2, or have other suggestion, I could change that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about doing error checking on the type first?

unless args[0].is_a? Array or args[0].is_a? Hash
  raise Puppet::ParseError, "delete_undef_values(): expected an array or hash, got a #{args[0]}"
end

result = args[0].dup
# ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@lmello
Copy link
Contributor Author

lmello commented Sep 17, 2013

Thanks for your suggestion, it is much better now! :-D

@adrienthebo
Copy link
Contributor

The travis-ci build failed (https://travis-ci.org/puppetlabs/puppetlabs-stdlib/jobs/11480648#L927), https://github.com/puppetlabs/puppetlabs-stdlib/pull/184/files#L0R24 should be got #{args[0]} instead of got #{arg[0]}

The issue #20681 describe the error of delete() function
removing the elements from the origin array/hash/string.

This issue affected other delete functions. Because
ruby delete and delete_if functions make destructive
changes to the origin array/hash.

The delete_undef_values removed elements from the
origin array/hash and this is not the desired behaviour.

To solve this, we should dup or clone the array/hash
before using the delete or delete_if ruby functions.

We should also check if args[0] is not nil before using
dup, since dup on nil raises exception.

This fix the problem and add unit tests, so we could
enforce this behaviour and prevent regressions.
@lmello
Copy link
Contributor Author

lmello commented Sep 18, 2013

@adrienthebo ... typo fixed.

adrienthebo added a commit that referenced this pull request Sep 18, 2013
delete_undef_values function fix bug #20681
@adrienthebo adrienthebo merged commit 7ccf8cf into puppetlabs:master Sep 18, 2013
@adrienthebo
Copy link
Contributor

summary: merged into master in 7ccf8cf; thanks for the contribution!

@lmello
Copy link
Contributor Author

lmello commented Sep 18, 2013

Thanks for merging! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants