Module: EasyType::ExtendedParameter::ClassMethods

Defined in:
lib/puppet_x/enterprisemodules/oci/monkey_patches/easy_type.rb

Instance Method Summary collapse

Instance Method Details

#coerce(value) ⇒ Object

Basic implementation for coercing a value to the correct type.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet_x/enterprisemodules/oci/monkey_patches/easy_type.rb', line 19

def coerce(value)
  return value unless data_type && Puppet.const_defined?(:ResourceApi)

  # Get inside element type for the array ones when the elements are
  # checked one by one
  element_type = if array_data_type? && value && value_not_array?(value)
                   element_type_string = data_type.to_s.scan(/Array\[(.*)\]/).dig(0, 0)
                   element_type_string.chop! if element_type_string.count('[') < element_type_string.count(']')
                   Puppet::Pops::Types::TypeParser.singleton.parse(element_type_string)
                 else
                   data_type
                 end
  # Get resource name from the class
  resource_name = to_s.split('::')[2].downcase

  begin
    value = Puppet::ResourceApi::DataTypeHandling.mungify(
      element_type,
      value,
      "#{resource_name}.#{@name}", # type_name needs to be added for error reporting
      true
    )
    if is_a_boolean_kind?
      # work around https://tickets.puppetlabs.com/browse/PUP-2368
      value ? :true : :false # rubocop:disable Lint/BooleanSymbol
    else
      value
    end
  rescue Puppet::ResourceError => e
    #
    # Because a this point in time pupet does not
    # sync the data types to agents, we can run into issues
    # with missing data types. To bypass this, we just "eat"
    # these error's and let them pass.
    # See issue https://tickets.puppetlabs.com/browse/PUP-7197
    #
    # This means no type checking is done when the types are not available.
    #
    raise unless e.message =~ /references an unresolved type/

    value
  end
end