Sha256: 367fbc854c6118a9429b32f5e2b15927730c94eb32b4c1207bb4a5271b0cc5f6

Contents?: true

Size: 1.93 KB

Versions: 20

Compression:

Stored size: 1.93 KB

Contents

require_relative 'function_provider'

module Puppet::Pops
module Lookup
# @api private
class DataDigFunctionProvider < FunctionProvider
  TAG = 'data_dig'.freeze

  # Performs a lookup with the assumption that a recursive check has been made.
  #
  # @param key [LookupKey] The key to lookup
  # @param lookup_invocation [Invocation] The current lookup invocation
  # @param merge [MergeStrategy,String,Hash{String => Object},nil] Merge strategy, merge strategy name, strategy and options hash, or nil (implies "first found")
  # @return [Object] the found object
  # @throw :no_such_key when the object is not found
  def unchecked_key_lookup(key, lookup_invocation, merge)
    segments = key.to_a
    lookup_invocation.with(:data_provider, self) do
      MergeStrategy.strategy(merge).lookup(locations, lookup_invocation) do |location|
        if location.nil?
          value = data_dig(key, segments, lookup_invocation, nil)
          lookup_invocation.report_found(key, validate_data_value(self, value))
          key.undig(value)
        else
          lookup_invocation.with(:location, location) do
            if location.exist?
              value = data_dig(key, segments, lookup_invocation, location.location)
              lookup_invocation.report_found(key, validate_data_value(self, value))
              key.undig(value)
            else
              lookup_invocation.report_path_not_found
              throw :no_such_key
            end
          end
        end
      end
    end
  end

  private

  def data_dig(key, segments, lookup_invocation, location)
    ctx = function_context(lookup_invocation, location)
    ctx.data_hash ||= {}
    catch(:no_such_key) do
      hash = ctx.data_hash
      hash[key] = ctx.function.call(lookup_invocation.scope, segments, options(location), Context.new(ctx, lookup_invocation)) unless hash.include?(key)
      return hash[key]
    end
    lookup_invocation.report_not_found(key)
    throw :no_such_key
  end
end
end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
puppet-4.9.4 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.4-x86-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.4-x64-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.4-universal-darwin lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.3 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.3-x86-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.3-x64-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.3-universal-darwin lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.2 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.2-x86-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.2-x64-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.2-universal-darwin lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.1 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.1-x86-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.1-x64-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.1-universal-darwin lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.0 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.0-x86-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.0-x64-mingw32 lib/puppet/pops/lookup/data_dig_function_provider.rb
puppet-4.9.0-universal-darwin lib/puppet/pops/lookup/data_dig_function_provider.rb