Module: Puppet::Pops::Types::TypeFactory
- Defined in:
- lib/puppet/pops/types/type_factory.rb
Overview
Helper module that makes creation of type objects simpler.
Constant Summary collapse
- Types =
Puppet::Pops::Types
Class Method Summary collapse
-
.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.
-
.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_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).
-
.label(t) ⇒ Object
Produces a string representation of the type.
- .not_undef(inst_type = nil) ⇒ Puppet::Pops::Types::PNotUndefType
-
.numeric ⇒ Object
Produces the Numeric type.
-
.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.
-
.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.
- .tuple(types = [], size_type = nil) ⇒ Object
-
.type_of(o) ⇒ Object
Produce a type corresponding to the class of given unless given is a String, Class or a PAnyType.
-
.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
206 207 208 |
# File 'lib/puppet/pops/types/type_factory.rb', line 206 def self.all_callables return Puppet::Pops::Types::PCallableType::DEFAULT end |
.any ⇒ Object
Produces the Any type
150 151 152 |
# File 'lib/puppet/pops/types/type_factory.rb', line 150 def self.any Types::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.
317 318 319 |
# File 'lib/puppet/pops/types/type_factory.rb', line 317 def self.array_of(o, size_type = nil) Types::PArrayType.new(type_of(o), size_type) end |
.array_of_data ⇒ Object
Produces a type for Array
332 333 334 |
# File 'lib/puppet/pops/types/type_factory.rb', line 332 def self.array_of_data Types::PArrayType::DATA end |
.boolean ⇒ Object
Produces the Boolean type
143 144 145 |
# File 'lib/puppet/pops/types/type_factory.rb', line 143 def self.boolean Types::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.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/puppet/pops/types/type_factory.rb', line 226 def self.callable(*params) last_callable = Puppet::Pops::Types::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 << Types::PUnitType::DEFAULT end # create a signature tuple_t = tuple(types, size_type) Types::PCallableType.new(tuple_t, block_t) end |
.catalog_entry ⇒ Object
Produces an instance of the abstract type PCatalogEntryType
279 280 281 |
# File 'lib/puppet/pops/types/type_factory.rb', line 279 def self.catalog_entry Types::PCatalogEntryType::DEFAULT end |
.collection(size_type = nil) ⇒ Object
Produces the abstract type Collection
255 256 257 |
# File 'lib/puppet/pops/types/type_factory.rb', line 255 def self.collection(size_type = nil) size_type.nil? ? Types::PCollectionType::DEFAULT : Types::PCollectionType.new(nil, size_type) end |
.data ⇒ Object
Produces the Data type
262 263 264 |
# File 'lib/puppet/pops/types/type_factory.rb', line 262 def self.data Types::PDataType::DEFAULT end |
.default ⇒ Object
Creates an instance of the Default type
274 275 276 |
# File 'lib/puppet/pops/types/type_factory.rb', line 274 def self.default Types::PDefaultType::DEFAULT end |
.enum(*values) ⇒ Object
Produces the Enum type, optionally with specific string values
79 80 81 |
# File 'lib/puppet/pops/types/type_factory.rb', line 79 def self.enum(*values) Types::PEnumType.new(values) end |
.float ⇒ Object
Produces the Float type
38 39 40 |
# File 'lib/puppet/pops/types/type_factory.rb', line 38 def self.float Types::PFloatType::DEFAULT end |
.float_range(from, to) ⇒ Object
Produces a Float range type
28 29 30 31 32 33 |
# File 'lib/puppet/pops/types/type_factory.rb', line 28 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? Types::PFloatType.new(from, to) 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.
325 326 327 |
# File 'lib/puppet/pops/types/type_factory.rb', line 325 def self.hash_of(value, key = scalar, size_type = nil) Types::PHashType.new(type_of(key), type_of(value), size_type) end |
.hash_of_data ⇒ Object
Produces a type for Hash[Scalar, Data]
339 340 341 |
# File 'lib/puppet/pops/types/type_factory.rb', line 339 def self.hash_of_data Types::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.
305 306 307 308 309 310 311 |
# File 'lib/puppet/pops/types/type_factory.rb', line 305 def self.host_class(class_name = nil) if class_name.nil? Types::PHostClassType::DEFAULT else Types::PHostClassType.new(class_name.sub(/^::/, '')) end end |
.integer ⇒ Object
Produces the Integer type
11 12 13 |
# File 'lib/puppet/pops/types/type_factory.rb', line 11 def self.integer Types::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).
420 421 422 |
# File 'lib/puppet/pops/types/type_factory.rb', line 420 def self.is_range_parameter?(t) t.is_a?(Integer) || t == 'default' || :default == t end |
.label(t) ⇒ Object
Produces a string representation of the type
52 53 54 |
# File 'lib/puppet/pops/types/type_factory.rb', line 52 def self.label(t) @type_calculator.string(t) end |
.not_undef(inst_type = nil) ⇒ Puppet::Pops::Types::PNotUndefType
352 353 354 355 |
# File 'lib/puppet/pops/types/type_factory.rb', line 352 def self.not_undef(inst_type = nil) inst_type = string(nil, inst_type) if inst_type.is_a?(String) Types::PNotUndefType.new(inst_type) end |
.numeric ⇒ Object
Produces the Numeric type
45 46 47 |
# File 'lib/puppet/pops/types/type_factory.rb', line 45 def self.numeric Types::PNumericType::DEFAULT 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.
72 73 74 |
# File 'lib/puppet/pops/types/type_factory.rb', line 72 def self.optional(optional_type = nil) Types::POptionalType.new(type_of(optional_type.is_a?(String) ? string(nil, optional_type) : type_of(optional_type))) end |
.pattern(*regular_expressions) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/puppet/pops/types/type_factory.rb', line 169 def self.pattern(*regular_expressions) patterns = regular_expressions.map do |re| case re when String re_t = Types::PRegexpType.new(re) re_t.regexp # compile it to catch errors re_t when Regexp # Regep.to_s includes options user did not enter and does not escape source # to work either as a string or as a // regexp. The inspect method does a better # job, but includes the // Types::PRegexpType.new(re.inspect[1..-2]) when Types::PRegexpType re when Types::PPatternType re.patterns else raise ArgumentError, "Only String, Regexp, Pattern-Type, and Regexp-Type are allowed: got '#{re.class}" end end.flatten.uniq Types::PPatternType.new(patterns) end |
.range(from, to) ⇒ Object
Produces an Integer range type
18 19 20 21 22 23 |
# File 'lib/puppet/pops/types/type_factory.rb', line 18 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' Types::PIntegerType.new(from, to) end |
.regexp(pattern = nil) ⇒ Object
Produces the Regexp type
159 160 161 162 163 164 165 166 167 |
# File 'lib/puppet/pops/types/type_factory.rb', line 159 def self.regexp(pattern = nil) if pattern t = Types::PRegexpType.new(pattern.is_a?(Regexp) ? pattern.inspect[1..-2] : pattern) t.regexp unless pattern.nil? # compile pattern to catch errors t else Types::PRegexpType::DEFAULT end 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)).
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/puppet/pops/types/type_factory.rb', line 288 def self.resource(type_name = nil, title = nil) type_name = type_name.type_name if type_name.is_a?(Types::PResourceType) type_name = type_name.downcase unless type_name.nil? unless type_name.nil? || type_name =~ Puppet::Pops::Patterns::CLASSREF raise ArgumentError, "Illegal type name '#{type.type_name}'" end if type_name.nil? && !title.nil? raise ArgumentError, 'The type name cannot be nil, if title is given' end Types::PResourceType.new(type_name, title) 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
394 395 396 397 398 399 400 |
# File 'lib/puppet/pops/types/type_factory.rb', line 394 def self.ruby(o) if o.is_a?(Class) @type_calculator.type(o) else Types::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.
406 407 408 |
# File 'lib/puppet/pops/types/type_factory.rb', line 406 def self.ruby_type(class_name = nil) Types::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).
413 414 415 416 |
# File 'lib/puppet/pops/types/type_factory.rb', line 413 def self.runtime(runtime=nil, runtime_type_name = nil) runtime = runtime.to_sym if runtime.is_a?(String) Types::PRuntimeType.new(runtime, runtime_type_name) end |
.scalar ⇒ Object
Produces the Literal type
199 200 201 |
# File 'lib/puppet/pops/types/type_factory.rb', line 199 def self.scalar Types::PScalarType::DEFAULT end |
.string(size_type = nil, *values) ⇒ Object
Produces the String type, optionally with specific string values
59 60 61 |
# File 'lib/puppet/pops/types/type_factory.rb', line 59 def self.string(size_type = nil, *values) Types::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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/puppet/pops/types/type_factory.rb', line 101 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?(Types::PAnyType) # TODO: Should have stricter name rule if key_type.is_a?(String) raise ArgumentError, 'Struct element key cannot be an empty String' if key_type.empty? key_type = string(nil, key_type) # Must make key optional if the value can be Undef key_type = optional(key_type) if tc.assignable?(value_type, Types::PUndefType::DEFAULT) else # assert that the key type is one of String[1], NotUndef[String[1]] and Optional[String[1]] case key_type when Types::PNotUndefType # We can loose the NotUndef wrapper here since String[1] isn't optional anyway key_type = key_type.type s = key_type when Types::POptionalType s = key_type.optional_type when Types::PStringType, Types::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?(Puppet::Pops::Types::PStringType) || s.is_a?(Puppet::Pops::Types::PEnumType)) && s.values.size == 1 && !s.values[0].empty? raise ArgumentError, "Unable to extract a non-empty literal string from Struct member key type #{tc.string(key_type)}" end end Types::PStructElement.new(key_type, value_type) end Types::PStructType.new(elements) end |
.tuple(types = [], size_type = nil) ⇒ Object
136 137 138 |
# File 'lib/puppet/pops/types/type_factory.rb', line 136 def self.tuple(types = [], size_type = nil) Types::PTupleType.new(types.map {|elem| type_of(elem) }, size_type) 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.
368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/puppet/pops/types/type_factory.rb', line 368 def self.type_of(o) if o.is_a?(Class) @type_calculator.type(o) elsif o.is_a?(Types::PAnyType) o elsif o.is_a?(String) Types::PRuntimeType.new(:ruby, o) else @type_calculator.infer_generic(o) end end |
.type_type(inst_type = nil) ⇒ Object
Produces a type for Type
360 361 362 |
# File 'lib/puppet/pops/types/type_factory.rb', line 360 def self.type_type(inst_type = nil) inst_type.nil? ? Types::PType::DEFAULT : Types::PType.new(inst_type) end |
.undef ⇒ Object
Creates an instance of the Undef type
268 269 270 |
# File 'lib/puppet/pops/types/type_factory.rb', line 268 def self.undef Types::PUndefType::DEFAULT end |
.variant(*types) ⇒ Object
Produces the Variant type, optionally with the “one of” types
86 87 88 |
# File 'lib/puppet/pops/types/type_factory.rb', line 86 def self.variant(*types) Types::PVariantType.new(types.map {|v| type_of(v) }) end |