Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

(#13202) Modify join() to pass strings unmodified without errors.

Previously the join function would raise an error if anything besides an
array was passed. This changes it to allow strings to be passed through
join unmodified. This allows puppet code to be written that can accept
either a string or an array, and act appropriately in both cases
automatically. Specifically, join("abc") would return "abc".
  • Loading branch information...
commit 070cbfbd8028db08140a5e2267ce7a7f5a26a259 1 parent 033edda
Mike Cooper authored committed
View
4 lib/puppet/parser/functions/join.rb
@@ -21,6 +21,10 @@ module Puppet::Parser::Functions
array = arguments[0]
unless array.is_a?(Array)
+ if array.is_a?(String)
+ # Ok, fine. return the string unmodified.
+ return array
+ end
raise(Puppet::ParseError, 'join(): Requires array to work with')
end
View
4 spec/unit/puppet/parser/functions/join_spec.rb
@@ -23,4 +23,8 @@
result.should(eq("a:b:c"))
end
+ it "should pass a string unmodified" do
+ result = @scope.function_join(["abc"])
+ result.should(eq("abc"))
+ end
end

4 comments on commit 070cbfb

@kwilczynski

I like this idea, but do you think it is a valid use case? Enforcing particular type makes it more predictable.

@mythmon
Owner

The case in which I was using this in was something like this:

define foo::bar ($users) {
    file { "/etc/foo/bar.conf":
        ensure => present,
        content => join($users),
    }
}

Without this modification, you have to write code like this:

foo::bar { "baz": users => ["bob"], }

which seems strange when you don't understand the reasoning behind it. With this change, both of these work:

foo::bar { "baz": users => "bob", }
foo::bar { "fez": users => ["bob", "fred"], }

which seems more natural to me. It also mirrors structures already found in Puppet, such as how require, notify, before, and subscribe can be a single resource, or an array of resources. I think its important to be consistent, and I think this helps that. I think the type safety issue isn't too bad, because it will still always return a string.

@kwilczynski

That is a fair point. I guess, we need to modify prefix() and others who are alike, to accommodate for this. Would you care to look at my "puppet-functions" repository and comment of what I could improve and/or add? :) I would be much obliged.

@kwilczynski

Oh the other hand, there is a function as well, which can coerce a string and/or an array into an array, which could be used here.

My reasoning here is that there is no String#join method in Ruby:

irb(main):001:0> ''.join
NoMethodError: undefined method `join' for "":String
    from (irb):1
    from :0
irb(main):002:0> 

I am still undecided.

KW

Please sign in to comment.
Something went wrong with that request. Please try again.