module Puppet::Pops::Types::TypeFactory

Helper module that makes creation of type objects simpler. @api public

Public Class Methods

all_callables() click to toggle source

Produces a CallableType matching all callables @api public

    # File lib/puppet/pops/types/type_factory.rb
308 def self.all_callables
309   return PCallableType::DEFAULT
310 end
any() click to toggle source

Produces the Any type @api public

    # File lib/puppet/pops/types/type_factory.rb
254 def self.any
255   PAnyType::DEFAULT
256 end
array_of(o, size_type = nil) click to toggle source

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

    # File lib/puppet/pops/types/type_factory.rb
459 def self.array_of(o, size_type = nil)
460   PArrayType.new(type_of(o), size_type)
461 end
array_of_any() click to toggle source

Produces a type for Array @api public

    # File lib/puppet/pops/types/type_factory.rb
485 def self.array_of_any
486   PArrayType::DEFAULT
487 end
array_of_data() click to toggle source

Produces a type for Array @api public

    # File lib/puppet/pops/types/type_factory.rb
492 def self.array_of_data
493   @array_of_data_t = PArrayType.new(data)
494 end
binary() click to toggle source

Creates an instance of the Binary type @api public

    # File lib/puppet/pops/types/type_factory.rb
402 def self.binary
403   PBinaryType::DEFAULT
404 end
boolean(value = nil) click to toggle source

Produces the Boolean type @api public

    # File lib/puppet/pops/types/type_factory.rb
247 def self.boolean(value = nil)
248   value.nil? ? PBooleanType::DEFAULT : (value ? PBooleanType::TRUE : PBooleanType::FALSE)
249 end
callable(*params) click to toggle source

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}.

    # File lib/puppet/pops/types/type_factory.rb
328 def self.callable(*params)
329   if params.size == 2 && params[0].is_a?(Array)
330     return_t = type_of(params[1])
331     params = params[0]
332   else
333     return_t = nil
334   end
335   last_callable = TypeCalculator.is_kind_of_callable?(params.last)
336   block_t = last_callable ? params.pop : nil
337 
338   # compute a size_type for the signature based on the two last parameters
339   if is_range_parameter?(params[-2]) && is_range_parameter?(params[-1])
340     size_type = range(params[-2], params[-1])
341     params = params[0, params.size - 2]
342   elsif is_range_parameter?(params[-1])
343     size_type = range(params[-1], :default)
344     params = params[0, params.size - 1]
345   else
346     size_type = nil
347   end
348 
349   types = params.map {|p| type_of(p) }
350 
351   # If the specification requires types, and none were given, a Unit type is used
352   if types.empty? && !size_type.nil? && size_type.range[1] > 0
353     types << PUnitType::DEFAULT
354   end
355   # create a signature
356   tuple_t = tuple(types, size_type)
357   PCallableType.new(tuple_t, block_t, return_t)
358 end
catalog_entry() click to toggle source

Produces an instance of the abstract type PCatalogEntryType

    # File lib/puppet/pops/types/type_factory.rb
407 def self.catalog_entry
408   PCatalogEntryType::DEFAULT
409 end
clear() click to toggle source

Clears caches - used when testing

   # File lib/puppet/pops/types/type_factory.rb
10 def self.clear
11   # these types are cached and needs to be nulled as the representation may change if loaders are cleared
12   @data_t = nil
13   @rich_data_t = nil
14   @rich_data_key_t = nil
15   @array_of_data_t = nil
16   @hash_of_data_t = nil
17   @error_t = nil
18   @task_t = nil
19   @deferred_t = nil
20 end
collection(size_type = nil) click to toggle source

Produces the abstract type Collection @api public

    # File lib/puppet/pops/types/type_factory.rb
363 def self.collection(size_type = nil)
364   size_type.nil? ? PCollectionType::DEFAULT : PCollectionType.new(size_type)
365 end
data() click to toggle source

Produces the Data type @api public

    # File lib/puppet/pops/types/type_factory.rb
370 def self.data
371   @data_t ||= TypeParser.singleton.parse('Data', Loaders.static_loader)
372 end
default() click to toggle source

Creates an instance of the Default type @api public

    # File lib/puppet/pops/types/type_factory.rb
396 def self.default
397   PDefaultType::DEFAULT
398 end
deferred() click to toggle source
    # File lib/puppet/pops/types/type_factory.rb
542 def self.deferred
543   @deferred_t ||= TypeParser.singleton.parse('Deferred')
544 end
enum(*values) click to toggle source

Produces the Enum type, optionally with specific string values @api public

    # File lib/puppet/pops/types/type_factory.rb
144 def self.enum(*values)
145   last = values.last
146   case_insensitive = false
147   if last == true || last == false
148     case_insensitive = last
149     values = values[0...-1]
150   end
151   PEnumType.new(values, case_insensitive)
152 end
error() click to toggle source

Produces a type for Error @api public

    # File lib/puppet/pops/types/type_factory.rb
534 def self.error
535   @error_t ||= TypeParser.singleton.parse('Error', Loaders.loaders.puppet_system_loader)
536 end
float() click to toggle source

Produces the Float type @api public

   # File lib/puppet/pops/types/type_factory.rb
52 def self.float
53   PFloatType::DEFAULT
54 end
float_range(from, to) click to toggle source

Produces a Float range type @api public

   # File lib/puppet/pops/types/type_factory.rb
42 def self.float_range(from, to)
43   # optimize eq with symbol (faster when it is left)
44   from = Float(from) unless :default == from || from.nil?
45   to = Float(to) unless :default == to || to.nil?
46   PFloatType.new(from, to)
47 end
hash_kv(key_type, value_type, size_type = nil) click to toggle source

Produces a type for Hash @param key_type [PAnyType] the key type @param value_type [PAnyType] the value type @param size_type [PIntegerType] @return [PHashType] the created hash type @api public

    # File lib/puppet/pops/types/type_factory.rb
478 def self.hash_kv(key_type, value_type, size_type = nil)
479   PHashType.new(key_type, value_type, size_type)
480 end
hash_of(value, key = scalar, size_type = nil) click to toggle source

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

    # File lib/puppet/pops/types/type_factory.rb
467 def self.hash_of(value, key = scalar, size_type = nil)
468   PHashType.new(type_of(key), type_of(value), size_type)
469 end
hash_of_any() click to toggle source

Produces a type for Hash @api public

    # File lib/puppet/pops/types/type_factory.rb
499 def self.hash_of_any
500   PHashType::DEFAULT
501 end
hash_of_data() click to toggle source

Produces a type for Hash @api public

    # File lib/puppet/pops/types/type_factory.rb
506 def self.hash_of_data
507   @hash_of_data_t = PHashType.new(string, data)
508 end
host_class(class_name = nil) click to toggle source

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

    # File lib/puppet/pops/types/type_factory.rb
447 def self.host_class(class_name = nil)
448   if class_name.nil?
449     PClassType::DEFAULT
450   else
451     PClassType.new(class_name.sub(/^::/, ''))
452   end
453 end
init(*args) click to toggle source

Produces the Init type @api public

   # File lib/puppet/pops/types/type_factory.rb
72 def self.init(*args)
73   case args.size
74   when 0
75     PInitType::DEFAULT
76   when 1
77     type = args[0]
78     type.nil? ? PInitType::DEFAULT : PInitType.new(type, EMPTY_ARRAY)
79   else
80     type = args.shift
81     PInitType.new(type, args)
82   end
83 end
integer() click to toggle source

Produces the Integer type @api public

   # File lib/puppet/pops/types/type_factory.rb
25 def self.integer
26   PIntegerType::DEFAULT
27 end
is_range_parameter?(t) click to toggle source

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

    # File lib/puppet/pops/types/type_factory.rb
625 def self.is_range_parameter?(t)
626   t.is_a?(Integer) || t == 'default' || :default == t
627 end
iterable(elem_type = nil) click to toggle source

Produces the Iterable type @api public

   # File lib/puppet/pops/types/type_factory.rb
88 def self.iterable(elem_type = nil)
89   elem_type.nil? ? PIterableType::DEFAULT : PIterableType.new(elem_type)
90 end
iterator(elem_type = nil) click to toggle source

Produces the Iterator type @api public

   # File lib/puppet/pops/types/type_factory.rb
95 def self.iterator(elem_type = nil)
96   elem_type.nil? ? PIteratorType::DEFAULT : PIteratorType.new(elem_type)
97 end
label(t) click to toggle source

Produces a string representation of the type @api public

    # File lib/puppet/pops/types/type_factory.rb
102 def self.label(t)
103   @type_calculator.string(t)
104 end
not_undef(inst_type = nil) click to toggle source

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

@param inst_type [Type,String] the type to qualify @return [PNotUndefType] the NotUndef type

@api public

    # File lib/puppet/pops/types/type_factory.rb
519 def self.not_undef(inst_type = nil)
520   inst_type = string(inst_type) if inst_type.is_a?(String)
521   PNotUndefType.new(inst_type)
522 end
numeric() click to toggle source

Produces the Numeric type @api public

   # File lib/puppet/pops/types/type_factory.rb
66 def self.numeric
67   PNumericType::DEFAULT
68 end
object(hash = nil, loader = nil) click to toggle source

Produces an `Object` type from the given hash that represents the features of the object

@param hash [{String=>Object}] the hash of feature groups @return [PObjectType] the created type

    # File lib/puppet/pops/types/type_factory.rb
214 def self.object(hash = nil, loader = nil)
215   hash.nil? || hash.empty? ? PObjectType::DEFAULT : PObjectType.new(hash, loader)
216 end
optional(optional_type = nil) click to toggle source

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.

@param optional_type [String,PAnyType,nil] the optional type @return [POptionalType] the created type

@api public

    # File lib/puppet/pops/types/type_factory.rb
133 def self.optional(optional_type = nil)
134   if optional_type.nil?
135     POptionalType::DEFAULT
136   else
137     POptionalType.new(type_of(optional_type.is_a?(String) ? string(optional_type) : type_of(optional_type)))
138   end
139 end
pattern(*regular_expressions) click to toggle source
    # File lib/puppet/pops/types/type_factory.rb
267 def self.pattern(*regular_expressions)
268   patterns = regular_expressions.map do |re|
269     case re
270     when String
271       re_t = PRegexpType.new(re)
272       re_t.regexp  # compile it to catch errors
273       re_t
274 
275     when Regexp
276       PRegexpType.new(re)
277 
278     when PRegexpType
279       re
280 
281     when PPatternType
282       re.patterns
283 
284    else
285      raise ArgumentError, "Only String, Regexp, Pattern-Type, and Regexp-Type are allowed: got '#{re.class}"
286     end
287   end.flatten.uniq
288   PPatternType.new(patterns)
289 end
range(from, to) click to toggle source

Produces an Integer range type @api public

   # File lib/puppet/pops/types/type_factory.rb
32 def self.range(from, to)
33   # optimize eq with symbol (faster when it is left)
34   from = :default == from if from == 'default'
35   to = :default if to == 'default'
36   PIntegerType.new(from, to)
37 end
regexp(pattern = nil) click to toggle source

Produces the Regexp type @param pattern [Regexp, String, nil] (nil) The regular expression object or

a regexp source string, or nil for bare type

@api public

    # File lib/puppet/pops/types/type_factory.rb
263 def self.regexp(pattern = nil)
264   pattern ?  PRegexpType.new(pattern) : PRegexpType::DEFAULT
265 end
resource(type_name = nil, title = nil) click to toggle source

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

    # File lib/puppet/pops/types/type_factory.rb
426 def self.resource(type_name = nil, title = nil)
427   case type_name
428   when PResourceType
429     PResourceType.new(type_name.type_name, title)
430   when String
431     type_name = TypeFormatter.singleton.capitalize_segments(type_name)
432     raise ArgumentError, "Illegal type name '#{type_name}'" unless type_name =~ Patterns::CLASSREF_EXT
433     PResourceType.new(type_name, title)
434   when nil
435     raise ArgumentError, 'The type name cannot be nil, if title is given' unless title.nil?
436     PResourceType::DEFAULT
437   else
438     raise ArgumentError, "The type name cannot be a #{type_name.class.name}"
439   end
440 end
rich_data() click to toggle source

Produces the RichData type @api public

    # File lib/puppet/pops/types/type_factory.rb
377 def self.rich_data
378   @rich_data_t ||= TypeParser.singleton.parse('RichData', Loaders.static_loader)
379 end
rich_data_key() click to toggle source

Produces the RichData type @api public

    # File lib/puppet/pops/types/type_factory.rb
384 def self.rich_data_key
385   @rich_data_key_t ||= TypeParser.singleton.parse('RichDataKey', Loaders.static_loader)
386 end
ruby(o) click to toggle source

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

To get the type for the class' class use `TypeCalculator.infer(c)`

@overload ruby(o)

@param o [Class] produces the type corresponding to the class (e.g.
  Integer becomes PIntegerType)

@overload ruby(o)

@param o [Object] produces the type corresponding to the instance class
  (e.g. 3 becomes PIntegerType)

@api public

    # File lib/puppet/pops/types/type_factory.rb
583 def self.ruby(o)
584   if o.is_a?(Class)
585     @type_calculator.type(o)
586   else
587     PRuntimeType.new(:ruby, o.class.name)
588   end
589 end
ruby_type(class_name = nil) click to toggle source

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.

    # File lib/puppet/pops/types/type_factory.rb
595 def self.ruby_type(class_name = nil)
596   PRuntimeType.new(:ruby, class_name)
597 end
runtime(runtime=nil, runtime_type_name = nil) click to toggle source

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

    # File lib/puppet/pops/types/type_factory.rb
602 def self.runtime(runtime=nil, runtime_type_name = nil)
603   runtime = runtime.to_sym if runtime.is_a?(String)
604   PRuntimeType.new(runtime, runtime_type_name)
605 end
scalar() click to toggle source

Produces the Scalar type @api public

    # File lib/puppet/pops/types/type_factory.rb
294 def self.scalar
295   PScalarType::DEFAULT
296 end
scalar_data() click to toggle source

Produces the ScalarData type @api public

    # File lib/puppet/pops/types/type_factory.rb
301 def self.scalar_data
302   PScalarDataType::DEFAULT
303 end
sem_ver(*ranges) click to toggle source

Produces an instance of the SemVer type

    # File lib/puppet/pops/types/type_factory.rb
417 def self.sem_ver(*ranges)
418   ranges.empty? ? PSemVerType::DEFAULT : PSemVerType::new(ranges)
419 end
sem_ver_range() click to toggle source

Produces an instance of the SemVerRange type

    # File lib/puppet/pops/types/type_factory.rb
412 def self.sem_ver_range
413   PSemVerRangeType::DEFAULT
414 end
sensitive(type = nil) click to toggle source

Produces the Sensitive type @api public

   # File lib/puppet/pops/types/type_factory.rb
59 def self.sensitive(type = nil)
60   PSensitiveType.new(type)
61 end
string(size_type_or_value = nil, *deprecated_second_argument) click to toggle source

Produces the String type based on nothing, a string value that becomes an exact match constraint, or a parameterized Integer type that constraints the size.

@api public

    # File lib/puppet/pops/types/type_factory.rb
111 def self.string(size_type_or_value = nil, *deprecated_second_argument)
112   if deprecated_second_argument.empty?
113     size_type_or_value.nil? ? PStringType::DEFAULT : PStringType.new(size_type_or_value)
114   else
115     if Puppet[:strict] != :off
116       #TRANSLATORS 'TypeFactory#string' is a class and method name and should not be translated
117       message = _("Passing more than one argument to TypeFactory#string is deprecated")
118       Puppet.warn_once('deprecations', "TypeFactory#string_multi_args", message)
119     end
120     deprecated_second_argument.size == 1 ? PStringType.new(deprecated_second_argument[0]) : PEnumType.new(*deprecated_second_argument)
121   end
122 end
struct(hash = {}) click to toggle source

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.

@param hash [{String,PAnyType=>PAnyType}] key => value hash @return [PStructType] the created Struct type

    # File lib/puppet/pops/types/type_factory.rb
172 def self.struct(hash = {})
173   tc = @type_calculator
174   elements = hash.map do |key_type, value_type|
175     value_type = type_of(value_type)
176     raise ArgumentError, 'Struct element value_type must be a Type' unless value_type.is_a?(PAnyType)
177 
178     # TODO: Should have stricter name rule
179     if key_type.is_a?(String)
180       raise ArgumentError, 'Struct element key cannot be an empty String' if key_type.empty?
181       key_type = string(key_type)
182       # Must make key optional if the value can be Undef
183       key_type = optional(key_type) if tc.assignable?(value_type, PUndefType::DEFAULT)
184     else
185       # assert that the key type is one of String[1], NotUndef[String[1]] and Optional[String[1]]
186       case key_type
187       when PNotUndefType
188         # We can loose the NotUndef wrapper here since String[1] isn't optional anyway
189         key_type = key_type.type
190         s = key_type
191       when POptionalType
192         s = key_type.optional_type
193       when PStringType
194         s = key_type
195       when PEnumType
196         s = key_type.values.size == 1 ? PStringType.new(key_type.values[0]) : nil
197       else
198         raise ArgumentError, "Illegal Struct member key type. Expected NotUndef, Optional, String, or Enum. Got: #{key_type.class.name}"
199       end
200       unless s.is_a?(PStringType) && !s.value.nil?
201         raise ArgumentError, "Unable to extract a non-empty literal string from Struct member key type #{tc.string(key_type)}"
202       end
203     end
204     PStructElement.new(key_type, value_type)
205   end
206   PStructType.new(elements)
207 end
task() click to toggle source
    # File lib/puppet/pops/types/type_factory.rb
538 def self.task
539   @task_t ||= TypeParser.singleton.parse('Task')
540 end
timespan(*args) click to toggle source
    # File lib/puppet/pops/types/type_factory.rb
231 def self.timespan(*args)
232   case args.size
233   when 0
234     PTimespanType::DEFAULT
235   else
236     PTimespanType.new(*args)
237   end
238 end
timestamp(*args) click to toggle source
    # File lib/puppet/pops/types/type_factory.rb
222 def self.timestamp(*args)
223   case args.size
224   when 0
225     PTimestampType::DEFAULT
226   else
227     PTimestampType.new(*args)
228   end
229 end
tuple(types = [], size_type = nil) click to toggle source
    # File lib/puppet/pops/types/type_factory.rb
240 def self.tuple(types = [], size_type = nil)
241   PTupleType.new(types.map {|elem| type_of(elem) }, size_type)
242 end
type_alias(name = nil, expression = nil) click to toggle source

Returns the type alias for the given expression @param name [String] the name of the unresolved type @param expression [Model::Expression] an expression that will evaluate to a type @return [PTypeAliasType] the type alias

    # File lib/puppet/pops/types/type_factory.rb
611 def self.type_alias(name = nil, expression = nil)
612   name.nil? ? PTypeAliasType::DEFAULT : PTypeAliasType.new(name, expression)
613 end
type_of(o) click to toggle source

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.

    # File lib/puppet/pops/types/type_factory.rb
557 def self.type_of(o)
558   if o.is_a?(Class)
559     @type_calculator.type(o)
560   elsif o.is_a?(PAnyType)
561     o
562   elsif o.is_a?(String)
563     PRuntimeType.new(:ruby, o)
564   else
565     @type_calculator.infer_generic(o)
566   end
567 end
type_reference(type_string = nil) click to toggle source

Returns the type that represents a type reference with a given name and optional parameters. @param type_string [String] the string form of the type @return [PTypeReferenceType] the type reference

    # File lib/puppet/pops/types/type_factory.rb
619 def self.type_reference(type_string = nil)
620   type_string == nil ? PTypeReferenceType::DEFAULT : PTypeReferenceType.new(type_string)
621 end
type_set(hash = nil) click to toggle source
    # File lib/puppet/pops/types/type_factory.rb
218 def self.type_set(hash = nil)
219   hash.nil? || hash.empty? ? PTypeSetType::DEFAULT : PTypeSetType.new(hash)
220 end
type_type(inst_type = nil) click to toggle source

Produces a type for Type @api public

    # File lib/puppet/pops/types/type_factory.rb
527 def self.type_type(inst_type = nil)
528   inst_type.nil? ? PTypeType::DEFAULT : PTypeType.new(inst_type)
529 end
undef() click to toggle source

Creates an instance of the Undef type @api public

    # File lib/puppet/pops/types/type_factory.rb
390 def self.undef
391   PUndefType::DEFAULT
392 end
uri(string_uri_or_hash = nil) click to toggle source

Produces a type for URI[String or Hash] @api public

    # File lib/puppet/pops/types/type_factory.rb
549 def self.uri(string_uri_or_hash = nil)
550   string_uri_or_hash.nil? ? PURIType::DEFAULT : PURIType.new(string_uri_or_hash)
551 end
variant(*types) click to toggle source

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

    # File lib/puppet/pops/types/type_factory.rb
157 def self.variant(*types)
158   PVariantType.maybe_create(types.map {|v| type_of(v) })
159 end