diff options
author | Reid Vandewiele <reid@puppetlabs.com> | 2017-06-30 14:01:41 -0700 |
---|---|---|
committer | Reid Vandewiele <reid@puppetlabs.com> | 2017-06-30 14:02:56 -0700 |
commit | 409a974095a3f5b637e091494b5d14b451c5de78 (patch) | |
tree | 36da2b0aa9f57b11ff798933f006a30f369e6d82 /lib/puppet | |
parent | 740ca7dc8053be93e43392ca61b2f308c0596d19 (diff) |
(FACT-932) Allow use of fact() on other hashes
Because sometimes people want to use an alternative data set, but treat
it like it's a set of facts.
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/functions/fact.rb | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb index dfb048b..48d0d3c 100644 --- a/lib/puppet/functions/fact.rb +++ b/lib/puppet/functions/fact.rb @@ -18,22 +18,27 @@ Puppet::Functions.create_function(:fact) do param 'String', :fact_name end - def to_dot_syntax(array_path) - array_path.map do |string| - string.include?('.') ? %Q{"#{string}"} : string - end.join('.') + dispatch :alternative do + param 'Hash', :fact_hash + param 'String', :fact_name end def fact(fact_name) - facts = closure_scope['facts'] + dot_dig(closure_scope['facts'], fact_name) + end + + def alternative(alternative_hash, fact_name) + dot_dig(alternative_hash, fact_name) + end + def dot_dig(fact_hash, fact_name) # Transform the dot-notation string into an array of paths to walk. Make # sure to correctly extract double-quoted values containing dots as single # elements in the path. path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } walked_path = [] - path.reduce(facts) do |d, k| + path.reduce(fact_hash) do |d, k| return nil if d.nil? || k.nil? case @@ -55,4 +60,10 @@ Puppet::Functions.create_function(:fact) do result end end + + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') + end end |