Module: Puppet::Pops::Lookup
- Defined in:
- lib/puppet/pops/lookup.rb,
lib/puppet/pops/lookup/explainer.rb,
lib/puppet/pops/lookup/invocation.rb
Defined Under Namespace
Classes: ExplainDataProvider, ExplainGlobal, ExplainInterpolate, ExplainInvalidKey, ExplainMerge, ExplainMergeSource, ExplainModule, ExplainNode, ExplainPath, ExplainScope, ExplainTop, ExplainTreeNode, Explainer, Invocation
Constant Summary collapse
- LOOKUP_OPTIONS =
'lookup_options'.freeze
- GLOBAL =
'__global__'.freeze
Class Method Summary collapse
-
.lookup(name, value_type, default_value, has_default, merge, lookup_invocation) ⇒ Object
Performs a lookup in the configured scopes and optionally merges the default.
- .search_and_merge(name, lookup_invocation, merge) ⇒ Object private
Class Method Details
.lookup(name, value_type, default_value, has_default, merge, lookup_invocation) ⇒ Object
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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/puppet/pops/lookup.rb', line 22 def self.lookup(name, value_type, default_value, has_default, merge, lookup_invocation) value_type = Types::PDataType::DEFAULT if value_type.nil? names = name.is_a?(Array) ? name : [name] # find first name that yields a non-nil result and wrap it in a two element array # with name and value. not_found = MergeStrategy::NOT_FOUND override_values = lookup_invocation.override_values result_with_name = names.reduce([nil, not_found]) do |memo, key| value = override_values.include?(key) ? assert_type(["Value found for key '%s' in override hash", key], value_type, override_values[key]) : not_found catch(:no_such_key) { value = search_and_merge(key, lookup_invocation, merge) } if value.equal?(not_found) break [key, assert_type('Found value', value_type, value)] unless value.equal?(not_found) memo end # Use the 'default_values' hash as a last resort if nothing is found if result_with_name[1].equal?(not_found) default_values = lookup_invocation.default_values unless default_values.empty? result_with_name = names.reduce(result_with_name) do |memo, key| value = default_values.include?(key) ? assert_type(["Value found for key '%s' in default values hash", key], value_type, default_values[key]) : not_found memo = [key, value] break memo unless value.equal?(not_found) memo end end end answer = result_with_name[1] if answer.equal?(not_found) if block_given? answer = assert_type('Value returned from default block', value_type, yield(name)) elsif has_default answer = assert_type('Default value', value_type, default_value) else fail_lookup(names) end end answer end |
.search_and_merge(name, lookup_invocation, merge) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
64 65 66 |
# File 'lib/puppet/pops/lookup.rb', line 64 def self.search_and_merge(name, lookup_invocation, merge) return Puppet::DataProviders.lookup_adapter(lookup_invocation).lookup(name, lookup_invocation, merge) end |