summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorBrice Figureau <brice-puppet@daysofwonder.com>2010-10-30 15:29:19 +0200
committerJames Turnbull <james@lovedthanlost.net>2010-11-10 05:53:56 +1100
commit4fa24bb4be7fd73af2a25884185f91b42f73d785 (patch)
tree35a0cde889acb6b05171c0646f83edd49f370f28 /lib/puppet
parentabb8c669378af8f3551418330751aaad80b2d6c2 (diff)
downloadpuppet-4fa24bb4be7fd73af2a25884185f91b42f73d785.tar.gz
puppet-4fa24bb4be7fd73af2a25884185f91b42f73d785.tar.xz
puppet-4fa24bb4be7fd73af2a25884185f91b42f73d785.zip
Fix #5127 - error when accessing array elements
Accesing an array with an integer index (ie $array[1]) is producing a ruby error: can't convert String into Integer This is because the array index is not properly converted to an number before the array element lookup is performed. Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/parser/ast/leaf.rb12
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb
index 090d75c4e..fcdd219d7 100644
--- a/lib/puppet/parser/ast/leaf.rb
+++ b/lib/puppet/parser/ast/leaf.rb
@@ -148,12 +148,20 @@ class Puppet::Parser::AST
key.respond_to?(:evaluate) ? key.safeevaluate(scope) : key
end
+ def array_index_or_key(object, key)
+ if object.is_a?(Array)
+ raise Puppet::ParserError, "#{key} is not an integer, but is used as an index of an array" unless key = Puppet::Parser::Scope.number?(key)
+ end
+ key
+ end
+
def evaluate(scope)
object = evaluate_container(scope)
+ accesskey = evaluate_key(scope)
raise Puppet::ParseError, "#{variable} is not an hash or array when accessing it with #{accesskey}" unless object.is_a?(Hash) or object.is_a?(Array)
- object[evaluate_key(scope)]
+ object[array_index_or_key(object, accesskey)]
end
# Assign value to this hashkey or array index
@@ -166,7 +174,7 @@ class Puppet::Parser::AST
end
# assign to hash or array
- object[accesskey] = value
+ object[array_index_or_key(object, accesskey)] = value
end
def to_s