class Puppet::Pops::Evaluator::Runtime3Converter
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
Constants
- MAX_INTEGER
- MIN_INTEGER
Public Class Methods
Converts 4x supported values to a 3x values. Same as calling Runtime3Converter.instance.convert(…)
@param o [Object]The value to convert @param scope [Puppet::Parser::Scope] The scope to use when converting @param undef_value [Object] The value that nil is converted to @return [Object] The converted value
# File lib/puppet/pops/evaluator/runtime3_converter.rb 32 def self.convert(o, scope, undef_value) 33 @instance.convert(o, scope, undef_value) 34 end
Returns the singleton instance of this class. @return [Runtime3Converter] The singleton instance
# File lib/puppet/pops/evaluator/runtime3_converter.rb 38 def self.instance 39 @instance 40 end
Converts 4x supported values to a 3x values. Same as calling Runtime3Converter.instance.map_args(…)
@param args [Array] Array of values to convert @param scope [Puppet::Parser::Scope] The scope to use when converting @param undef_value [Object] The value that nil is converted to @return [Array] The converted values
# File lib/puppet/pops/evaluator/runtime3_converter.rb 21 def self.map_args(args, scope, undef_value) 22 @instance.map_args(args, scope, undef_value) 23 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 158 def initialize(inner = false) 159 @inner = inner 160 @inner_converter = inner ? self : self.class.new(true) 161 @convert_visitor = Puppet::Pops::Visitor.new(self, 'convert', 2, 2) 162 end
Public Instance Methods
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.
# File lib/puppet/pops/evaluator/runtime3_converter.rb 133 def catalog_type_to_split_type_title(catalog_type) 134 split_type = catalog_type.is_a?(Puppet::Pops::Types::PTypeType) ? catalog_type.type : catalog_type 135 case split_type 136 when Puppet::Pops::Types::PClassType 137 class_name = split_type.class_name 138 ['class', class_name.nil? ? nil : class_name.sub(/^::/, '')] 139 when Puppet::Pops::Types::PResourceType 140 type_name = split_type.type_name 141 title = split_type.title 142 if type_name =~ /^(::)?[Cc]lass$/ 143 ['class', title.nil? ? nil : title.sub(/^::/, '')] 144 else 145 # Ensure that title is '' if nil 146 # Resources with absolute name always results in error because tagging does not support leading :: 147 [type_name.nil? ? nil : type_name.sub(/^::/, '').downcase, title.nil? ? '' : title] 148 end 149 else 150 #TRANSLATORS 'PClassType' and 'PResourceType' are Puppet types and should not be translated 151 raise ArgumentError, _("Cannot split the type %{class_name}, it represents neither a PClassType, nor a PResourceType.") % 152 { class_name: catalog_type.class } 153 end 154 end
Converts a 4x supported value to a 3x value.
@param o [Object]The value to convert @param scope [Puppet::Parser::Scope] The scope to use when converting @param undef_value [Object] The value that nil is converted to @return [Object] The converted value
# File lib/puppet/pops/evaluator/runtime3_converter.rb 60 def convert(o, scope, undef_value) 61 @convert_visitor.visit_this_2(self, o, scope, undef_value) 62 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 92 def convert_Array(o, scope, undef_value) 93 ic = @inner_converter 94 o.map {|x| ic.convert(x, scope, undef_value) } 95 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 74 def convert_BigDecimal(o, scope, undef_value) 75 # transform to same value float value if possible without any rounding error 76 f = o.to_f 77 return f unless f != o 78 raise Puppet::Error, "Use of a Ruby BigDecimal value outside Puppet Float range, got '#{o}'" 79 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 97 def convert_Hash(o, scope, undef_value) 98 result = {} 99 ic = @inner_converter 100 o.each {|k,v| result[ic.convert(k, scope, undef_value)] = ic.convert(v, scope, undef_value) } 101 result 102 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 68 def convert_Integer(o, scope, undef_value) 69 return o unless o < MIN_INTEGER || o > MAX_INTEGER 70 range_end = o > MAX_INTEGER ? 'max' : 'min' 71 raise Puppet::Error, "Use of a Ruby Integer outside of Puppet Integer #{range_end} range, got '#{"0x%x" % o}'" 72 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 104 def convert_Iterator(o, scope, undef_value) 105 raise Puppet::Error, _('Use of an Iterator is not supported here') 106 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 64 def convert_NilClass(o, scope, undef_value) 65 @inner ? nil : undef_value 66 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 88 def convert_Object(o, scope, undef_value) 89 o 90 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 113 def convert_PAnyType(o, scope, undef_value) 114 o 115 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 117 def convert_PCatalogEntryType(o, scope, undef_value) 118 # Since 4x does not support dynamic scoping, all names are absolute and can be 119 # used as is (with some check/transformation/mangling between absolute/relative form 120 # due to Puppet::Resource's idiosyncratic behavior where some references must be 121 # absolute and others cannot be. 122 # Thus there is no need to call scope.resolve_type_and_titles to do dynamic lookup. 123 t, title = catalog_type_to_split_type_title(o) 124 t = Runtime3ResourceSupport.find_resource_type(scope, t) unless t == 'class' || t == 'node' 125 Puppet::Resource.new(t, title) 126 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 81 def convert_String(o, scope, undef_value) 82 # Although wasteful, a dup is needed because user code may mutate these strings when applying 83 # Resources. This does not happen when in server mode since it only uses Resources that are 84 # in puppet core and those are all safe. 85 o.frozen? && !Puppet.run_mode.server? ? o.dup : o 86 end
# File lib/puppet/pops/evaluator/runtime3_converter.rb 108 def convert_Symbol(o, scope, undef_value) 109 return o unless o == :undef 110 !@inner ? undef_value : nil 111 end
Converts 4x supported values to a 3x values.
@param args [Array] Array of values to convert @param scope [Puppet::Parser::Scope] The scope to use when converting @param undef_value [Object] The value that nil is converted to @return [Array] The converted values
# File lib/puppet/pops/evaluator/runtime3_converter.rb 49 def map_args(args, scope, undef_value) 50 args.map {|a| convert(a, scope, undef_value) } 51 end