Module: EasyType::ExtendedParameter::ClassMethods

Defined in:
lib/easy_type/extended_parameter.rb

Instance Method Summary collapse

Instance Method Details

#aliasvalue(*values) ⇒ Object



116
117
118
119
# File 'lib/easy_type/extended_parameter.rb', line 116

def aliasvalue(*values)
  return if resource_api_available? && data_type
  super
end

#array_data_type?Boolean

Check if the main declared data type is array. Because it is seldom a regular array we have extended the check to be optional and either be ‘absent’ or an array of anything.

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/easy_type/extended_parameter.rb', line 97

def array_data_type?
  array_type = Puppet::Pops::Types::TypeParser.singleton.parse("Optional[Variant[Enum['absent'],Array[Any]]]")
  array_type.assignable?(data_type)
end

#coerce(value) ⇒ Object

Basic implementation for coercing a value to the correct type.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/easy_type/extended_parameter.rb', line 38

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)
                   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

#data_type(type = nil) ⇒ Object

Parse data_type provided in parameter. In ex. value_data(‘Boolean’)



106
107
108
109
# File 'lib/easy_type/extended_parameter.rb', line 106

def data_type(type = nil)
  return @data_type if type == nil
  @data_type ||= Puppet::Pops::Types::TypeParser.singleton.parse(type)
end

#is_a_boolean_kind?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/easy_type/extended_parameter.rb', line 81

def is_a_boolean_kind?
  return true if data_type.is_a?(Puppet::Pops::Types::PBooleanType)
  return true if data_type.is_a?(Puppet::Pops::Types::POptionalType) && data_type.type.is_a?(Puppet::Pops::Types::PBooleanType)
  false
end

#newvalues(*names) ⇒ Object



111
112
113
114
# File 'lib/easy_type/extended_parameter.rb', line 111

def newvalues(*names)
  return if resource_api_available? && data_type
  super
end

#value_not_array?(value) ⇒ Boolean

Check if the provided value is not an array

Returns:

  • (Boolean)


89
90
91
# File 'lib/easy_type/extended_parameter.rb', line 89

def value_not_array?(value)
  !value.is_a?(Array)
end