From 164f1ce85bad49c7e197deeb452828c94539e06e Mon Sep 17 00:00:00 2001 From: Brice Figureau Date: Sat, 14 Nov 2009 19:51:15 +0100 Subject: Allow adding single key to hashes This patch allow this syntax: $hash[mykey] = 12 If the key already exist an error is raised. Hashes are essentially write only, like puppet variables. Signed-off-by: Brice Figureau --- lib/puppet/parser/ast/leaf.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'lib/puppet/parser/ast/leaf.rb') diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb index 206ea5cbe..0f427ef37 100644 --- a/lib/puppet/parser/ast/leaf.rb +++ b/lib/puppet/parser/ast/leaf.rb @@ -142,17 +142,36 @@ class Puppet::Parser::AST class HashOrArrayAccess < AST::Leaf attr_accessor :variable, :key - def evaluate(scope) + def evaluate_container(scope) container = variable.respond_to?(:evaluate) ? variable.safeevaluate(scope) : variable - object = (container.is_a?(Hash) or container.is_a?(Array)) ? container : scope.lookupvar(container) + (container.is_a?(Hash) or container.is_a?(Array)) ? container : scope.lookupvar(container) + end - accesskey = key.respond_to?(:evaluate) ? key.safeevaluate(scope) : key + def evaluate_key(scope) + key.respond_to?(:evaluate) ? key.safeevaluate(scope) : key + end + + def evaluate(scope) + object = evaluate_container(scope) unless object.is_a?(Hash) or object.is_a?(Array) raise Puppet::ParseError, "#{variable} is not an hash or array when accessing it with #{accesskey}" end - return object[accesskey] + return object[evaluate_key(scope)] + end + + # Assign value to this hashkey or array index + def assign(scope, value) + object = evaluate_container(scope) + accesskey = evaluate_key(scope) + + if object.is_a?(Hash) and object.include?(accesskey) + raise Puppet::ParseError, "Assigning to the hash '#{variable}' with an existing key '#{accesskey}' is forbidden" + end + + # assign to hash or array + object[accesskey] = value end def to_s -- cgit