-
Notifications
You must be signed in to change notification settings - Fork 583
(#13610) Add function_available to stdlib #59
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
(#13610) Add function_available to stdlib #59
Conversation
Hi, This is definitely a nice idea, but I'd rather see real boolean being returned in both cases, therefore a little update: function = Puppet::Parser::Functions.function(:notice) function.is_a?(String) and not function.empty? Above would yield boolean true, whereas: function = Puppet::Parser::Functions.function(:abc) function.is_a?(String) and not function.empty? Results in false. This makes this function a little bit more deterministic in terms of its behaviour :) KW |
Krzysztof, The idea here is to expose some Puppet internals that otherwise would need a (light) Ruby wrapper. If you don't you need an additional function to grab this info. This works well enough on the Ruby side...any significant reason to make it a straight-up boolean? It still seems easy enough to test against in its present form. -Eric |
Hi, I am not quite sure if I understand your reasoning. Is this intended to use for people within Puppet DSL? KW |
It is. But the effect is the same. If you say if function_available('foo') { Then with the function as provided, should function_available return false, Y will happen. On any other return value, X will. So I guess the question is, what's the advantage in obscuring the output/returning LESS information? |
Hi, I still do not follow your reasoning, but after reading description which you have added, I would expect this to work: matti@acrux ~/test $ RUBYLIB=. cat - | puppet apply case function_available('notice') { true: { notice 'yes' } false: { notice 'no' } } notice: Finished catalog run in 0.02 seconds Like this piece of code: matti@acrux ~/test $ RUBYLIB=. cat - | puppet apply case function_available('notice1') { true: { notice 'yes' } false: { notice 'no' } } notice: Scope(Class[main]): no notice: Finished catalog run in 0.03 seconds Just "going by" the sheer fact that :undef, '' (empty string) and false evaluates to boolean false (or FalseClass in Ruby) is still not enough to justify the reason for retuning either string or boolean. So, please either fix it and/or update your description so you wont confuse users :) Also, I do not want this to change into a bike shedding problem, therefore I leave people from Puppet Labs to decide what behaviour they would prefer :) KW |
OK I definitely can see that - the description is definitely misleading. Fixing..... ...and fixed. Description now better reflects the actual behavior. -Eric |
I would expect a "function_available" method to return a boolean. |
This function provides a simple wrapper around Puppet::Parser::Functions.function for access within Puppet manifests. This will allow users to check whether or not a plugin or functionality such as hiera is installed on the server.
OK. Modified to return a boolean, spec test updated as well. |
Hi Nigel, [...]
I felt a little bit like a douche, therefore thank you for making the P.S. I care a great deal about stdlib :) KW |
You should totally not feel like a douche :) Many eyes make code better :) |
@kwilczynski +1 to Nigel's comment. That's what pull requests are for :) |
@eshamow +1 for implementing and sharing! Open Source is so amazing ;-) |
@kwilczynski It's less awesome than it seems, I'm a PL employee :D I just don't contribute code all that often. |
@eshamow haha, lucky you ;-) For being PL employee ;-) |
@kwilczynski Apply! Puppet is always hiring and it's an awesome place to hang your hat. @nigelkersten This look mergeable to you? (ignore recruiting efforts above :D ) |
@eshamow you guys don't hire in London :-( Plus, I am not entirely sure if @nigelkersten would be happy to have me on-board (?) :) |
@eshamow reviewing this now. |
end | ||
|
||
function = Puppet::Parser::Functions.function(arguments[0].to_sym) | ||
function.is_a?(String) and not function.empty? |
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.
This could also be a string comparison:
function == "function_" + arguments[0]
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.
@kbarber Could be, indeed.
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.
@eshamow and @kbarber there is one little bit that is missing, at least I believe so. I mean, the missing bit is error handling on argument type-checking. For instance, calling to_sym on [] and {}, as well as any Float will result in NoMethodError, then for both positive and negative Integer it would return simply NilClass, and finally for empty string '' we'd get ArgumentError being risen.
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.
Aaah sorry - @kwilczynski is correct, but not as bad as he outlines. The problem is things like this:
Puppet::Parser::Functions.function("".to_sym)
ArgumentError: interning empty string
from (irb):3:in `to_sym'
from (irb):3
Since Puppet treats numbers likes strings, this is pretty much the main issue. Regardless, lets validate the input Eric - adding tests to make sure its a string, not empty etc. Thanks @kwilczynski.
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.
I am merely trying help :) I did not wanted to make it sound bad or horrifying, sorry. Fix is easy: check if String and if not empty.
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.
I think generally an input function name might need validation, even if Puppet::Parser::Functions.function() does kind of the right thing anyway - it really should return a second indicator if the function name is invalid (not just if it exists/doesn't exists) ... however I'm wary of replicating validation for function names here in this function. Better to let Puppet core do this kind of deeper validation IMHO so the rules are kept in one place.
But string and not empty is a damn good start.
The code is good made can't find much fault (although you don't have a space before the require 'spec_helper' you heathen!!! :-). Umm ... what about putting an 'is' in front of this? I figure it lines up with the other is_something_or_rather functions we have around. |
@kbarber +1 for prefixing with "is_". |
Awesome work guys. |
…3610_function_available
Merged into master as fc2352f. Thanks again for the contribution! -Adrien |
This function provides a simple wrapper around
Puppet::Parser::Functions.function for access within Puppet manifests.
This will allow users to check whether or not a plugin or functionality
such as hiera is installed on the server.