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

@api private

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

Private Class Methods

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