delete_undef_values function fix bug #20681#184
delete_undef_values function fix bug #20681#184adrienthebo merged 1 commit intopuppetlabs:masterfrom
Conversation
lmello
commented
Sep 16, 2013
|
sure! :-) |
There was a problem hiding this comment.
Sorry, one last comment - why do we alternate between result and args[0] here?
There was a problem hiding this comment.
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:
- do .dup on the result when its value is attributed (method i used)
or - 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.
There was a problem hiding this comment.
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
# ...|
Thanks for your suggestion, it is much better now! :-D |
|
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 |
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 ... typo fixed. |
delete_undef_values function fix bug #20681
|
summary: merged into master in 7ccf8cf; thanks for the contribution! |
|
Thanks for merging! :-) |