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

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

Overview

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



28
29
30
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 28

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

.instanceRuntime3Converter

Returns the singleton instance of this class.

Returns:



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

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



17
18
19
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 17

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.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 134

def catalog_type_to_split_type_title(catalog_type)
  split_type = catalog_type.is_a?(Puppet::Pops::Types::PType) ? catalog_type.type : catalog_type
  case split_type
    when Puppet::Pops::Types::PHostClassType
      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(/^::/, ''), title.nil? ? '' : title]
      end
    else
      raise ArgumentError, "Cannot split the type #{catalog_type.class}, it represents neither a PHostClassType, nor a PResourceType."
  end
end

#convert(o, scope, undef_value) ⇒ Object

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

Parameters:

Returns:

  • (Object)

    The converted value



56
57
58
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 56

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

#convert2_NilClass(o, scope, undef_value) ⇒ Object



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

def convert2_NilClass(o, scope, undef_value)
  :undef
end

#convert2_Symbol(o, scope, undef_value) ⇒ Object

The :undef symbol should not be converted when nested in arrays or hashes



109
110
111
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 109

def convert2_Symbol(o, scope, undef_value)
  o
end

#convert_Array(o, scope, undef_value) ⇒ Object Also known as: convert2_Array



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

def convert_Array(o, scope, undef_value)
  o.map {|x| convert2(x, scope, undef_value) }
end

#convert_Hash(o, scope, undef_value) ⇒ Object Also known as: convert2_Hash



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

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

#convert_NilClass(o, scope, undef_value) ⇒ Object



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

def convert_NilClass(o, scope, undef_value)
  undef_value
end

#convert_Object(o, scope, undef_value) ⇒ Object Also known as: convert2_Object



74
75
76
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 74

def convert_Object(o, scope, undef_value)
  o
end

#convert_PAnyType(o, scope, undef_value) ⇒ Object Also known as: convert2_PAnyType



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

def convert_PAnyType(o, scope, undef_value)
  o
end

#convert_PCatalogEntryType(o, scope, undef_value) ⇒ Object Also known as: convert2_PCatalogEntryType



118
119
120
121
122
123
124
125
126
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 118

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.

  Puppet::Resource.new(*catalog_type_to_split_type_title(o))
end

#convert_Regexp(o, scope, undef_value) ⇒ Object Also known as: convert2_Regexp



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

def convert_Regexp(o, scope, undef_value)
  # Puppet 3x cannot handle parameter values that are reqular expressions. Turn into regexp string in
  # source form
  o.inspect
end

#convert_String(o, scope, undef_value) ⇒ Object Also known as: convert2_String



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

def convert_String(o, scope, undef_value)
  # although wasteful, needed because user code may mutate these strings in Resources
  o.frozen? ? o.dup : o
end

#convert_Symbol(o, scope, undef_value) ⇒ Object



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

def convert_Symbol(o, scope, undef_value)
  case o
    # Support :undef since it may come from a 3x structure
    when :undef
      undef_value  # 3x wants undef as either empty string or :undef
    else
      o   # :default, and all others are verbatim since they are new in future evaluator
  end
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



45
46
47
# File 'lib/puppet/pops/evaluator/runtime3_converter.rb', line 45

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