Module: Puppet::Pops::Types::TypeFactory

Defined in:
lib/puppet/pops/types/type_factory.rb

Overview

Helper module that makes creation of type objects simpler.

Constant Summary collapse

Types =
Puppet::Pops::Types

Class Method Summary collapse

Class Method Details

.all_callablesObject

Produces a CallableType matching all callables



232
233
234
# File 'lib/puppet/pops/types/type_factory.rb', line 232

def self.all_callables()
  return Puppet::Pops::Types::PCallableType.new
end

.anyObject

Produces the Any type



171
172
173
# File 'lib/puppet/pops/types/type_factory.rb', line 171

def self.any()
  Types::PAnyType.new()
end

.array_of(o) ⇒ Object

Produces a type for Array where o is either a type, or an instance for which a type is inferred.



361
362
363
364
365
# File 'lib/puppet/pops/types/type_factory.rb', line 361

def self.array_of(o)
  type = Types::PArrayType.new()
  type.element_type = type_of(o)
  type
end

.array_of_dataObject

Produces a type for Array



381
382
383
384
385
# File 'lib/puppet/pops/types/type_factory.rb', line 381

def self.array_of_data()
  type = Types::PArrayType.new()
  type.element_type = data()
  type
end

.booleanObject

Produces the Boolean type



164
165
166
# File 'lib/puppet/pops/types/type_factory.rb', line 164

def self.boolean()
  Types::PBooleanType.new()
end

.callable(*params) ⇒ Object

Produces a Callable type with one signature without support for a block Use #with_block, or #with_optional_block to add a block to the callable If no parameters are given, the Callable will describe a signature that does not accept parameters. To create a Callable that matches all callables use #all_callables.

The params is a list of types, where the three last entries may be optionally followed by min, max count, and a Callable which is taken as the block_type. If neither min or max are specified the parameters must match exactly. A min < params.size means that the difference are optional. If max > params.size means that the last type repeats. if max is :default, the max value is unbound (infinity).

Params are given as a sequence of arguments to #type_of.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/puppet/pops/types/type_factory.rb', line 252

def self.callable(*params)
  if Puppet::Pops::Types::TypeCalculator.is_kind_of_callable?(params.last)
    last_callable = true
  end
  block_t = last_callable ? params.pop : nil

  # compute a size_type for the signature based on the two last parameters
  if is_range_parameter?(params[-2]) && is_range_parameter?(params[-1])
    size_type = range(params[-2], params[-1])
    params = params[0, params.size - 2]
  elsif is_range_parameter?(params[-1])
    size_type = range(params[-1], :default)
    params = params[0, params.size - 1]
  end

  types = params.map {|p| type_of(p) }

  # If the specification requires types, and none were given, a Unit type is used
  if types.empty? && !size_type.nil? && size_type.range[1] > 0
    types << Types::PUnitType.new
  end
  # create a signature
  callable_t = Types::PCallableType.new()
  tuple_t = tuple(*types)
  tuple_t.size_type = size_type unless size_type.nil?
  callable_t.param_types = tuple_t

  callable_t.block_type = block_t
  callable_t
end

.catalog_entryObject

Produces an instance of the abstract type PCatalogEntryType



320
321
322
# File 'lib/puppet/pops/types/type_factory.rb', line 320

def self.catalog_entry()
  Types::PCatalogEntryType.new()
end

.collectionObject

Produces the abstract type Collection



296
297
298
# File 'lib/puppet/pops/types/type_factory.rb', line 296

def self.collection()
  Types::PCollectionType.new()
end

.constrain_size(collection_t, from, to) ⇒ Object

Sets the accepted size range of a collection if something other than the default 0 to Infinity is wanted. The semantics for from/to are the same as for #range



480
481
482
483
# File 'lib/puppet/pops/types/type_factory.rb', line 480

def self.constrain_size(collection_t, from, to)
  collection_t.size_type = range(from, to)
  collection_t
end

.dataObject

Produces the Data type



303
304
305
# File 'lib/puppet/pops/types/type_factory.rb', line 303

def self.data()
  Types::PDataType.new()
end

.defaultObject

Creates an instance of the Default type



315
316
317
# File 'lib/puppet/pops/types/type_factory.rb', line 315

def self.default()
  Types::PDefaultType.new()
end

.enum(*values) ⇒ Object

Produces the Enum type, optionally with specific string values



92
93
94
95
96
# File 'lib/puppet/pops/types/type_factory.rb', line 92

def self.enum(*values)
  t = Types::PEnumType.new()
  values.each {|v| t.addValues(v) }
  t
end

.floatObject

Produces the Float type



47
48
49
# File 'lib/puppet/pops/types/type_factory.rb', line 47

def self.float()
  Types::PFloatType.new()
end

.float_range(from, to) ⇒ Object

Produces a Float range type

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/pops/types/type_factory.rb', line 33

def self.float_range(from, to)
  # NOTE! Do not merge the following line to 4.x. It has the same check in the initialize method
  raise ArgumentError, "'from' must be less or equal to 'to'. Got (#{from}, #{to}" if from.is_a?(Numeric) && to.is_a?(Numeric) && from > to

  t = Types::PFloatType.new()
  # optimize eq with symbol (faster when it is left)
  t.from = Float(from) unless :default == from || from.nil?
  t.to = Float(to) unless :default == to || to.nil?
  t
end

.hash_of(value, key = scalar()) ⇒ Object

Produces a type for Hash[Scalar, o] where o is either a type, or an instance for which a type is inferred.



371
372
373
374
375
376
# File 'lib/puppet/pops/types/type_factory.rb', line 371

def self.hash_of(value, key = scalar())
  type = Types::PHashType.new()
  type.key_type = type_of(key)
  type.element_type = type_of(value)
  type
end

.hash_of_dataObject

Produces a type for Hash[Scalar, Data]



390
391
392
393
394
395
# File 'lib/puppet/pops/types/type_factory.rb', line 390

def self.hash_of_data()
  type = Types::PHashType.new()
  type.key_type = scalar()
  type.element_type = data()
  type
end

.host_class(class_name = nil) ⇒ Object

Produces PHostClassType with a string class_name. A PHostClassType with nil or empty name is compatible with any other PHostClassType. A PHostClassType with a given name is only compatible with a PHostClassType with the same name.



349
350
351
352
353
354
355
# File 'lib/puppet/pops/types/type_factory.rb', line 349

def self.host_class(class_name = nil)
  type = Types::PHostClassType.new()
  unless class_name.nil?
    type.class_name = class_name.sub(/^::/, '')
  end
  type
end

.integerObject

Produces the Integer type



12
13
14
# File 'lib/puppet/pops/types/type_factory.rb', line 12

def self.integer()
  Types::PIntegerType.new()
end

.is_range_parameter?(t) ⇒ Boolean

Returns true if the given type t is of valid range parameter type (integer or literal default).

Returns:

  • (Boolean)


487
488
489
# File 'lib/puppet/pops/types/type_factory.rb', line 487

def self.is_range_parameter?(t)
  t.is_a?(Integer) || t == 'default' || :default == t
end

.label(t) ⇒ Object

Produces a string representation of the type



61
62
63
# File 'lib/puppet/pops/types/type_factory.rb', line 61

def self.label(t)
  @type_calculator.string(t)
end

.not_undef(inst_type = nil) ⇒ Puppet::Pops::Types::PNotUndefType

Produces a type for NotUndef The given ‘inst_type’ can be a string in which case it will be converted into the type String.

Parameters:

  • inst_type (Type, String) (defaults to: nil)

    the type to qualify

Returns:



406
407
408
409
410
411
# File 'lib/puppet/pops/types/type_factory.rb', line 406

def self.not_undef(inst_type = nil)
  type = Types::PNotUndefType.new()
  inst_type = string(inst_type) if inst_type.is_a?(String)
  type.type = inst_type
  type
end

.numericObject

Produces the Numeric type



54
55
56
# File 'lib/puppet/pops/types/type_factory.rb', line 54

def self.numeric()
  Types::PNumericType.new()
end

.optional(optional_type = nil) ⇒ POptionalType

Produces the Optional type, i.e. a short hand for Variant[T, Undef] If the given ‘optional_type’ argument is a String, then it will be converted into a String type that represents that string.

Parameters:

  • optional_type (String, PAnyType, nil) (defaults to: nil)

    the optional type

Returns:



83
84
85
86
87
# File 'lib/puppet/pops/types/type_factory.rb', line 83

def self.optional(optional_type = nil)
  t = Types::POptionalType.new
  t.optional_type = optional_type.is_a?(String) ? string(optional_type) : type_of(optional_type)
  t
end

.pattern(*regular_expressions) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/puppet/pops/types/type_factory.rb', line 189

def self.pattern(*regular_expressions)
  t = Types::PPatternType.new()
  regular_expressions.each do |re|
    case re
    when String
      re_T = Types::PRegexpType.new()
      re_T.pattern = re
      re_T.regexp()  # compile it to catch errors
      t.addPatterns(re_T)

    when Regexp
      re_T = Types::PRegexpType.new()
      # Regep.to_s includes options user did not enter and does not escape source
      # to work either as a string or as a // regexp. The inspect method does a better
      # job, but includes the //
      re_T.pattern = re.inspect[1..-2]
      t.addPatterns(re_T)

    when Types::PRegexpType
      t.addPatterns(re.copy)

    when Types::PPatternType
      re.patterns.each do |p|
        t.addPatterns(p.copy)
      end

   else
     raise ArgumentError, "Only String, Regexp, Pattern-Type, and Regexp-Type are allowed: got '#{re.class}"
    end
  end
  t
end

.range(from, to) ⇒ Object

Produces an Integer range type

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
# File 'lib/puppet/pops/types/type_factory.rb', line 19

def self.range(from, to)
  # NOTE! Do not merge the following line to 4.x. It has the same check in the initialize method
  raise ArgumentError, "'from' must be less or equal to 'to'. Got (#{from}, #{to}" if from.is_a?(Numeric) && to.is_a?(Numeric) && from > to

  t = Types::PIntegerType.new()
  # optimize eq with symbol (faster when it is left)
  t.from = from unless (:default == from || from == 'default')
  t.to = to unless (:default == to || to == 'default')
  t
end

.regexp(pattern = nil) ⇒ Object

Produces the Regexp type

Parameters:

  • pattern (Regexp, String, nil) (defaults to: nil)

    (nil) The regular expression object or a regexp source string, or nil for bare type



180
181
182
183
184
185
186
187
# File 'lib/puppet/pops/types/type_factory.rb', line 180

def self.regexp(pattern = nil)
  t = Types::PRegexpType.new()
  if pattern
    t.pattern = pattern.is_a?(Regexp) ? pattern.inspect[1..-2] : pattern
  end
  t.regexp() unless pattern.nil? # compile pattern to catch errors
  t
end

.resource(type_name = nil, title = nil) ⇒ Object

Produces a PResourceType with a String type_name A PResourceType with a nil or empty name is compatible with any other PResourceType. A PResourceType with a given name is only compatible with a PResourceType with the same name. (There is no resource-type subtyping in Puppet (yet)).



329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/puppet/pops/types/type_factory.rb', line 329

def self.resource(type_name = nil, title = nil)
  type = Types::PResourceType.new()
  type_name = type_name.type_name if type_name.is_a?(Types::PResourceType)
  type_name = type_name.downcase unless type_name.nil?
  type.type_name = type_name
  unless type_name.nil? || type_name =~ Puppet::Pops::Patterns::CLASSREF
    raise ArgumentError, "Illegal type name '#{type.type_name}'"
  end
  if type_name.nil? && !title.nil?
    raise ArgumentError, "The type name cannot be nil, if title is given"
  end
  type.title = title
  type
end

.ruby(o) ⇒ Object .ruby(o) ⇒ Object

Note:

To get the type for the class’ class use ‘TypeCalculator.infer©`

Produces a type for a class or infers a type for something that is not a class

Overloads:

  • .ruby(o) ⇒ Object

    Parameters:

    • o (Class)

      produces the type corresponding to the class (e.g. Integer becomes PIntegerType)

  • .ruby(o) ⇒ Object

    Parameters:

    • o (Object)

      produces the type corresponding to the instance class (e.g. 3 becomes PIntegerType)



452
453
454
455
456
457
458
# File 'lib/puppet/pops/types/type_factory.rb', line 452

def self.ruby(o)
  if o.is_a?(Class)
    @type_calculator.type(o)
  else
    Types::PRuntimeType.new(:runtime => :ruby, :runtime_type_name => o.class.name)
  end
end

.ruby_type(class_name = nil) ⇒ Object

Generic creator of a RuntimeType - allows creating the Ruby type with nil name, or String name. Also see ruby(o) which performs inference, or mapps a Ruby Class to its name.



464
465
466
# File 'lib/puppet/pops/types/type_factory.rb', line 464

def self.ruby_type(class_name = nil)
  Types::PRuntimeType.new(:runtime => :ruby, :runtime_type_name => class_name)
end

.runtime(runtime = nil, runtime_type_name = nil) ⇒ Object

Generic creator of a RuntimeType - allows creating the type with nil or String runtime_type_name. Also see ruby_type(o) and ruby(o).



471
472
473
474
# File 'lib/puppet/pops/types/type_factory.rb', line 471

def self.runtime(runtime=nil, runtime_type_name = nil)
  runtime = runtime.to_sym if runtime.is_a?(String)
  Types::PRuntimeType.new(:runtime => runtime, :runtime_type_name => runtime_type_name)
end

.scalarObject

Produces the Literal type



225
226
227
# File 'lib/puppet/pops/types/type_factory.rb', line 225

def self.scalar()
  Types::PScalarType.new()
end

.string(*values) ⇒ Object

Produces the String type, optionally with specific string values



68
69
70
71
72
# File 'lib/puppet/pops/types/type_factory.rb', line 68

def self.string(*values)
  t = Types::PStringType.new()
  values.each {|v| t.addValues(v) }
  t
end

.struct(hash = {}) ⇒ PStructType

Produces the Struct type, either a non parameterized instance representing all structs (i.e. all hashes) or a hash with entries where the key is either a literal String, an Enum with one entry, or a String representing exactly one value. The key type may also be wrapped in a NotUndef or an Optional.

The value can be a ruby class, a String (interpreted as the name of a ruby class) or a Type.

Parameters:

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/puppet/pops/types/type_factory.rb', line 118

def self.struct(hash = {})
  tc = @type_calculator
  t = Types::PStructType.new
  t.elements = hash.map do |key_type, value_type|
    value_type = type_of(value_type)
    raise ArgumentError, 'Struct element value_type must be a Type' unless value_type.is_a?(Types::PAnyType)

    # TODO: Should have stricter name rule
    if key_type.is_a?(String)
      raise ArgumentError, 'Struct element key cannot be an empty String' if key_type.empty?
      key_type = string(key_type)
      # Must make key optional if the value can be Undef
      key_type = optional(key_type) if tc.assignable?(value_type, @undef_t)
    else
      # assert that the key type is one of String[1], NotUndef[String[1]] and Optional[String[1]]
      case key_type
      when Types::PNotUndefType
        # We can loose the NotUndef wrapper here since String[1] isn't optional anyway
        key_type = key_type.type
        s = key_type
      when Types::POptionalType
        s = key_type.optional_type
      else
        s = key_type
      end
      unless (s.is_a?(Puppet::Pops::Types::PStringType) || s.is_a?(Puppet::Pops::Types::PEnumType)) && s.values.size == 1 && !s.values[0].empty?
        raise ArgumentError, 'Unable to extract a non-empty literal string from Struct member key type' if key_type.empty?
      end
    end
    elem = Types::PStructElement.new
    elem.key_type = key_type
    elem.value_type = value_type
    elem
  end
  t
end

.tuple(*types) ⇒ Object



155
156
157
158
159
# File 'lib/puppet/pops/types/type_factory.rb', line 155

def self.tuple(*types)
  t = Types::PTupleType.new
  types.each {|elem| t.addTypes(type_of(elem)) }
  t
end

.type_of(o) ⇒ Object

Produce a type corresponding to the class of given unless given is a String, Class or a PAnyType. When a String is given this is taken as a classname.



426
427
428
429
430
431
432
433
434
435
436
# File 'lib/puppet/pops/types/type_factory.rb', line 426

def self.type_of(o)
  if o.is_a?(Class)
    @type_calculator.type(o)
  elsif o.is_a?(Types::PAnyType)
    o
  elsif o.is_a?(String)
    Types::PRuntimeType.new(:runtime => :ruby, :runtime_type_name => o)
  else
    @type_calculator.infer_generic(o)
  end
end

.type_type(inst_type = nil) ⇒ Object

Produces a type for Type



416
417
418
419
420
# File 'lib/puppet/pops/types/type_factory.rb', line 416

def self.type_type(inst_type = nil)
  type = Types::PType.new()
  type.type = inst_type
  type
end

.undefObject

Creates an instance of the Undef type



309
310
311
# File 'lib/puppet/pops/types/type_factory.rb', line 309

def self.undef()
  Types::PUndefType.new()
end

.variant(*types) ⇒ Object

Produces the Variant type, optionally with the “one of” types



101
102
103
104
105
# File 'lib/puppet/pops/types/type_factory.rb', line 101

def self.variant(*types)
  t = Types::PVariantType.new()
  types.each {|v| t.addTypes(type_of(v)) }
  t
end

.with_block(callable, *block_params) ⇒ Object



283
284
285
286
# File 'lib/puppet/pops/types/type_factory.rb', line 283

def self.with_block(callable, *block_params)
  callable.block_type = callable(*block_params)
  callable
end

.with_optional_block(callable, *block_params) ⇒ Object



288
289
290
291
# File 'lib/puppet/pops/types/type_factory.rb', line 288

def self.with_optional_block(callable, *block_params)
  callable.block_type = optional(callable(*block_params))
  callable
end