summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/parser/ast/objectdef.rb33
-rwxr-xr-xtest/language/ast.rb24
2 files changed, 49 insertions, 8 deletions
diff --git a/lib/puppet/parser/ast/objectdef.rb b/lib/puppet/parser/ast/objectdef.rb
index 7ccc9851a..7ed64388e 100644
--- a/lib/puppet/parser/ast/objectdef.rb
+++ b/lib/puppet/parser/ast/objectdef.rb
@@ -55,23 +55,40 @@ class Puppet::Parser::AST
raise error
end
+ hash = {}
+ # Evaluate all of the specified params.
+ @params.each { |param|
+ ary = param.safeevaluate(:scope => scope)
+ hash[ary[0]] = ary[1]
+ }
+
objnames = [nil]
- # Autogenerate the name if one was not passed.
+ # Determine our name if we have one.
if self.name
objnames = @name.safeevaluate(:scope => scope)
# it's easier to always use an array, even for only one name
unless objnames.is_a?(Array)
objnames = [objnames]
end
- end
+ else
+ # See if they specified the name as a parameter instead of as a
+ # normal name (i.e., before the colon).
+ unless object # we're a builtin
+ if objclass = Puppet::Type.type(objtype)
+ namevar = objclass.namevar
- hash = {}
+ tmp = hash["name"] || hash[namevar.to_s]
- # then set all of the specified params
- @params.each { |param|
- ary = param.safeevaluate(:scope => scope)
- hash[ary[0]] = ary[1]
- }
+ if tmp
+ objnames = [tmp]
+ end
+ else
+ # this should never happen, because we've already
+ # typechecked, but it's no real problem if it does happen.
+ # We just end up with an object with no name.
+ end
+ end
+ end
# this is where our implicit iteration takes place;
# if someone passed an array as the name, then we act
diff --git a/test/language/ast.rb b/test/language/ast.rb
index 7dc8d80d3..8860f4ad3 100755
--- a/test/language/ast.rb
+++ b/test/language/ast.rb
@@ -753,6 +753,30 @@ class TestAST < Test::Unit::TestCase
end
end
+ # Make sure we catch names that are specified like parameters.
+ def test_name_or_param
+ obj = nil
+ assert_nothing_raised {
+ obj = AST::ObjectDef.new(
+ :type => nameobj("file"),
+ :params => astarray(AST::ObjectParam.new(
+ :param => stringobj("name"),
+ :value => stringobj("yayness")
+ ))
+ )
+ }
+
+ scope = Puppet::Parser::Scope.new
+
+ trans = nil
+ assert_nothing_raised {
+ trans = scope.evaluate(:ast => obj, :facts => {})
+ }
+
+ transobj = trans.shift
+ assert(transobj.name, "Name did not convert from param to name")
+ end
+
if defined? ActiveRecord
# Verify that our collection stuff works.
def test_collection