Module: Puppet::Pops::Types::TypeFactory
- Defined in:
- lib/puppet/pops/types/type_factory.rb
Class Method Summary collapse
-
.all_callables ⇒ Object
Produces a CallableType matching all callables.
-
.any ⇒ Object
Produces the Any type.
-
.array_of(o, size_type = nil) ⇒ Object
Produces a type for Array where o is either a type, or an instance for which a type is inferred.
-
.array_of_data ⇒ Object
Produces a type for Array.
-
.binary ⇒ Object
Creates an instance of the Binary type.
-
.boolean ⇒ Object
Produces the Boolean type.
-
.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.
-
.catalog_entry ⇒ Object
Produces an instance of the abstract type PCatalogEntryType.
-
.collection(size_type = nil) ⇒ Object
Produces the abstract type Collection.
-
.data ⇒ Object
Produces the Data type.
-
.default ⇒ Object
Creates an instance of the Default type.
-
.enum(*values) ⇒ Object
Produces the Enum type, optionally with specific string values.
-
.float ⇒ Object
Produces the Float type.
-
.float_range(from, to) ⇒ Object
Produces a Float range type.
-
.hash_kv(key_type, value_type, size_type = nil) ⇒ PHashType
Produces a type for Hash.
-
.hash_of(value, key = scalar, size_type = nil) ⇒ Object
Produces a type for Hash[Scalar, o] where o is either a type, or an instance for which a type is inferred.
-
.hash_of_data ⇒ Object
Produces a type for Hash[Scalar, Data].
-
.host_class(class_name = nil) ⇒ Object
Produces PHostClassType with a string class_name.
-
.integer ⇒ Object
Produces the Integer type.
-
.is_range_parameter?(t) ⇒ Boolean
Returns true if the given type t is of valid range parameter type (integer or literal default).
-
.iterable(elem_type = nil) ⇒ Object
Produces the Iterable type.
-
.iterator(elem_type = nil) ⇒ Object
Produces the Iterator type.
-
.label(t) ⇒ Object
Produces a string representation of the type.
- .not_undef(inst_type = nil) ⇒ PNotUndefType
-
.numeric ⇒ Object
Produces the Numeric type.
-
.object(hash = nil) ⇒ PObjectType
Produces an ‘Object` type from the given hash that represents the features of the object.
-
.optional(optional_type = nil) ⇒ POptionalType
Produces the Optional type, i.e.
- .pattern(*regular_expressions) ⇒ Object
-
.range(from, to) ⇒ Object
Produces an Integer range type.
-
.regexp(pattern = nil) ⇒ Object
Produces the Regexp type.
-
.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.
-
.ruby(o) ⇒ Object
Produces a type for a class or infers a type for something that is not a class.
-
.ruby_type(class_name = nil) ⇒ Object
Generic creator of a RuntimeType - allows creating the Ruby type with nil name, or String name.
-
.runtime(runtime = nil, runtime_type_name = nil) ⇒ Object
Generic creator of a RuntimeType - allows creating the type with nil or String runtime_type_name.
-
.scalar ⇒ Object
Produces the Literal type.
-
.sem_ver(*ranges) ⇒ Object
Produces an instance of the SemVer type.
-
.sem_ver_range ⇒ Object
Produces an instance of the SemVerRange type.
-
.sensitive(type = nil) ⇒ Object
Produces the Sensitive type.
-
.string(size_type = nil, *values) ⇒ Object
Produces the String type, optionally with specific string values.
-
.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.
- .timespan(*args) ⇒ Object
- .timestamp(*args) ⇒ Object
- .tuple(types = [], size_type = nil) ⇒ Object
-
.type_alias(name = nil, expression = nil) ⇒ PTypeAliasType
Returns the type alias for the given expression.
-
.type_of(o) ⇒ Object
Produce a type corresponding to the class of given unless given is a String, Class or a PAnyType.
-
.type_reference(type_string = nil) ⇒ PTypeReferenceType
Returns the type that represents a type reference with a given name and optional parameters.
- .type_set(hash = nil) ⇒ Object
-
.type_type(inst_type = nil) ⇒ Object
Produces a type for Type.
-
.undef ⇒ Object
Creates an instance of the Undef type.
-
.variant(*types) ⇒ Object
Produces the Variant type, optionally with the “one of” types.
Class Method Details
.all_callables ⇒ Object
Produces a CallableType matching all callables
254 255 256 |
# File 'lib/puppet/pops/types/type_factory.rb', line 254 def self.all_callables return PCallableType::DEFAULT end |
.any ⇒ Object
Produces the Any type
207 208 209 |
# File 'lib/puppet/pops/types/type_factory.rb', line 207 def self.any PAnyType::DEFAULT end |
.array_of(o, size_type = nil) ⇒ Object
Produces a type for Array where o is either a type, or an instance for which a type is inferred.
391 392 393 |
# File 'lib/puppet/pops/types/type_factory.rb', line 391 def self.array_of(o, size_type = nil) PArrayType.new(type_of(o), size_type) end |
.array_of_data ⇒ Object
Produces a type for Array
417 418 419 |
# File 'lib/puppet/pops/types/type_factory.rb', line 417 def self.array_of_data PArrayType::DATA end |
.binary ⇒ Object
Creates an instance of the Binary type
334 335 336 |
# File 'lib/puppet/pops/types/type_factory.rb', line 334 def self.binary PBinaryType::DEFAULT end |
.boolean ⇒ Object
Produces the Boolean type
200 201 202 |
# File 'lib/puppet/pops/types/type_factory.rb', line 200 def self.boolean PBooleanType::DEFAULT 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.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/puppet/pops/types/type_factory.rb', line 274 def self.callable(*params) if params.size == 2 && params[0].is_a?(Array) return_t = type_of(params[1]) params = params[0] else return_t = nil end last_callable = TypeCalculator.is_kind_of_callable?(params.last) 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] else size_type = nil 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 << PUnitType::DEFAULT end # create a signature tuple_t = tuple(types, size_type) PCallableType.new(tuple_t, block_t, return_t) end |
.catalog_entry ⇒ Object
Produces an instance of the abstract type PCatalogEntryType
339 340 341 |
# File 'lib/puppet/pops/types/type_factory.rb', line 339 def self.catalog_entry PCatalogEntryType::DEFAULT end |
.collection(size_type = nil) ⇒ Object
Produces the abstract type Collection
309 310 311 |
# File 'lib/puppet/pops/types/type_factory.rb', line 309 def self.collection(size_type = nil) size_type.nil? ? PCollectionType::DEFAULT : PCollectionType.new(nil, size_type) end |
.data ⇒ Object
Produces the Data type
316 317 318 |
# File 'lib/puppet/pops/types/type_factory.rb', line 316 def self.data PDataType::DEFAULT end |
.default ⇒ Object
Creates an instance of the Default type
328 329 330 |
# File 'lib/puppet/pops/types/type_factory.rb', line 328 def self.default PDefaultType::DEFAULT end |
.enum(*values) ⇒ Object
Produces the Enum type, optionally with specific string values
101 102 103 |
# File 'lib/puppet/pops/types/type_factory.rb', line 101 def self.enum(*values) PEnumType.new(values) end |
.float ⇒ Object
Produces the Float type
39 40 41 |
# File 'lib/puppet/pops/types/type_factory.rb', line 39 def self.float PFloatType::DEFAULT end |
.float_range(from, to) ⇒ Object
Produces a Float range type
29 30 31 32 33 34 |
# File 'lib/puppet/pops/types/type_factory.rb', line 29 def self.float_range(from, to) # optimize eq with symbol (faster when it is left) from = Float(from) unless :default == from || from.nil? to = Float(to) unless :default == to || to.nil? PFloatType.new(from, to) end |
.hash_kv(key_type, value_type, size_type = nil) ⇒ PHashType
Produces a type for Hash
410 411 412 |
# File 'lib/puppet/pops/types/type_factory.rb', line 410 def self.hash_kv(key_type, value_type, size_type = nil) PHashType.new(key_type, value_type, size_type) end |
.hash_of(value, key = scalar, size_type = nil) ⇒ Object
Produces a type for Hash[Scalar, o] where o is either a type, or an instance for which a type is inferred.
399 400 401 |
# File 'lib/puppet/pops/types/type_factory.rb', line 399 def self.hash_of(value, key = scalar, size_type = nil) PHashType.new(type_of(key), type_of(value), size_type) end |
.hash_of_data ⇒ Object
Produces a type for Hash[Scalar, Data]
424 425 426 |
# File 'lib/puppet/pops/types/type_factory.rb', line 424 def self.hash_of_data PHashType::DATA 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.
379 380 381 382 383 384 385 |
# File 'lib/puppet/pops/types/type_factory.rb', line 379 def self.host_class(class_name = nil) if class_name.nil? PHostClassType::DEFAULT else PHostClassType.new(class_name.sub(/^::/, '')) end end |
.integer ⇒ Object
Produces the Integer type
12 13 14 |
# File 'lib/puppet/pops/types/type_factory.rb', line 12 def self.integer PIntegerType::DEFAULT end |
.is_range_parameter?(t) ⇒ Boolean
Returns true if the given type t is of valid range parameter type (integer or literal default).
521 522 523 |
# File 'lib/puppet/pops/types/type_factory.rb', line 521 def self.is_range_parameter?(t) t.is_a?(Integer) || t == 'default' || :default == t end |
.iterable(elem_type = nil) ⇒ Object
Produces the Iterable type
60 61 62 |
# File 'lib/puppet/pops/types/type_factory.rb', line 60 def self.iterable(elem_type = nil) elem_type.nil? ? PIterableType::DEFAULT : PIterableType.new(elem_type) end |
.iterator(elem_type = nil) ⇒ Object
Produces the Iterator type
67 68 69 |
# File 'lib/puppet/pops/types/type_factory.rb', line 67 def self.iterator(elem_type = nil) elem_type.nil? ? PIteratorType::DEFAULT : PIteratorType.new(elem_type) end |
.label(t) ⇒ Object
Produces a string representation of the type
74 75 76 |
# File 'lib/puppet/pops/types/type_factory.rb', line 74 def self.label(t) @type_calculator.string(t) end |
.not_undef(inst_type = nil) ⇒ PNotUndefType
437 438 439 440 |
# File 'lib/puppet/pops/types/type_factory.rb', line 437 def self.not_undef(inst_type = nil) inst_type = string(nil, inst_type) if inst_type.is_a?(String) PNotUndefType.new(inst_type) end |
.numeric ⇒ Object
Produces the Numeric type
53 54 55 |
# File 'lib/puppet/pops/types/type_factory.rb', line 53 def self.numeric PNumericType::DEFAULT end |
.object(hash = nil) ⇒ PObjectType
Produces an ‘Object` type from the given hash that represents the features of the object
163 164 165 |
# File 'lib/puppet/pops/types/type_factory.rb', line 163 def self.object(hash = nil) hash.nil? || hash.empty? ? PObjectType::DEFAULT : PObjectType.new(hash) 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.
94 95 96 |
# File 'lib/puppet/pops/types/type_factory.rb', line 94 def self.optional(optional_type = nil) POptionalType.new(type_of(optional_type.is_a?(String) ? string(nil, optional_type) : type_of(optional_type))) end |
.pattern(*regular_expressions) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/puppet/pops/types/type_factory.rb', line 220 def self.pattern(*regular_expressions) patterns = regular_expressions.map do |re| case re when String re_t = PRegexpType.new(re) re_t.regexp # compile it to catch errors re_t when Regexp PRegexpType.new(re) when PRegexpType re when PPatternType re.patterns else raise ArgumentError, "Only String, Regexp, Pattern-Type, and Regexp-Type are allowed: got '#{re.class}" end end.flatten.uniq PPatternType.new(patterns) end |
.range(from, to) ⇒ Object
Produces an Integer range type
19 20 21 22 23 24 |
# File 'lib/puppet/pops/types/type_factory.rb', line 19 def self.range(from, to) # optimize eq with symbol (faster when it is left) from = :default == from if from == 'default' to = :default if to == 'default' PIntegerType.new(from, to) end |
.regexp(pattern = nil) ⇒ Object
Produces the Regexp type
216 217 218 |
# File 'lib/puppet/pops/types/type_factory.rb', line 216 def self.regexp(pattern = nil) pattern ? PRegexpType.new(pattern) : PRegexpType::DEFAULT 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)).
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/puppet/pops/types/type_factory.rb', line 358 def self.resource(type_name = nil, title = nil) case type_name when PResourceType PResourceType.new(type_name.type_name, title) when String type_name = TypeFormatter.singleton.capitalize_segments(type_name) raise ArgumentError, "Illegal type name '#{type_name}'" unless type_name =~ Patterns::CLASSREF_EXT PResourceType.new(type_name, title) when nil raise ArgumentError, 'The type name cannot be nil, if title is given' unless title.nil? PResourceType::DEFAULT else raise ArgumentError, "The type name cannot be a #{type_name.class.name}" end end |
.ruby(o) ⇒ Object .ruby(o) ⇒ Object
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
479 480 481 482 483 484 485 |
# File 'lib/puppet/pops/types/type_factory.rb', line 479 def self.ruby(o) if o.is_a?(Class) @type_calculator.type(o) else PRuntimeType.new(:ruby, 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.
491 492 493 |
# File 'lib/puppet/pops/types/type_factory.rb', line 491 def self.ruby_type(class_name = nil) PRuntimeType.new(:ruby, 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).
498 499 500 501 |
# File 'lib/puppet/pops/types/type_factory.rb', line 498 def self.runtime(runtime=nil, runtime_type_name = nil) runtime = runtime.to_sym if runtime.is_a?(String) PRuntimeType.new(runtime, runtime_type_name) end |
.scalar ⇒ Object
Produces the Literal type
247 248 249 |
# File 'lib/puppet/pops/types/type_factory.rb', line 247 def self.scalar PScalarType::DEFAULT end |
.sem_ver(*ranges) ⇒ Object
Produces an instance of the SemVer type
349 350 351 |
# File 'lib/puppet/pops/types/type_factory.rb', line 349 def self.sem_ver(*ranges) ranges.empty? ? PSemVerType::DEFAULT : PSemVerType::new(ranges) end |
.sem_ver_range ⇒ Object
Produces an instance of the SemVerRange type
344 345 346 |
# File 'lib/puppet/pops/types/type_factory.rb', line 344 def self.sem_ver_range PSemVerRangeType::DEFAULT end |
.sensitive(type = nil) ⇒ Object
Produces the Sensitive type
46 47 48 |
# File 'lib/puppet/pops/types/type_factory.rb', line 46 def self.sensitive(type = nil) PSensitiveType.new(type) end |
.string(size_type = nil, *values) ⇒ Object
Produces the String type, optionally with specific string values
81 82 83 |
# File 'lib/puppet/pops/types/type_factory.rb', line 81 def self.string(size_type = nil, *values) PStringType.new(size_type, values) 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.
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 154 155 156 |
# File 'lib/puppet/pops/types/type_factory.rb', line 123 def self.struct(hash = {}) tc = @type_calculator 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?(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(nil, key_type) # Must make key optional if the value can be Undef key_type = optional(key_type) if tc.assignable?(value_type, PUndefType::DEFAULT) else # assert that the key type is one of String[1], NotUndef[String[1]] and Optional[String[1]] case key_type when PNotUndefType # We can loose the NotUndef wrapper here since String[1] isn't optional anyway key_type = key_type.type s = key_type when POptionalType s = key_type.optional_type when PStringType, PEnumType s = key_type else raise ArgumentError, "Illegal Struct member key type. Expected NotUndef, Optional, String, or Enum. Got: #{key_type.class.name}" end unless (s.is_a?(PStringType) || s.is_a?(PEnumType)) && s.values.size == 1 && !s.values[0].empty? raise ArgumentError, "Unable to extract a non-empty literal string from Struct member key type #{tc.string(key_type)}" end end PStructElement.new(key_type, value_type) end PStructType.new(elements) end |
.timespan(*args) ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/puppet/pops/types/type_factory.rb', line 182 def self.timespan(*args) case args.size when 0 PTimespanType::DEFAULT when 1 PTimespanType.new(args[0], args[0]) else PTimespanType.new(*args) end end |
.timestamp(*args) ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/puppet/pops/types/type_factory.rb', line 171 def self.(*args) case args.size when 0 PTimestampType::DEFAULT when 1 PTimestampType.new(args[0], args[0]) else PTimestampType.new(*args) end end |
.tuple(types = [], size_type = nil) ⇒ Object
193 194 195 |
# File 'lib/puppet/pops/types/type_factory.rb', line 193 def self.tuple(types = [], size_type = nil) PTupleType.new(types.map {|elem| type_of(elem) }, size_type) end |
.type_alias(name = nil, expression = nil) ⇒ PTypeAliasType
Returns the type alias for the given expression
507 508 509 |
# File 'lib/puppet/pops/types/type_factory.rb', line 507 def self.type_alias(name = nil, expression = nil) name.nil? ? PTypeAliasType::DEFAULT : PTypeAliasType.new(name, expression) 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.
453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/puppet/pops/types/type_factory.rb', line 453 def self.type_of(o) if o.is_a?(Class) @type_calculator.type(o) elsif o.is_a?(PAnyType) o elsif o.is_a?(String) PRuntimeType.new(:ruby, o) else @type_calculator.infer_generic(o) end end |
.type_reference(type_string = nil) ⇒ PTypeReferenceType
Returns the type that represents a type reference with a given name and optional parameters.
515 516 517 |
# File 'lib/puppet/pops/types/type_factory.rb', line 515 def self.type_reference(type_string = nil) type_string == nil ? PTypeReferenceType::DEFAULT : PTypeReferenceType.new(type_string) end |
.type_set(hash = nil) ⇒ Object
167 168 169 |
# File 'lib/puppet/pops/types/type_factory.rb', line 167 def self.type_set(hash = nil) hash.nil? || hash.empty? ? PTypeSetType::DEFAULT : PTypeSetType.new(hash) end |
.type_type(inst_type = nil) ⇒ Object
Produces a type for Type
445 446 447 |
# File 'lib/puppet/pops/types/type_factory.rb', line 445 def self.type_type(inst_type = nil) inst_type.nil? ? PType::DEFAULT : PType.new(inst_type) end |
.undef ⇒ Object
Creates an instance of the Undef type
322 323 324 |
# File 'lib/puppet/pops/types/type_factory.rb', line 322 def self.undef PUndefType::DEFAULT end |
.variant(*types) ⇒ Object
Produces the Variant type, optionally with the “one of” types
108 109 110 |
# File 'lib/puppet/pops/types/type_factory.rb', line 108 def self.variant(*types) PVariantType.maybe_create(types.map {|v| type_of(v) }) end |