Class: Puppet::Pops::Types::TypeCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/type_calculator.rb

Overview

Note:

In general, new instances of the wanted type should be created as they are assigned to models using containment, and a contained object can only be in one container at a time. Also, the type system may include more details in each type instance, such as if it may be nil, be empty, contain a certain count etc. Or put differently, the puppet types are not singletons.

The TypeCalculator can answer questions about puppet types.

The Puppet type system is primarily based on sub-classing. When asking the type calculator to infer types from Ruby in general, it may not provide the wanted answer; it does not for instance take module inclusions and extensions into account. In general the type system should be unsurprising for anyone being exposed to the notion of type. The type ‘Data` may require a bit more explanation; this is an abstract type that includes all scalar types, as well as Array with an element type compatible with Data, and Hash with key compatible with scalar and elements compatible with Data. Expressed differently; Data is what you typically express using JSON (with the exception that the Puppet type system also includes Pattern (regular expression) as a scalar.

Inference


The ‘infer(o)` method infers a Puppet type for scalar Ruby objects, and for Arrays and Hashes. The inference result is instance specific for single typed collections and allows answering questions about its embedded type. It does not however preserve multiple types in a collection, and can thus not answer questions like `[1,a].infer() =~ Array[Integer, String]` since the inference computes the common type Scalar when combining Integer and String.

The ‘infer_generic(o)` method infers a generic Puppet type for scalar Ruby object, Arrays and Hashes. This inference result does not contain instance specific information; e.g. Array where the integer range is the generic default. Just `infer` it also combines types into a common type.

The ‘infer_set(o)` method works like `infer` but preserves all type information. It does not do any reduction into common types or ranges. This method of inference is best suited for answering questions about an object being an instance of a type. It correctly answers: `[1,a].infer_set() =~ Array[Integer, String]`

The ‘generalize!(t)` method modifies an instance specific inference result to a generic. The method mutates the given argument. Basically, this removes string instances from String, and range from Integer and Float.

Assignability


The ‘assignable?(t1, t2)` method answers if t2 conforms to t1. The type t2 may be an instance, in which case its type is inferred, or a type.

Instance?


The ‘instance?(t, o)` method answers if the given object (instance) is an instance that is assignable to the given type.

String


Creates a string representation of a type.

Creation of Type instances


Instance of the classes in the type model are used to denote a specific type. It is most convenient to use the TypeFactory when creating instances.

All types support ‘copy` which should be used when assigning a type where it is unknown if it is bound or not to a parent type. A check can be made with `t.eContainer().nil?`

Equality and Hash


Type instances are equal in terms of Ruby eql? and ‘==` if they describe the same type, but they are not `equal?` if they are not the same type instance. Two types that describe the same type have identical hash - this makes them usable as hash keys.

Types and Subclasses


In general, the type calculator should be used to answer questions if a type is a subtype of another (using #assignable?, or #instance? if the question is if a given object is an instance of a given type (or is a subtype thereof). Many of the types also have a Ruby subtype relationship; e.g. PHashType and PArrayType are both subtypes of PCollectionType, and PIntegerType, PFloatType, PStringType,… are subtypes of PScalarType. Even if it is possible to answer certain questions about type by looking at the Ruby class of the types this is considered an implementation detail, and such checks should in general be performed by the type_calculator which implements the type system semantics.

The PRuntimeType


The PRuntimeType corresponds to a type in the runtime system (currently only supported runtime is ‘ruby’). The type has a runtime_type_name that corresponds to a Ruby Class name. A Runtime type can be used to describe any ruby class except for the puppet types that are specialized (i.e. PRuntimeType should not be used for Integer, String, etc. since there are specialized types for those). When the type calculator deals with PRuntimeTypes and checks for assignability, it determines the “common ancestor class” of two classes. This check is made based on the superclasses of the two classes being compared. In order to perform this, the classes must be present (i.e. they are resolved from the string form in the PRuntimeType to a loaded, instantiated Ruby Class). In general this is not a problem, since the question to produce the common super type for two objects means that the classes must be present or there would have been no instances present in the first place. If however the classes are not present, the type calculator will fall back and state that the two types at least have Any in common.

Using the Type Calculator


The type calculator can be directly used via its class methods. If doing time critical work and doing many calls to the type calculator, it is more performant to create an instance and invoke the corresponding instance methods. Note that inference is an expensive operation, rather than inferring the same thing several times, it is in general better to infer once and then copy the result if mutation to a more generic form is required.

Constant Summary collapse

Types =
Puppet::Pops::Types

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypeCalculator

Returns a new instance of TypeCalculator.



162
163
164
165
166
167
# File 'lib/puppet/pops/types/type_calculator.rb', line 162

def initialize
  @@infer_visitor ||= Puppet::Pops::Visitor.new(nil, 'infer',0,0)
  @@string_visitor ||= Puppet::Pops::Visitor.new(nil, 'string',0,0)
  @@inspect_visitor ||= Puppet::Pops::Visitor.new(nil, 'debug_string',0,0)
  @@extract_visitor ||= Puppet::Pops::Visitor.new(nil, 'extract',0,0)
end

Class Method Details

.assignable?(t1, t2) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/puppet/pops/types/type_calculator.rb', line 104

def self.assignable?(t1, t2)
  singleton.assignable?(t1,t2)
end

.callable?(callable, args) ⇒ Boolan

Answers, does the given callable accept the arguments given in args (an array or a tuple)

Parameters:

Returns:

  • (Boolan)

    true if the callable accepts the arguments



114
115
116
# File 'lib/puppet/pops/types/type_calculator.rb', line 114

def self.callable?(callable, args)
  singleton.callable?(callable, args)
end

.debug_string(t) ⇒ Object



144
145
146
# File 'lib/puppet/pops/types/type_calculator.rb', line 144

def self.debug_string(t)
  singleton.debug_string(t)
end

.enumerable(t) ⇒ Object



149
150
151
# File 'lib/puppet/pops/types/type_calculator.rb', line 149

def self.enumerable(t)
  singleton.enumerable(t)
end

.generalize(o) ⇒ Object



134
135
136
# File 'lib/puppet/pops/types/type_calculator.rb', line 134

def self.generalize(o)
  singleton.generalize(o)
end

.infer(o) ⇒ Object



129
130
131
# File 'lib/puppet/pops/types/type_calculator.rb', line 129

def self.infer(o)
  singleton.infer(o)
end

.infer_set(o) ⇒ Object



139
140
141
# File 'lib/puppet/pops/types/type_calculator.rb', line 139

def self.infer_set(o)
  singleton.infer_set(o)
end

.instance?(t, o) ⇒ Boolean

Answers ‘is o an instance of type t’

Returns:

  • (Boolean)


311
312
313
# File 'lib/puppet/pops/types/type_calculator.rb', line 311

def self.instance?(t, o)
  singleton.instance?(t,o)
end

.is_kind_of_callable?(t, optional = true) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


720
721
722
# File 'lib/puppet/pops/types/type_calculator.rb', line 720

def self.is_kind_of_callable?(t, optional = true)
  t.is_a?(Types::PAnyType) && t.kind_of_callable?(optional)
end

.singletonTypeCalculator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the singleton instance.

Returns:



156
157
158
# File 'lib/puppet/pops/types/type_calculator.rb', line 156

def self.singleton
  @tc_instance ||= new
end

.string(t) ⇒ String

Produces a String representation of the given type.

Parameters:

Returns:

  • (String)

    the type in string form



124
125
126
# File 'lib/puppet/pops/types/type_calculator.rb', line 124

def self.string(t)
  singleton.string(t)
end

Instance Method Details

#assignable?(t, t2) ⇒ Boolean

Answers ‘can an instance of type t2 be assigned to a variable of type t’. Does not accept nil/undef unless the type accepts it.

Returns:

  • (Boolean)


197
198
199
200
201
202
# File 'lib/puppet/pops/types/type_calculator.rb', line 197

def assignable?(t, t2)
   if t.is_a?(Module)
     t = type(t)
   end
   t.is_a?(Types::PAnyType) ? t.assignable?(t2) : false
end

#callable?(callable, args) ⇒ Boolean

Answers, does the given callable accept the arguments given in args (an array or a tuple)

Returns:

  • (Boolean)


212
213
214
# File 'lib/puppet/pops/types/type_calculator.rb', line 212

def callable?(callable, args)
  callable.is_a?(Types::PAnyType) && callable.callable?(args)
end

#common_type(t1, t2) ⇒ Object

Answers, ‘What is the common type of t1 and t2?’

TODO: The current implementation should be optimized for performance

Raises:

  • (ArgumentError)


345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/puppet/pops/types/type_calculator.rb', line 345

def common_type(t1, t2)
  raise ArgumentError, 'two types expected' unless (is_ptype?(t1) || is_pnil?(t1)) && (is_ptype?(t2) || is_pnil?(t2))

  # TODO: This is not right since Scalar U Undef is Any
  # if either is nil, the common type is the other
  if is_pnil?(t1)
    return t2
  elsif is_pnil?(t2)
    return t1
  end

  # If either side is Unit, it is the other type
  if t1.is_a?(Types::PUnitType)
    return t2
  elsif t2.is_a?(Types::PUnitType)
    return t1
  end

  # Simple case, one is assignable to the other
  if assignable?(t1, t2)
    return t1
  elsif assignable?(t2, t1)
    return t2
  end

  # when both are arrays, return an array with common element type
  if t1.is_a?(Types::PArrayType) && t2.is_a?(Types::PArrayType)
    return Types::PArrayType.new(common_type(t1.element_type, t2.element_type))
  end

  # when both are hashes, return a hash with common key- and element type
  if t1.is_a?(Types::PHashType) && t2.is_a?(Types::PHashType)
    key_type = common_type(t1.key_type, t2.key_type)
    element_type = common_type(t1.element_type, t2.element_type)
    return Types::PHashType.new(key_type, element_type)
  end

  # when both are host-classes, reduce to PHostClass[] (since one was not assignable to the other)
  if t1.is_a?(Types::PHostClassType) && t2.is_a?(Types::PHostClassType)
    return Types::PHostClassType::DEFAULT
  end

  # when both are resources, reduce to Resource[T] or Resource[] (since one was not assignable to the other)
  if t1.is_a?(Types::PResourceType) && t2.is_a?(Types::PResourceType)
    # only Resource[] unless the type name is the same
    return t1.type_name == t2.type_name ?  Types::PResourceType.new(t1.type_name, nil) : Types::PResourceType::DEFAULT
  end

  # Integers have range, expand the range to the common range
  if t1.is_a?(Types::PIntegerType) && t2.is_a?(Types::PIntegerType)
    return Types::PIntegerType.new([t1.numeric_from, t2.numeric_from].min, [t1.numeric_to, t2.numeric_to].max)
  end

  # Floats have range, expand the range to the common range
  if t1.is_a?(Types::PFloatType) && t2.is_a?(Types::PFloatType)
    return Types::PFloatType.new([t1.numeric_from, t2.numeric_from].min, [t1.numeric_to, t2.numeric_to].max)
  end

  if t1.is_a?(Types::PStringType) && t2.is_a?(Types::PStringType)
    common_size_type = common_type(t1.size_type, t2.size_type) unless t1.size_type.nil? || t2.size_type.nil?
    common_strings = t1.values.empty? || t2.values.empty? ? [] : t1.values | t2.values
    return Types::PStringType.new(common_size_type, common_strings)
  end

  if t1.is_a?(Types::PPatternType) && t2.is_a?(Types::PPatternType)
    return Types::PPatternType.new(t1.patterns | t2.patterns)
  end

  if t1.is_a?(Types::PEnumType) && t2.is_a?(Types::PEnumType)
    # The common type is one that complies with either set
    return Types::PEnumType.new(t1.values | t2.values)
  end

  if t1.is_a?(Types::PVariantType) && t2.is_a?(Types::PVariantType)
    # The common type is one that complies with either set
    return Types::PVariantType.new(t1.types | t2.types)
  end

  if t1.is_a?(Types::PRegexpType) && t2.is_a?(Types::PRegexpType)
    # if they were identical, the general rule would return a parameterized regexp
    # since they were not, the result is a generic regexp type
    return Types::PPatternType::DEFAULT
  end

  if t1.is_a?(Types::PCallableType) && t2.is_a?(Types::PCallableType)
    # They do not have the same signature, and one is not assignable to the other,
    # what remains is the most general form of Callable
    return Types::PCallableType::DEFAULT
  end

  # Common abstract types, from most specific to most general
  if common_numeric?(t1, t2)
    return Types::PNumericType::DEFAULT
  end

  if common_scalar?(t1, t2)
    return Types::PScalarType::DEFAULT
  end

  if common_data?(t1,t2)
    return Types::PDataType::DEFAULT
  end

  # Meta types Type[Integer] + Type[String] => Type[Data]
  if t1.is_a?(Types::PType) && t2.is_a?(Types::PType)
    return Types::PType.new(common_type(t1.type, t2.type))
  end

  # If both are Runtime types
  if t1.is_a?(Types::PRuntimeType) && t2.is_a?(Types::PRuntimeType)
    if t1.runtime == t2.runtime && t1.runtime_type_name == t2.runtime_type_name
      return t1
    end
    # finding the common super class requires that names are resolved to class
    # NOTE: This only supports runtime type of :ruby
    c1 = Types::ClassLoader.provide_from_type(t1)
    c2 = Types::ClassLoader.provide_from_type(t2)
    if c1 && c2
      c2_superclasses = superclasses(c2)
      superclasses(c1).each do|c1_super|
        c2_superclasses.each do |c2_super|
          if c1_super == c2_super
            return Types::PRuntimeType.new(:ruby, c1_super.name)
          end
        end
      end
    end
  end

  # They better both be Any type, or the wrong thing was asked and nil is returned
  t1.is_a?(Types::PAnyType) && t2.is_a?(Types::PAnyType) ? Types::PAnyType::DEFAULT : nil
end

#debug_string(t) ⇒ Object

Produces a debug string representing the type (possibly with more information that the regular string format)



501
502
503
504
505
506
# File 'lib/puppet/pops/types/type_calculator.rb', line 501

def debug_string(t)
  if t.is_a?(Module)
    t = type(t)
  end
  @@inspect_visitor.visit_this_0(self, t)
end

#debug_string_Object(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



752
753
754
# File 'lib/puppet/pops/types/type_calculator.rb', line 752

def debug_string_Object(t)
  string(t)
end

#debug_string_PStringType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



838
839
840
841
842
# File 'lib/puppet/pops/types/type_calculator.rb', line 838

def debug_string_PStringType(t)
  range = range_array_part(t.size_type)
  range_part = range.empty? ? '' : '[' << range.join(' ,') << '], '
  'String[' << range_part << (t.values.map {|s| "'#{s}'" }).join(', ') << ']'
end

#enumerable(t) ⇒ Object

Returns an enumerable if the t represents something that can be iterated



205
206
207
208
# File 'lib/puppet/pops/types/type_calculator.rb', line 205

def enumerable(t)
  # Only PIntegerTypes are enumerable and only if not representing an infinite range
  t.is_a?(Types::PIntegerType) && t.size < Float::INFINITY ? t : nil
end

#equals(left, right) ⇒ Object

Answers if the two given types describe the same type



217
218
219
220
221
222
223
# File 'lib/puppet/pops/types/type_calculator.rb', line 217

def equals(left, right)
  return false unless left.is_a?(Types::PAnyType) && right.is_a?(Types::PAnyType)
  # Types compare per class only - an extra test must be made if the are mutually assignable
  # to find all types that represent the same type of instance
  #
  left == right || (assignable?(right, left) && assignable?(left, right))
end

#generalize(o) ⇒ Object

Generalizes value specific types. The generalized type is returned.



264
265
266
# File 'lib/puppet/pops/types/type_calculator.rb', line 264

def generalize(o)
  o.is_a?(Types::PAnyType) ? o.generalize : o
end

#infer(o) ⇒ Object

Answers ‘what is the single common Puppet Type describing o’, or if o is an Array or Hash, what is the single common type of the elements (or keys and elements for a Hash).



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/puppet/pops/types/type_calculator.rb', line 272

def infer(o)
  # Optimize the most common cases into direct calls.
  case o
  when String
    infer_String(o)
  when Integer
    infer_Integer(o)
  when Array
    infer_Array(o)
  when Hash
    infer_Hash(o)
  when Puppet::Pops::Evaluator::PuppetProc
    infer_PuppetProc(o)
  else
    @@infer_visitor.visit_this_0(self, o)
  end
end

#infer_and_reduce_type(enumerable) ⇒ Object

Reduce an enumerable of objects to a single common type



519
520
521
# File 'lib/puppet/pops/types/type_calculator.rb', line 519

def infer_and_reduce_type(enumerable)
  reduce_type(enumerable.map {|o| infer(o) })
end

#infer_Array(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



644
645
646
647
648
649
650
# File 'lib/puppet/pops/types/type_calculator.rb', line 644

def infer_Array(o)
  if o.empty?
    Types::PArrayType::EMPTY
  else
    Types::PArrayType.new(infer_and_reduce_type(o), size_as_type(o))
  end
end

#infer_Closure(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



531
532
533
# File 'lib/puppet/pops/types/type_calculator.rb', line 531

def infer_Closure(o)
  o.type
end

#infer_FalseClass(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



628
629
630
# File 'lib/puppet/pops/types/type_calculator.rb', line 628

def infer_FalseClass(o)
  Types::PBooleanType::DEFAULT
end

#infer_Float(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



566
567
568
# File 'lib/puppet/pops/types/type_calculator.rb', line 566

def infer_Float(o)
  Types::PFloatType.new(o, o)
end

#infer_Function(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



536
537
538
# File 'lib/puppet/pops/types/type_calculator.rb', line 536

def infer_Function(o)
  o.class.dispatcher.to_type
end

#infer_generic(o) ⇒ Object



290
291
292
# File 'lib/puppet/pops/types/type_calculator.rb', line 290

def infer_generic(o)
  generalize(infer(o))
end

#infer_Hash(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



653
654
655
656
657
658
659
660
661
# File 'lib/puppet/pops/types/type_calculator.rb', line 653

def infer_Hash(o)
  if o.empty?
    Types::PHashType::EMPTY
  else
    ktype = infer_and_reduce_type(o.keys)
    etype = infer_and_reduce_type(o.values)
    Types::PHashType.new(ktype, etype, size_as_type(o))
  end
end

#infer_Integer(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



571
572
573
# File 'lib/puppet/pops/types/type_calculator.rb', line 571

def infer_Integer(o)
  Types::PIntegerType.new(o, o)
end

#infer_Module(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The type of all modules is PType



526
527
528
# File 'lib/puppet/pops/types/type_calculator.rb', line 526

def infer_Module(o)
  Types::PType::new(Types::PRuntimeType.new(:ruby, o.name))
end

#infer_NilClass(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



581
582
583
# File 'lib/puppet/pops/types/type_calculator.rb', line 581

def infer_NilClass(o)
  Types::PUndefType::DEFAULT
end

#infer_Object(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



541
542
543
# File 'lib/puppet/pops/types/type_calculator.rb', line 541

def infer_Object(o)
  Types::PRuntimeType.new(:ruby, o.class.name)
end

#infer_PAnyType(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The type of all types is PType



548
549
550
# File 'lib/puppet/pops/types/type_calculator.rb', line 548

def infer_PAnyType(o)
  Types::PType.new(o)
end

#infer_Proc(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • o (Proc)


587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/puppet/pops/types/type_calculator.rb', line 587

def infer_Proc(o)
  min = 0
  max = 0
  mapped_types = o.parameters.map do |p|
    case p[0]
    when :rest
      max = :default
      break Types::PAnyType::DEFAULT
    when :req
      min += 1
    end
    max += 1
    Types::PAnyType::DEFAULT
  end
  mapped_types << min
  mapped_types << max
  Types::TypeFactory.callable(*mapped_types)
end

#infer_PType(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The type of all types is PType This is the metatype short circuit.



556
557
558
# File 'lib/puppet/pops/types/type_calculator.rb', line 556

def infer_PType(o)
  Types::PType.new(o)
end

#infer_PuppetProc(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



607
608
609
# File 'lib/puppet/pops/types/type_calculator.rb', line 607

def infer_PuppetProc(o)
  infer_Closure(o.closure)
end

#infer_Regexp(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



576
577
578
# File 'lib/puppet/pops/types/type_calculator.rb', line 576

def infer_Regexp(o)
  Types::PRegexpType.new(o.source)
end

#infer_Resource(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A Puppet::Parser::Resource, or Puppet::Resource



635
636
637
638
639
640
641
# File 'lib/puppet/pops/types/type_calculator.rb', line 635

def infer_Resource(o)
  # Only Puppet::Resource can have a title that is a symbol :undef, a PResource cannot.
  # A mapping must be made to empty string. A nil value will result in an error later
  title = o.title
  title = '' if :undef == title
  Types::PType.new(Types::PResourceType.new(o.type.to_s.downcase, title))
end

#infer_set(o) ⇒ Object

Answers ‘what is the set of Puppet Types of o’



297
298
299
300
301
302
303
304
305
306
# File 'lib/puppet/pops/types/type_calculator.rb', line 297

def infer_set(o)
  case o
    when Array
      infer_set_Array(o)
    when Hash
      infer_set_Hash(o)
    else
      infer_set_Object(o)
  end
end

#infer_set_Array(o) ⇒ Object



673
674
675
676
677
678
679
# File 'lib/puppet/pops/types/type_calculator.rb', line 673

def infer_set_Array(o)
  if o.empty?
    Types::PArrayType::EMPTY
  else
    Types::PTupleType.new(o.map {|x| infer_set(x) })
  end
end

#infer_set_Hash(o) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
# File 'lib/puppet/pops/types/type_calculator.rb', line 681

def infer_set_Hash(o)
  if o.empty?
    Types::PHashType::EMPTY
  elsif o.keys.all? {|k| Types::PStringType::NON_EMPTY.instance?(k) }
    Types::PStructType.new(o.each_pair.map { |k,v| Types::PStructElement.new(Types::PStringType.new(nil, [k]), infer_set(v)) })
  else
    ktype = Types::PVariantType.new(o.keys.map {|k| infer_set(k) })
    etype = Types::PVariantType.new(o.values.map {|e| infer_set(e) })
    Types::PHashType.new(unwrap_single_variant(ktype), unwrap_single_variant(etype), size_as_type(o))
  end
end

#infer_set_Object(o) ⇒ Object

Common case for everything that intrinsically only has a single type



669
670
671
# File 'lib/puppet/pops/types/type_calculator.rb', line 669

def infer_set_Object(o)
  infer(o)
end

#infer_String(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



561
562
563
# File 'lib/puppet/pops/types/type_calculator.rb', line 561

def infer_String(o)
  Types::PStringType.new(size_as_type(o), [o])
end

#infer_Symbol(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inference of :default as PDefaultType, and all other are Ruby



613
614
615
616
617
618
619
620
# File 'lib/puppet/pops/types/type_calculator.rb', line 613

def infer_Symbol(o)
  case o
  when :default
    Types::PDefaultType::DEFAULT
  else
    infer_Object(o)
  end
end

#infer_TrueClass(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



623
624
625
# File 'lib/puppet/pops/types/type_calculator.rb', line 623

def infer_TrueClass(o)
  Types::PBooleanType::DEFAULT
end

#injectable_class(klazz) ⇒ Class?

Answers the question ‘is it possible to inject an instance of the given class’ A class is injectable if it has a special *assisted inject* class method called ‘inject` taking an injector and a scope as argument, or if it has a zero args `initialize` method.

Parameters:

Returns:

  • (Class, nil)

    the injectable Class, or nil if not injectable



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/puppet/pops/types/type_calculator.rb', line 177

def injectable_class(klazz)
  # Handle case when we get a PType instead of a class
  if klazz.is_a?(Types::PRuntimeType)
    klazz = Puppet::Pops::Types::ClassLoader.provide(klazz)
  end

  # data types can not be injected (check again, it is not safe to assume that given RubyRuntime klazz arg was ok)
  return false unless type(klazz).is_a?(Types::PRuntimeType)
  if (klazz.respond_to?(:inject) && klazz.method(:inject).arity == -4) || klazz.instance_method(:initialize).arity == 0
    klazz
  else
    nil
  end
end

#instance?(t, o) ⇒ Boolean

Answers ‘is o an instance of type t’

Returns:

  • (Boolean)


318
319
320
321
322
323
# File 'lib/puppet/pops/types/type_calculator.rb', line 318

def instance?(t, o)
  if t.is_a?(Module)
    t = type(t)
  end
  t.is_a?(Types::PAnyType) ? t.instance?(o) : false
end

#is_pnil?(t) ⇒ Boolean

Answers if t represents the puppet type PUndefType

Returns:

  • (Boolean)


335
336
337
# File 'lib/puppet/pops/types/type_calculator.rb', line 335

def is_pnil?(t)
  t.nil? || t.is_a?(Types::PUndefType)
end

#is_ptype?(t) ⇒ Boolean

Answers if t is a puppet type

Returns:

  • (Boolean)


328
329
330
# File 'lib/puppet/pops/types/type_calculator.rb', line 328

def is_ptype?(t)
  t.is_a?(Types::PAnyType)
end

#max(a, b) ⇒ Object



724
725
726
# File 'lib/puppet/pops/types/type_calculator.rb', line 724

def max(a,b)
  a >=b ? a : b
end

#min(a, b) ⇒ Object



728
729
730
# File 'lib/puppet/pops/types/type_calculator.rb', line 728

def min(a,b)
  a <= b ? a : b
end

#range_array_part(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Produces a string from an Integer range type that is used inside other type strings



806
807
808
809
# File 'lib/puppet/pops/types/type_calculator.rb', line 806

def range_array_part(t)
  return [] if t.nil? || t.unbounded?
  [t.from.nil? ? 'default' : t.from , t.to.nil? ? 'default' : t.to ]
end

#reduce_type(enumerable) ⇒ Object

Reduces an enumerable of types to a single common type.



512
513
514
# File 'lib/puppet/pops/types/type_calculator.rb', line 512

def reduce_type(enumerable)
  enumerable.reduce(nil) {|memo, t| common_type(memo, t) }
end

#size_as_type(collection) ⇒ Object



663
664
665
666
# File 'lib/puppet/pops/types/type_calculator.rb', line 663

def size_as_type(collection)
  size = collection.size
  Types::PIntegerType.new(size, size)
end

#size_range(range) ⇒ Object

Transform int range to a size constraint if range == nil the constraint is 1,1 if range.from == nil min size = 1 if range.to == nil max size == Infinity



706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/puppet/pops/types/type_calculator.rb', line 706

def size_range(range)
  return [1,1] if range.nil?
  from = range.from
  to = range.to
  x = from.nil? ? 1 : from
  y = to.nil? ? Float::INFINITY : to
  if x < y
    [x, y]
  else
    [y, x]
  end
end

#string(t) ⇒ Object

Produces a string representing the type



491
492
493
494
495
496
# File 'lib/puppet/pops/types/type_calculator.rb', line 491

def string(t)
  if t.is_a?(Module)
    t = type(t)
  end
  @@string_visitor.visit_this_0(self, t)
end

#string_NilClass(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



766
# File 'lib/puppet/pops/types/type_calculator.rb', line 766

def string_NilClass(t)     ; '?'       ; end

#string_PAnyType(t) ⇒ Object



774
# File 'lib/puppet/pops/types/type_calculator.rb', line 774

def string_PAnyType(t)     ; 'Any'; end

#string_PArrayType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



940
941
942
943
# File 'lib/puppet/pops/types/type_calculator.rb', line 940

def string_PArrayType(t)
  parts = [string(t.element_type)] + range_array_part(t.size_type)
  "Array[#{parts.join(', ')}]"
end

#string_PBooleanType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



783
# File 'lib/puppet/pops/types/type_calculator.rb', line 783

def string_PBooleanType(t) ; 'Boolean'; end

#string_PCallableType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# File 'lib/puppet/pops/types/type_calculator.rb', line 869

def string_PCallableType(t)
  # generic
  return 'Callable' if t.param_types.nil?

  if t.param_types.types.empty?
    range = [0, 0]
  else
    range = range_array_part(t.param_types.size_type)
  end
  # translate to string, and skip Unit types
  types = t.param_types.types.map {|t2| string(t2) unless t2.class == Types::PUnitType }.compact

  s = 'Callable[' << types.join(', ')
  unless range.empty?
    (s << ', ') unless types.empty?
    s << range.join(', ')
  end
  # Add block T last (after min, max) if present)
  #
  unless t.block_type.nil?
    (s << ', ') unless types.empty? && range.empty?
    s << string(t.block_type)
  end
  s << ']'
  s
end

#string_PCatalogEntryType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



952
953
954
# File 'lib/puppet/pops/types/type_calculator.rb', line 952

def string_PCatalogEntryType(t)
  'CatalogEntry'
end

#string_PCollectionType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



922
923
924
925
926
927
928
929
# File 'lib/puppet/pops/types/type_calculator.rb', line 922

def string_PCollectionType(t)
  range = range_array_part(t.size_type)
  if range.empty?
    'Collection'
  else
    "Collection[#{range.join(', ')}]"
  end
end

#string_PDataType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



789
# File 'lib/puppet/pops/types/type_calculator.rb', line 789

def string_PDataType(t)    ; 'Data'; end

#string_PDefaultType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



780
# File 'lib/puppet/pops/types/type_calculator.rb', line 780

def string_PDefaultType(t) ; 'Default' ; end

#string_PEnumType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



845
846
847
848
# File 'lib/puppet/pops/types/type_calculator.rb', line 845

def string_PEnumType(t)
  return 'Enum' if t.values.empty?
  'Enum[' << t.values.map {|s| "'#{s}'" }.join(', ') << ']'
end

#string_PFloatType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



812
813
814
815
816
817
818
819
# File 'lib/puppet/pops/types/type_calculator.rb', line 812

def string_PFloatType(t)
  range = range_array_part(t)
  if range.empty?
    'Float'
  else
    "Float[#{range.join(', ')}]"
  end
end

#string_PHashType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



946
947
948
949
# File 'lib/puppet/pops/types/type_calculator.rb', line 946

def string_PHashType(t)
  parts = [string(t.key_type), string(t.element_type)] + range_array_part(t.size_type)
  "Hash[#{parts.join(', ')}]"
end

#string_PHostClassType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



957
958
959
960
961
962
963
# File 'lib/puppet/pops/types/type_calculator.rb', line 957

def string_PHostClassType(t)
  if t.class_name
    "Class[#{t.class_name}]"
  else
    'Class'
  end
end

#string_PIntegerType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



795
796
797
798
799
800
801
802
# File 'lib/puppet/pops/types/type_calculator.rb', line 795

def string_PIntegerType(t)
  range = range_array_part(t)
  if range.empty?
    'Integer'
  else
    "Integer[#{range.join(', ')}]"
  end
end

#string_PNotUndefType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/puppet/pops/types/type_calculator.rb', line 979

def string_PNotUndefType(t)
  contained_type = t.type
  if contained_type.nil? || contained_type.class == Puppet::Pops::Types::PAnyType
    'NotUndef'
  else
    if contained_type.is_a?(Puppet::Pops::Types::PStringType) && contained_type.values.size == 1
      "NotUndef['#{contained_type.values[0]}']"
    else
      "NotUndef[#{string(contained_type)}]"
    end
  end
end

#string_PNumericType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



792
# File 'lib/puppet/pops/types/type_calculator.rb', line 792

def string_PNumericType(t) ; 'Numeric'; end

#string_POptionalType(t) ⇒ Object



992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/puppet/pops/types/type_calculator.rb', line 992

def string_POptionalType(t)
  optional_type = t.optional_type
  if optional_type.nil?
    'Optional'
  else
    if optional_type.is_a?(Puppet::Pops::Types::PStringType) && optional_type.values.size == 1
      "Optional['#{optional_type.values[0]}']"
    else
      "Optional[#{string(optional_type)}]"
    end
  end
end

#string_PPatternType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



916
917
918
919
# File 'lib/puppet/pops/types/type_calculator.rb', line 916

def string_PPatternType(t)
  return 'Pattern' if t.patterns.empty?
  'Pattern[' << t.patterns.map {|s| "#{s.regexp.inspect}" }.join(', ') << ']'
end

#string_PRegexpType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



822
823
824
# File 'lib/puppet/pops/types/type_calculator.rb', line 822

def string_PRegexpType(t)
  t.pattern.nil? ? 'Regexp' : "Regexp[#{t.regexp.inspect}]"
end

#string_PResourceType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



966
967
968
969
970
971
972
973
974
975
976
# File 'lib/puppet/pops/types/type_calculator.rb', line 966

def string_PResourceType(t)
  if t.type_name
    if t.title
      "#{capitalize_segments(t.type_name)}['#{t.title}']"
    else
      capitalize_segments(t.type_name)
    end
  else
    'Resource'
  end
end

#string_PRuntimeType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



937
# File 'lib/puppet/pops/types/type_calculator.rb', line 937

def string_PRuntimeType(t)   ; "Runtime[#{string(t.runtime)}, #{string(t.runtime_type_name)}]"  ; end

#string_PScalarType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



786
# File 'lib/puppet/pops/types/type_calculator.rb', line 786

def string_PScalarType(t)  ; 'Scalar'; end

#string_PStringType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



827
828
829
830
831
832
833
834
835
# File 'lib/puppet/pops/types/type_calculator.rb', line 827

def string_PStringType(t)
  # skip values in regular output - see debug_string
  range = range_array_part(t.size_type)
  if range.empty?
    'String'
  else
    "String[#{range.join(', ')}]"
  end
end

#string_PStructElement(t) ⇒ Object



902
903
904
905
906
907
908
909
910
911
912
913
# File 'lib/puppet/pops/types/type_calculator.rb', line 902

def string_PStructElement(t)
  k = t.key_type
  value_optional = t.value_type.assignable?(Types::PUndefType::DEFAULT)
  key_string =
    if k.is_a?(Types::POptionalType)
      # Output as literal String
      value_optional ? "'#{t.name}'" : string(k)
    else
      value_optional ? "NotUndef['#{t.name}']" : "'#{t.name}'"
    end
  "#{key_string}=>#{string(t.value_type)}"
end

#string_PStructType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



897
898
899
900
# File 'lib/puppet/pops/types/type_calculator.rb', line 897

def string_PStructType(t)
  return 'Struct' if t.elements.empty?
  'Struct[{' << t.elements.map {|element| string(element) }.join(', ') << '}]'
end

#string_PTupleType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



857
858
859
860
861
862
863
864
865
866
# File 'lib/puppet/pops/types/type_calculator.rb', line 857

def string_PTupleType(t)
  range = range_array_part(t.size_type)
  return 'Tuple' if t.types.empty?
  s = 'Tuple[' << t.types.map {|t2| string(t2) }.join(', ')
  unless range.empty?
    s << ', ' << range.join(', ')
  end
  s << ']'
  s
end

#string_PType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



757
758
759
760
761
762
763
# File 'lib/puppet/pops/types/type_calculator.rb', line 757

def string_PType(t)
  if t.type.nil?
    'Type'
  else
    "Type[#{string(t.type)}]"
  end
end

#string_PUndefType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



777
# File 'lib/puppet/pops/types/type_calculator.rb', line 777

def string_PUndefType(t)     ; 'Undef'   ; end

#string_PUnitType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



932
933
934
# File 'lib/puppet/pops/types/type_calculator.rb', line 932

def string_PUnitType(t)
  'Unit'
end

#string_PVariantType(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



851
852
853
854
# File 'lib/puppet/pops/types/type_calculator.rb', line 851

def string_PVariantType(t)
  return 'Variant' if t.types.empty?
  'Variant[' << t.types.map {|t2| string(t2) }.join(', ') << ']'
end

#string_String(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



769
# File 'lib/puppet/pops/types/type_calculator.rb', line 769

def string_String(t)       ; t         ; end

#string_Symbol(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



772
# File 'lib/puppet/pops/types/type_calculator.rb', line 772

def string_Symbol(t)       ; t.to_s    ; end

#superclasses(c) ⇒ Object

Produces the superclasses of the given class, including the class



479
480
481
482
483
484
485
486
# File 'lib/puppet/pops/types/type_calculator.rb', line 479

def superclasses(c)
  result = [c]
  while s = c.superclass
    result << s
    c = s
  end
  result
end

#to_sObject

Debugging to_s to reduce the amount of output



1006
1007
1008
# File 'lib/puppet/pops/types/type_calculator.rb', line 1006

def to_s
  '[a TypeCalculator]'
end

#tuple_entry_at(tuple_t, from, to, index) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Produces the tuple entry at the given index given a tuple type, its from/to constraints on the last type, and an index. Produces nil if the index is out of bounds from must be less than to, and from may not be less than 0



739
740
741
742
743
744
745
746
747
748
749
# File 'lib/puppet/pops/types/type_calculator.rb', line 739

def tuple_entry_at(tuple_t, from, to, index)
  regular = (tuple_t.types.size - 1)
  if index < regular
    tuple_t.types[index]
  elsif index < regular + to
    # in the varargs part
    tuple_t.types[-1]
  else
    nil
  end
end

#type(c) ⇒ Object

Answers ‘what is the Puppet Type corresponding to the given Ruby class’

Parameters:

  • c (Module)

    the class for which a puppet type is wanted

Raises:

  • (ArgumentError)


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/puppet/pops/types/type_calculator.rb', line 229

def type(c)
  raise ArgumentError, 'Argument must be a Module' unless c.is_a? Module

  # Can't use a visitor here since we don't have an instance of the class
  case
  when c <= Integer
    type = Types::PIntegerType::DEFAULT
  when c == Float
    type = Types::PFloatType::DEFAULT
  when c == Numeric
    type = Types::PNumericType::DEFAULT
  when c == String
    type = Types::PStringType::DEFAULT
  when c == Regexp
    type = Types::PRegexpType::DEFAULT
  when c == NilClass
    type = Types::PUndefType::DEFAULT
  when c == FalseClass, c == TrueClass
    type = Types::PBooleanType::DEFAULT
  when c == Class
    type = Types::PType::DEFAULT
  when c == Array
    # Assume array of data values
    type = Types::PArrayType::DATA
  when c == Hash
    # Assume hash with scalar keys and data values
    type = Types::PHashType::DATA
 else
    type = Types::PRuntimeType.new(:ruby, c.name)
  end
  type
end

#unwrap_single_variant(possible_variant) ⇒ Object



693
694
695
696
697
698
699
# File 'lib/puppet/pops/types/type_calculator.rb', line 693

def unwrap_single_variant(possible_variant)
  if possible_variant.is_a?(Types::PVariantType) && possible_variant.types.size == 1
    possible_variant.types[0]
  else
    possible_variant
  end
end