module Puppet::Pops::Lookup

Constants

GLOBAL
LOOKUP_OPTIONS

Public Class Methods

debug_preamble(names) click to toggle source

@api private

   # File lib/puppet/pops/lookup.rb
66 def self.debug_preamble(names)
67   if names.size == 1
68     names = "'#{names[0]}'"
69   else
70     names = names.map { |n| "'#{n}'" }.join(', ')
71   end
72   "Lookup of #{names}"
73 end
lookup(name, value_type, default_value, has_default, merge, lookup_invocation) { |name| ... } click to toggle source

Performs a lookup in the configured scopes and optionally merges the default.

This is a backing function and all parameters are assumed to have been type checked. See puppet/functions/lookup.rb for full documentation and all parameter combinations.

@param name [String|Array<String>] The name or names to lookup @param type [Types::PAnyType|nil] The expected type of the found value @param default_value [Object] The value to use as default when no value is found @param has_default [Boolean] Set to true if default_value is included (nil is a valid default_value) @param merge [MergeStrategy,String,Hash<String,Object>,nil] Merge strategy or hash with strategy and options @param lookup_invocation [Invocation] Invocation data containing scope, overrides, and defaults @return [Object] The found value

   # File lib/puppet/pops/lookup.rb
23 def self.lookup(name, value_type, default_value, has_default, merge, lookup_invocation)
24   names = name.is_a?(Array) ? name : [name]
25 
26   # find first name that yields a non-nil result and wrap it in a two element array
27   # with name and value.
28   not_found = MergeStrategy::NOT_FOUND
29   override_values = lookup_invocation.override_values
30   result_with_name = names.reduce([nil, not_found]) do |memo, key|
31     value = override_values.include?(key) ? assert_type(["Value found for key '%s' in override hash", key], value_type, override_values[key]) : not_found
32     catch(:no_such_key) { value = search_and_merge(key, lookup_invocation, merge, false) } if value.equal?(not_found)
33     break [key, assert_type('Found value', value_type, value)] unless value.equal?(not_found)
34     memo
35   end
36 
37   # Use the 'default_values' hash as a last resort if nothing is found
38   if result_with_name[1].equal?(not_found)
39     default_values = lookup_invocation.default_values
40     unless default_values.empty?
41       result_with_name = names.reduce(result_with_name) do |memo, key|
42         value = default_values.include?(key) ? assert_type(["Value found for key '%s' in default values hash", key], value_type, default_values[key]) : not_found
43         memo = [key, value]
44         break memo unless value.equal?(not_found)
45         memo
46       end
47     end
48   end
49 
50   answer = result_with_name[1]
51   if answer.equal?(not_found)
52     if block_given?
53       answer = assert_type('Value returned from default block', value_type, yield(name))
54     elsif has_default
55       answer = assert_type('Default value', value_type, default_value)
56     else
57       lookup_invocation.emit_debug_info(debug_preamble(names)) if Puppet[:debug]
58       fail_lookup(names)
59     end
60   end
61   lookup_invocation.emit_debug_info(debug_preamble(names)) if Puppet[:debug]
62   answer
63 end
search_and_merge(name, lookup_invocation, merge, apl = true) click to toggle source

@api private

   # File lib/puppet/pops/lookup.rb
76 def self.search_and_merge(name, lookup_invocation, merge, apl = true)
77   answer = lookup_invocation.lookup_adapter.lookup(name, lookup_invocation, merge)
78   lookup_invocation.emit_debug_info("Automatic Parameter Lookup of '#{name}'") if apl && Puppet[:debug]
79   answer
80 end

Private Class Methods

assert_type(subject, type, value) click to toggle source
   # File lib/puppet/pops/lookup.rb
82 def self.assert_type(subject, type, value)
83   type ? Types::TypeAsserter.assert_instance_of(subject, type, value) : value
84 end
fail_lookup(names) click to toggle source
   # File lib/puppet/pops/lookup.rb
87 def self.fail_lookup(names)
88   raise Puppet::DataBinding::LookupError,
89         n_("Function lookup() did not find a value for the name '%{name}'",
90            "Function lookup() did not find a value for any of the names [%{name_list}]", names.size
91           ) % { name: names[0], name_list: names.map { |n| "'#{n}'" }.join(', ') }
92 end