Module: Puppet::Pops::Lookup

Defined in:
lib/puppet/pops/lookup.rb,
lib/puppet/pops/lookup/context.rb,
lib/puppet/pops/lookup/explainer.rb,
lib/puppet/pops/lookup/invocation.rb,
lib/puppet/pops/lookup/lookup_key.rb,
lib/puppet/pops/lookup/sub_lookup.rb,
lib/puppet/pops/lookup/data_adapter.rb,
lib/puppet/pops/lookup/hiera_config.rb,
lib/puppet/pops/lookup/data_provider.rb,
lib/puppet/pops/lookup/interpolation.rb,
lib/puppet/pops/lookup/lookup_adapter.rb,
lib/puppet/pops/lookup/function_provider.rb,
lib/puppet/pops/lookup/location_resolver.rb,
lib/puppet/pops/lookup/global_data_provider.rb,
lib/puppet/pops/lookup/module_data_provider.rb,
lib/puppet/pops/lookup/configured_data_provider.rb,
lib/puppet/pops/lookup/environment_data_provider.rb,
lib/puppet/pops/lookup/data_dig_function_provider.rb,
lib/puppet/pops/lookup/data_hash_function_provider.rb,
lib/puppet/pops/lookup/lookup_key_function_provider.rb

Defined Under Namespace

Modules: DataProvider, Interpolation, LocationResolver, SubLookup Classes: ConfiguredDataProvider, Context, DataAdapter, DataDigFunctionProvider, DataHashFunctionProvider, DebugExplainer, EnvironmentContext, EnvironmentDataProvider, ExplainDataProvider, ExplainGlobal, ExplainInterpolate, ExplainInvalidKey, ExplainKeySegment, ExplainLocation, ExplainMerge, ExplainMergeSource, ExplainModule, ExplainNode, ExplainScope, ExplainSubLookup, ExplainTop, ExplainTreeNode, Explainer, FunctionContext, FunctionProvider, GlobalDataProvider, HieraConfig, HieraConfigV3, HieraConfigV4, HieraConfigV5, Invocation, LookupAdapter, LookupKey, LookupKeyFunctionProvider, ModuleDataProvider, ResolvedLocation, ScopeLookupCollectingInvocation, V3BackendFunctionProvider, V3DataHashFunctionProvider, V3LookupKeyFunctionProvider, V4DataHashFunctionProvider

Constant Summary collapse

LOOKUP_OPTIONS =
'lookup_options'.freeze
GLOBAL =
'__global__'.freeze

Class Method Summary collapse

Class Method Details

.debug_preamble(names) ⇒ 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.



65
66
67
68
69
70
71
72
# File 'lib/puppet/pops/lookup.rb', line 65

def self.debug_preamble(names)
  if names.size == 1
    names = "'#{names[0]}'"
  else
    names = names.map { |n| "'#{n}'" }.join(', ')
  end
  "Lookup of #{names}"
end

.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.

Parameters:

  • name (String|Array<String>)

    The name or names to lookup

  • type (Types::PAnyType|nil)

    The expected type of the found value

  • default_value (Object)

    The value to use as default when no value is found

  • has_default (Boolean)

    Set to true if default_value is included (nil is a valid default_value)

  • merge (MergeStrategy, String, Hash<String,Object>, nil)

    Merge strategy or hash with strategy and options

  • lookup_invocation (Invocation)

    Invocation data containing scope, overrides, and defaults

Returns:

  • (Object)

    The found value



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
62
# File 'lib/puppet/pops/lookup.rb', line 22

def self.lookup(name, value_type, default_value, has_default, merge, lookup_invocation)
  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, false) } 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
      lookup_invocation.emit_debug_info(debug_preamble(names)) if Puppet[:debug]
      fail_lookup(names)
    end
  end
  lookup_invocation.emit_debug_info(debug_preamble(names)) if Puppet[:debug]
  answer
end

.search_and_merge(name, lookup_invocation, merge, apl = true) ⇒ 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.



75
76
77
78
79
# File 'lib/puppet/pops/lookup.rb', line 75

def self.search_and_merge(name, lookup_invocation, merge, apl = true)
  answer = lookup_invocation.lookup_adapter.lookup(name, lookup_invocation, merge)
  lookup_invocation.emit_debug_info("Automatic Parameter Lookup of '#{name}'") if apl && Puppet[:debug]
  answer
end