module Puppet::Pops::Types::TypeFactory
Helper module that makes creation of type objects simpler. @api public
Public Class Methods
Produces a CallableType matching all callables @api public
# File lib/puppet/pops/types/type_factory.rb 309 def self.all_callables 310 return PCallableType::DEFAULT 311 end
Produces the Any type @api public
# File lib/puppet/pops/types/type_factory.rb 255 def self.any 256 PAnyType::DEFAULT 257 end
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 460 def self.array_of(o, size_type = nil) 461 PArrayType.new(type_of(o), size_type) 462 end
Produces a type for Array @api public
# File lib/puppet/pops/types/type_factory.rb 486 def self.array_of_any 487 PArrayType::DEFAULT 488 end
Produces a type for Array @api public
# File lib/puppet/pops/types/type_factory.rb 493 def self.array_of_data 494 @array_of_data_t = PArrayType.new(data) 495 end
Creates an instance of the Binary type @api public
# File lib/puppet/pops/types/type_factory.rb 403 def self.binary 404 PBinaryType::DEFAULT 405 end
Produces the Boolean type @api public
# File lib/puppet/pops/types/type_factory.rb 248 def self.boolean(value = nil) 249 value.nil? ? PBooleanType::DEFAULT : (value ? PBooleanType::TRUE : PBooleanType::FALSE) 250 end
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 329 def self.callable(*params) 330 if params.size == 2 && params[0].is_a?(Array) 331 return_t = type_of(params[1]) 332 params = params[0] 333 else 334 return_t = nil 335 end 336 last_callable = TypeCalculator.is_kind_of_callable?(params.last) 337 block_t = last_callable ? params.pop : nil 338 339 # compute a size_type for the signature based on the two last parameters 340 if is_range_parameter?(params[-2]) && is_range_parameter?(params[-1]) 341 size_type = range(params[-2], params[-1]) 342 params = params[0, params.size - 2] 343 elsif is_range_parameter?(params[-1]) 344 size_type = range(params[-1], :default) 345 params = params[0, params.size - 1] 346 else 347 size_type = nil 348 end 349 350 types = params.map {|p| type_of(p) } 351 352 # If the specification requires types, and none were given, a Unit type is used 353 if types.empty? && !size_type.nil? && size_type.range[1] > 0 354 types << PUnitType::DEFAULT 355 end 356 # create a signature 357 tuple_t = tuple(types, size_type) 358 PCallableType.new(tuple_t, block_t, return_t) 359 end
Produces an instance of the abstract type PCatalogEntryType
# File lib/puppet/pops/types/type_factory.rb 408 def self.catalog_entry 409 PCatalogEntryType::DEFAULT 410 end
Clears caches - used when testing
# File lib/puppet/pops/types/type_factory.rb 11 def self.clear 12 # these types are cached and needs to be nulled as the representation may change if loaders are cleared 13 @data_t = nil 14 @rich_data_t = nil 15 @rich_data_key_t = nil 16 @array_of_data_t = nil 17 @hash_of_data_t = nil 18 @error_t = nil 19 @task_t = nil 20 @deferred_t = nil 21 end
Produces the abstract type Collection @api public
# File lib/puppet/pops/types/type_factory.rb 364 def self.collection(size_type = nil) 365 size_type.nil? ? PCollectionType::DEFAULT : PCollectionType.new(size_type) 366 end
Produces the Data type @api public
# File lib/puppet/pops/types/type_factory.rb 371 def self.data 372 @data_t ||= TypeParser.singleton.parse('Data', Loaders.static_loader) 373 end
Creates an instance of the Default type @api public
# File lib/puppet/pops/types/type_factory.rb 397 def self.default 398 PDefaultType::DEFAULT 399 end
# File lib/puppet/pops/types/type_factory.rb 543 def self.deferred 544 @deferred_t ||= TypeParser.singleton.parse('Deferred') 545 end
Produces the Enum type, optionally with specific string values @api public
# File lib/puppet/pops/types/type_factory.rb 145 def self.enum(*values) 146 last = values.last 147 case_insensitive = false 148 if last == true || last == false 149 case_insensitive = last 150 values = values[0...-1] 151 end 152 PEnumType.new(values, case_insensitive) 153 end
Produces a type for Error @api public
# File lib/puppet/pops/types/type_factory.rb 535 def self.error 536 @error_t ||= TypeParser.singleton.parse('Error', Loaders.loaders.puppet_system_loader) 537 end
Produces the Float type @api public
# File lib/puppet/pops/types/type_factory.rb 53 def self.float 54 PFloatType::DEFAULT 55 end
Produces a Float range type @api public
# File lib/puppet/pops/types/type_factory.rb 43 def self.float_range(from, to) 44 # optimize eq with symbol (faster when it is left) 45 from = Float(from) unless :default == from || from.nil? 46 to = Float(to) unless :default == to || to.nil? 47 PFloatType.new(from, to) 48 end
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 479 def self.hash_kv(key_type, value_type, size_type = nil) 480 PHashType.new(key_type, value_type, size_type) 481 end
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 468 def self.hash_of(value, key = scalar, size_type = nil) 469 PHashType.new(type_of(key), type_of(value), size_type) 470 end
Produces a type for Hash @api public
# File lib/puppet/pops/types/type_factory.rb 500 def self.hash_of_any 501 PHashType::DEFAULT 502 end
Produces a type for Hash @api public
# File lib/puppet/pops/types/type_factory.rb 507 def self.hash_of_data 508 @hash_of_data_t = PHashType.new(string, data) 509 end
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 448 def self.host_class(class_name = nil) 449 if class_name.nil? 450 PClassType::DEFAULT 451 else 452 PClassType.new(class_name.sub(/^::/, '')) 453 end 454 end
Produces the Init type @api public
# File lib/puppet/pops/types/type_factory.rb 73 def self.init(*args) 74 case args.size 75 when 0 76 PInitType::DEFAULT 77 when 1 78 type = args[0] 79 type.nil? ? PInitType::DEFAULT : PInitType.new(type, EMPTY_ARRAY) 80 else 81 type = args.shift 82 PInitType.new(type, args) 83 end 84 end
Produces the Integer type @api public
# File lib/puppet/pops/types/type_factory.rb 26 def self.integer 27 PIntegerType::DEFAULT 28 end
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 626 def self.is_range_parameter?(t) 627 t.is_a?(Integer) || t == 'default' || :default == t 628 end
Produces the Iterable type @api public
# File lib/puppet/pops/types/type_factory.rb 89 def self.iterable(elem_type = nil) 90 elem_type.nil? ? PIterableType::DEFAULT : PIterableType.new(elem_type) 91 end
Produces the Iterator type @api public
# File lib/puppet/pops/types/type_factory.rb 96 def self.iterator(elem_type = nil) 97 elem_type.nil? ? PIteratorType::DEFAULT : PIteratorType.new(elem_type) 98 end
Produces a string representation of the type @api public
# File lib/puppet/pops/types/type_factory.rb 103 def self.label(t) 104 @type_calculator.string(t) 105 end
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 520 def self.not_undef(inst_type = nil) 521 inst_type = string(inst_type) if inst_type.is_a?(String) 522 PNotUndefType.new(inst_type) 523 end
Produces the Numeric type @api public
# File lib/puppet/pops/types/type_factory.rb 67 def self.numeric 68 PNumericType::DEFAULT 69 end
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 215 def self.object(hash = nil, loader = nil) 216 hash.nil? || hash.empty? ? PObjectType::DEFAULT : PObjectType.new(hash, loader) 217 end
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 134 def self.optional(optional_type = nil) 135 if optional_type.nil? 136 POptionalType::DEFAULT 137 else 138 POptionalType.new(type_of(optional_type.is_a?(String) ? string(optional_type) : type_of(optional_type))) 139 end 140 end
# File lib/puppet/pops/types/type_factory.rb 268 def self.pattern(*regular_expressions) 269 patterns = regular_expressions.map do |re| 270 case re 271 when String 272 re_t = PRegexpType.new(re) 273 re_t.regexp # compile it to catch errors 274 re_t 275 276 when Regexp 277 PRegexpType.new(re) 278 279 when PRegexpType 280 re 281 282 when PPatternType 283 re.patterns 284 285 else 286 raise ArgumentError, "Only String, Regexp, Pattern-Type, and Regexp-Type are allowed: got '#{re.class}" 287 end 288 end.flatten.uniq 289 PPatternType.new(patterns) 290 end
Produces an Integer range type @api public
# File lib/puppet/pops/types/type_factory.rb 33 def self.range(from, to) 34 # optimize eq with symbol (faster when it is left) 35 from = :default == from if from == 'default' 36 to = :default if to == 'default' 37 PIntegerType.new(from, to) 38 end
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 264 def self.regexp(pattern = nil) 265 pattern ? PRegexpType.new(pattern) : PRegexpType::DEFAULT 266 end
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 427 def self.resource(type_name = nil, title = nil) 428 case type_name 429 when PResourceType 430 PResourceType.new(type_name.type_name, title) 431 when String 432 type_name = TypeFormatter.singleton.capitalize_segments(type_name) 433 raise ArgumentError, "Illegal type name '#{type_name}'" unless type_name =~ Patterns::CLASSREF_EXT 434 PResourceType.new(type_name, title) 435 when nil 436 raise ArgumentError, 'The type name cannot be nil, if title is given' unless title.nil? 437 PResourceType::DEFAULT 438 else 439 raise ArgumentError, "The type name cannot be a #{type_name.class.name}" 440 end 441 end
Produces the RichData type @api public
# File lib/puppet/pops/types/type_factory.rb 378 def self.rich_data 379 @rich_data_t ||= TypeParser.singleton.parse('RichData', Loaders.static_loader) 380 end
Produces the RichData type @api public
# File lib/puppet/pops/types/type_factory.rb 385 def self.rich_data_key 386 @rich_data_key_t ||= TypeParser.singleton.parse('RichDataKey', Loaders.static_loader) 387 end
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 584 def self.ruby(o) 585 if o.is_a?(Class) 586 @type_calculator.type(o) 587 else 588 PRuntimeType.new(:ruby, o.class.name) 589 end 590 end
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 596 def self.ruby_type(class_name = nil) 597 PRuntimeType.new(:ruby, class_name) 598 end
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 603 def self.runtime(runtime=nil, runtime_type_name = nil) 604 runtime = runtime.to_sym if runtime.is_a?(String) 605 PRuntimeType.new(runtime, runtime_type_name) 606 end
Produces the Scalar type @api public
# File lib/puppet/pops/types/type_factory.rb 295 def self.scalar 296 PScalarType::DEFAULT 297 end
Produces the ScalarData type @api public
# File lib/puppet/pops/types/type_factory.rb 302 def self.scalar_data 303 PScalarDataType::DEFAULT 304 end
Produces an instance of the SemVer type
# File lib/puppet/pops/types/type_factory.rb 418 def self.sem_ver(*ranges) 419 ranges.empty? ? PSemVerType::DEFAULT : PSemVerType::new(ranges) 420 end
Produces an instance of the SemVerRange type
# File lib/puppet/pops/types/type_factory.rb 413 def self.sem_ver_range 414 PSemVerRangeType::DEFAULT 415 end
Produces the Sensitive type @api public
# File lib/puppet/pops/types/type_factory.rb 60 def self.sensitive(type = nil) 61 PSensitiveType.new(type) 62 end
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 112 def self.string(size_type_or_value = nil, *deprecated_second_argument) 113 if deprecated_second_argument.empty? 114 size_type_or_value.nil? ? PStringType::DEFAULT : PStringType.new(size_type_or_value) 115 else 116 if Puppet[:strict] != :off 117 #TRANSLATORS 'TypeFactory#string' is a class and method name and should not be translated 118 message = _("Passing more than one argument to TypeFactory#string is deprecated") 119 Puppet.warn_once('deprecations', "TypeFactory#string_multi_args", message) 120 end 121 deprecated_second_argument.size == 1 ? PStringType.new(deprecated_second_argument[0]) : PEnumType.new(*deprecated_second_argument) 122 end 123 end
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 173 def self.struct(hash = {}) 174 tc = @type_calculator 175 elements = hash.map do |key_type, value_type| 176 value_type = type_of(value_type) 177 raise ArgumentError, 'Struct element value_type must be a Type' unless value_type.is_a?(PAnyType) 178 179 # TODO: Should have stricter name rule 180 if key_type.is_a?(String) 181 raise ArgumentError, 'Struct element key cannot be an empty String' if key_type.empty? 182 key_type = string(key_type) 183 # Must make key optional if the value can be Undef 184 key_type = optional(key_type) if tc.assignable?(value_type, PUndefType::DEFAULT) 185 else 186 # assert that the key type is one of String[1], NotUndef[String[1]] and Optional[String[1]] 187 case key_type 188 when PNotUndefType 189 # We can loose the NotUndef wrapper here since String[1] isn't optional anyway 190 key_type = key_type.type 191 s = key_type 192 when POptionalType 193 s = key_type.optional_type 194 when PStringType 195 s = key_type 196 when PEnumType 197 s = key_type.values.size == 1 ? PStringType.new(key_type.values[0]) : nil 198 else 199 raise ArgumentError, "Illegal Struct member key type. Expected NotUndef, Optional, String, or Enum. Got: #{key_type.class.name}" 200 end 201 unless s.is_a?(PStringType) && !s.value.nil? 202 raise ArgumentError, "Unable to extract a non-empty literal string from Struct member key type #{tc.string(key_type)}" 203 end 204 end 205 PStructElement.new(key_type, value_type) 206 end 207 PStructType.new(elements) 208 end
# File lib/puppet/pops/types/type_factory.rb 539 def self.task 540 @task_t ||= TypeParser.singleton.parse('Task') 541 end
# File lib/puppet/pops/types/type_factory.rb 232 def self.timespan(*args) 233 case args.size 234 when 0 235 PTimespanType::DEFAULT 236 else 237 PTimespanType.new(*args) 238 end 239 end
# File lib/puppet/pops/types/type_factory.rb 223 def self.timestamp(*args) 224 case args.size 225 when 0 226 PTimestampType::DEFAULT 227 else 228 PTimestampType.new(*args) 229 end 230 end
# File lib/puppet/pops/types/type_factory.rb 241 def self.tuple(types = [], size_type = nil) 242 PTupleType.new(types.map {|elem| type_of(elem) }, size_type) 243 end
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 612 def self.type_alias(name = nil, expression = nil) 613 name.nil? ? PTypeAliasType::DEFAULT : PTypeAliasType.new(name, expression) 614 end
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 558 def self.type_of(o) 559 if o.is_a?(Class) 560 @type_calculator.type(o) 561 elsif o.is_a?(PAnyType) 562 o 563 elsif o.is_a?(String) 564 PRuntimeType.new(:ruby, o) 565 else 566 @type_calculator.infer_generic(o) 567 end 568 end
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 620 def self.type_reference(type_string = nil) 621 type_string == nil ? PTypeReferenceType::DEFAULT : PTypeReferenceType.new(type_string) 622 end
# File lib/puppet/pops/types/type_factory.rb 219 def self.type_set(hash = nil) 220 hash.nil? || hash.empty? ? PTypeSetType::DEFAULT : PTypeSetType.new(hash) 221 end
Produces a type for Type @api public
# File lib/puppet/pops/types/type_factory.rb 528 def self.type_type(inst_type = nil) 529 inst_type.nil? ? PTypeType::DEFAULT : PTypeType.new(inst_type) 530 end
Creates an instance of the Undef type @api public
# File lib/puppet/pops/types/type_factory.rb 391 def self.undef 392 PUndefType::DEFAULT 393 end
Produces a type for URI[String or Hash] @api public
# File lib/puppet/pops/types/type_factory.rb 550 def self.uri(string_uri_or_hash = nil) 551 string_uri_or_hash.nil? ? PURIType::DEFAULT : PURIType.new(string_uri_or_hash) 552 end
Produces the Variant type, optionally with the “one of” types @api public
# File lib/puppet/pops/types/type_factory.rb 158 def self.variant(*types) 159 PVariantType.maybe_create(types.map {|v| type_of(v) }) 160 end