Class: Puppet::Pops::Evaluator::Runtime3Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/evaluator/runtime3_converter.rb

Overview

Converts nested 4x supported values to 3x values. This is required because resources and other objects do not know about the new type system, and does not support regular expressions. Unfortunately this has to be done for array and hash as well. A complication is that catalog types needs to be resolved against the scope.

Users should not create instances of this class. Instead the class methods Runtime3Converter.convert, Runtime3Converter.map_args, or Runtime3Converter.instance should be used

Direct Known Subclasses

Runtime3FunctionArgumentConverter

Constant Summary collapse

MAX_INTEGER =
Puppet::Pops::MAX_INTEGER
MIN_INTEGER =
Puppet::Pops::MIN_INTEGER

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(o, scope, undef_value) ⇒ Object

Converts 4x supported values to a 3x values. Same as calling Runtime3Converter.instance.convert(...)

Parameters:

Returns:

  • (Object)

    The converted value



33
34
35
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 33

def self.convert(o, scope, undef_value)
  @instance.convert(o, scope, undef_value)
end

.instanceRuntime3Converter

Returns the singleton instance of this class.

Returns:



39
40
41
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 39

def self.instance
  @instance
end

.map_args(args, scope, undef_value) ⇒ Array

Converts 4x supported values to a 3x values. Same as calling Runtime3Converter.instance.map_args(...)

Parameters:

  • args (Array)

    Array of values to convert

  • scope (Puppet::Parser::Scope)

    The scope to use when converting

  • undef_value (Object)

    The value that nil is converted to

Returns:

  • (Array)

    The converted values



22
23
24
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 22

def self.map_args(args, scope, undef_value)
  @instance.map_args(args, scope, undef_value)
end

Instance Method Details

#catalog_type_to_split_type_title(catalog_type) ⇒ Object

Produces an array with [type, title] from a PCatalogEntryType This method is used to produce the arguments for creation of reference resource instances (used when 3x is operating on a resource). Ensures that resources are not absolute.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 137

def catalog_type_to_split_type_title(catalog_type)
  split_type = catalog_type.is_a?(Puppet::Pops::Types::PTypeType) ? catalog_type.type : catalog_type
  case split_type
  when Puppet::Pops::Types::PClassType
    class_name = split_type.class_name
    ['class', class_name.nil? ? nil : class_name.sub(/^::/, '')]
  when Puppet::Pops::Types::PResourceType
    type_name = split_type.type_name
    title = split_type.title
    if type_name =~ /^(::)?[Cc]lass$/
      ['class', title.nil? ? nil : title.sub(/^::/, '')]
    else
      # Ensure that title is '' if nil
      # Resources with absolute name always results in error because tagging does not support leading ::
      [type_name.nil? ? nil : type_name.sub(/^::/, '').downcase, title.nil? ? '' : title]
    end
  else
    # TRANSLATORS 'PClassType' and 'PResourceType' are Puppet types and should not be translated
    raise ArgumentError, _("Cannot split the type %{class_name}, it represents neither a PClassType, nor a PResourceType.") %
                         { class_name: catalog_type.class }
  end
end

#convert(o, scope, undef_value) ⇒ Object

Converts a 4x supported value to a 3x value.

Parameters:

Returns:

  • (Object)

    The converted value



61
62
63
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 61

def convert(o, scope, undef_value)
  @convert_visitor.visit_this_2(self, o, scope, undef_value)
end

#convert_Array(o, scope, undef_value) ⇒ Object



95
96
97
98
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 95

def convert_Array(o, scope, undef_value)
  ic = @inner_converter
  o.map { |x| ic.convert(x, scope, undef_value) }
end

#convert_BigDecimal(o, scope, undef_value) ⇒ Object

Raises:



76
77
78
79
80
81
82
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 76

def convert_BigDecimal(o, scope, undef_value)
  # transform to same value float value if possible without any rounding error
  f = o.to_f
  return f unless f != o

  raise Puppet::Error, "Use of a Ruby BigDecimal value outside Puppet Float range, got '#{o}'"
end

#convert_Hash(o, scope, undef_value) ⇒ Object



100
101
102
103
104
105
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 100

def convert_Hash(o, scope, undef_value)
  result = {}
  ic = @inner_converter
  o.each { |k, v| result[ic.convert(k, scope, undef_value)] = ic.convert(v, scope, undef_value) }
  result
end

#convert_Integer(o, scope, undef_value) ⇒ Object

Raises:



69
70
71
72
73
74
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 69

def convert_Integer(o, scope, undef_value)
  return o unless o < MIN_INTEGER || o > MAX_INTEGER

  range_end = o > MAX_INTEGER ? 'max' : 'min'
  raise Puppet::Error, "Use of a Ruby Integer outside of Puppet Integer #{range_end} range, got '#{'0x%x' % o}'"
end

#convert_Iterator(o, scope, undef_value) ⇒ Object

Raises:



107
108
109
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 107

def convert_Iterator(o, scope, undef_value)
  raise Puppet::Error, _('Use of an Iterator is not supported here')
end

#convert_NilClass(o, scope, undef_value) ⇒ Object



65
66
67
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 65

def convert_NilClass(o, scope, undef_value)
  @inner ? nil : undef_value
end

#convert_Object(o, scope, undef_value) ⇒ Object



91
92
93
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 91

def convert_Object(o, scope, undef_value)
  o
end

#convert_PAnyType(o, scope, undef_value) ⇒ Object



117
118
119
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 117

def convert_PAnyType(o, scope, undef_value)
  o
end

#convert_PCatalogEntryType(o, scope, undef_value) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 121

def convert_PCatalogEntryType(o, scope, undef_value)
  # Since 4x does not support dynamic scoping, all names are absolute and can be
  # used as is (with some check/transformation/mangling between absolute/relative form
  # due to Puppet::Resource's idiosyncratic behavior where some references must be
  # absolute and others cannot be.
  # Thus there is no need to call scope.resolve_type_and_titles to do dynamic lookup.
  t, title = catalog_type_to_split_type_title(o)
  t = Runtime3ResourceSupport.find_resource_type(scope, t) unless t == 'class' || t == 'node'
  Puppet::Resource.new(t, title)
end

#convert_String(o, scope, undef_value) ⇒ Object



84
85
86
87
88
89
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 84

def convert_String(o, scope, undef_value)
  # Although wasteful, a dup is needed because user code may mutate these strings when applying
  # Resources. This does not happen when in server mode since it only uses Resources that are
  # in puppet core and those are all safe.
  o.frozen? && !Puppet.run_mode.server? ? o.dup : o
end

#convert_Symbol(o, scope, undef_value) ⇒ Object



111
112
113
114
115
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 111

def convert_Symbol(o, scope, undef_value)
  return o unless o == :undef

  !@inner ? undef_value : nil
end

#map_args(args, scope, undef_value) ⇒ Array

Converts 4x supported values to a 3x values.

Parameters:

  • args (Array)

    Array of values to convert

  • scope (Puppet::Parser::Scope)

    The scope to use when converting

  • undef_value (Object)

    The value that nil is converted to

Returns:

  • (Array)

    The converted values



50
51
52
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 50

def map_args(args, scope, undef_value)
  args.map { |a| convert(a, scope, undef_value) }
end