Class: Hiera::Interpolate Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/interpolate.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

RX_INTERPOLATION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/%\{([^\}]*)\}/
RX_ONLY_INTERPOLATION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/^%\{([^\}]*)\}$/
RX_METHOD_AND_ARG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/^(\w+)\(([^)]*)\)$/
EMPTY_INTERPOLATIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  '' => true,
  '::' => true,
  '""' => true,
  "''" => true,
  '"::"' => true,
  "'::'" => true
}.freeze
INTERPOLATION_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  'hiera' => :hiera_interpolate,
  'scope' => :scope_interpolate,
  'literal' => :literal_interpolate,
  'alias' => :alias_interpolate
}.freeze
INTERPOLATION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Deprecated.

These two patterns are never used but kept here anyway since they used to be public and therefore must be considered API. The class is now marked @api private and these should be removed in a future version

/%\{([^\}]*)\}/
METHOD_INTERPOLATION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Deprecated.
/%\{(scope|hiera|literal|alias)\(['"]([^"']*)["']\)\}/

Class Method Summary collapse

Class Method Details

.alias_interpolate(data, key, scope, extra_data, context) ⇒ 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.



116
117
118
# File 'lib/hiera/interpolate.rb', line 116

def alias_interpolate(data, key, scope, extra_data, context)
  Hiera::Backend.lookup(key, nil, scope, context[:order_override], :priority, context)
end

.do_interpolation(data, scope, extra_data, context) ⇒ 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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hiera/interpolate.rb', line 61

def do_interpolation(data, scope, extra_data, context)
  if data.is_a?(String) && (match = data.match(RX_INTERPOLATION))
    interpolation_variable = match[1]

    # HI-494
    return ['', nil] if EMPTY_INTERPOLATIONS[interpolation_variable.strip]

    context[:recurse_guard].check(interpolation_variable) do
      interpolate_method, key = get_interpolation_method_and_key(interpolation_variable, context)
      interpolated_data = send(interpolate_method, data, key, scope, extra_data, context)

      # Halt recursion if we encounter a literal.
      return [interpolated_data, interpolate_method] if interpolate_method == :literal_interpolate

      [do_interpolation(interpolated_data, scope, extra_data, context)[0], interpolate_method]
    end
  else
    [data, nil]
  end
end

.get_interpolation_method_and_key(interpolation_variable, context) ⇒ 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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hiera/interpolate.rb', line 83

def get_interpolation_method_and_key(interpolation_variable, context)
  if (match = interpolation_variable.match(RX_METHOD_AND_ARG))
    Hiera.warn('Use of interpolation methods in hiera configuration file is deprecated') if context[:is_interpolate_config]
    method = match[1]
    method_sym = INTERPOLATION_METHODS[method]
    raise Hiera::InterpolationInvalidValue, "Invalid interpolation method '#{method}'" unless method_sym
    arg = match[2]
    match_data = arg.match(Hiera::QUOTED_KEY)
    raise Hiera::InterpolationInvalidValue, "Argument to interpolation method '#{method}' must be quoted, got '#{arg}'" unless match_data
    [method_sym, match_data[1] || match_data[2]]
  else
    [:scope_interpolate, interpolation_variable]
  end
end

.hiera_interpolate(data, key, scope, extra_data, context) ⇒ 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.



106
107
108
# File 'lib/hiera/interpolate.rb', line 106

def hiera_interpolate(data, key, scope, extra_data, context)
  Hiera::Backend.lookup(key, nil, scope, context[:order_override], :priority, context)
end

.interpolate(data, scope, extra_data, context) ⇒ 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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hiera/interpolate.rb', line 40

def interpolate(data, scope, extra_data, context)
  if data.is_a?(String)
    # Wrapping do_interpolation in a gsub block ensures we process
    # each interpolation site in isolation using separate recursion guards.
    new_context = context.nil? ? {} : context.clone
    new_context[:recurse_guard] ||= Hiera::RecursiveGuard.new
    data.gsub(RX_INTERPOLATION) do |match|
      (interp_val, interpolate_method) = do_interpolation(match, scope, extra_data, new_context)

      if (interpolate_method == :alias_interpolate) && !interp_val.is_a?(String)
        return interp_val if data.match(RX_ONLY_INTERPOLATION)
        raise Hiera::InterpolationInvalidValue, "Cannot call alias in the string context"
      else
        interp_val
      end
    end
  else
    data
  end
end

.literal_interpolate(data, key, scope, extra_data, context) ⇒ 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.



111
112
113
# File 'lib/hiera/interpolate.rb', line 111

def literal_interpolate(data, key, scope, extra_data, context)
  key
end

.scope_interpolate(data, key, scope, extra_data, context) ⇒ 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.



99
100
101
102
103
# File 'lib/hiera/interpolate.rb', line 99

def scope_interpolate(data, key, scope, extra_data, context)
  segments = Hiera::Util.split_key(key) { |problem| Hiera::InterpolationInvalidValue.new("#{problem} in interpolation expression: #{data}") }
  catch(:no_such_key) { return Hiera::Backend.qualified_lookup(segments, scope, key) }
  catch(:no_such_key) { Hiera::Backend.qualified_lookup(segments, extra_data, key) }
end