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