Class: Puppet::Pops::Types::TypeCalculator
- Defined in:
- lib/puppet/pops/types/type_calculator.rb
Class Method Summary collapse
- .assignable?(t1, t2) ⇒ Boolean
-
.callable?(callable, args) ⇒ Boolean
Answers, does the given callable accept the arguments given in args (an array or a tuple).
- .generalize(o) ⇒ Object
- .infer(o) ⇒ Object
- .infer_set(o) ⇒ Object
-
.instance?(t, o) ⇒ Boolean
Answers ‘is o an instance of type t’.
- .is_kind_of_callable?(t, optional = true) ⇒ Boolean private
- .iterable(t) ⇒ Object
-
.singleton ⇒ TypeCalculator
private
The singleton instance.
Instance Method Summary collapse
-
#assignable?(t, t2) ⇒ Boolean
Answers ‘can an instance of type t2 be assigned to a variable of type t’.
-
#callable?(callable, args) ⇒ Boolean
Answers, does the given callable accept the arguments given in args (an array or a tuple).
-
#common_type(t1, t2) ⇒ Object
Answers, ‘What is the common type of t1 and t2?’.
-
#enumerable(t) ⇒ Object
Returns an iterable if the t represents something that can be iterated.
-
#equals(left, right) ⇒ Object
Answers if the two given types describe the same type.
-
#generalize(o) ⇒ Object
Generalizes value specific types.
-
#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).
-
#infer_and_reduce_type(enumerable) ⇒ Object
Reduce an enumerable of objects to a single common type.
- #infer_Array(o) ⇒ Object private
- #infer_Binary(o) ⇒ Object private
- #infer_Closure(o) ⇒ Object private
- #infer_FalseClass(o) ⇒ Object private
- #infer_Float(o) ⇒ Object private
- #infer_Function(o) ⇒ Object private
- #infer_generic(o) ⇒ Object
- #infer_Hash(o) ⇒ Object private
- #infer_Integer(o) ⇒ Object private
- #infer_Iterator(o) ⇒ Object private
-
#infer_Module(o) ⇒ Object
private
The type of all modules is PTypeType.
- #infer_NilClass(o) ⇒ Object private
- #infer_Object(o) ⇒ Object private
-
#infer_PAnyType(o) ⇒ Object
private
The type of all types is PTypeType.
- #infer_Proc(o) ⇒ Object private
-
#infer_PTypeType(o) ⇒ Object
private
The type of all types is PTypeType This is the metatype short circuit.
- #infer_PuppetProc(o) ⇒ Object private
- #infer_Regexp(o) ⇒ Object private
-
#infer_Resource(o) ⇒ Object
private
A Puppet::Parser::Resource, or Puppet::Resource.
- #infer_Sensitive(o) ⇒ Object private
-
#infer_set(o) ⇒ Object
Answers ‘what is the set of Puppet Types of o’.
- #infer_set_Array(o) ⇒ Object
- #infer_set_Hash(o) ⇒ Object
-
#infer_set_Object(o) ⇒ Object
Common case for everything that intrinsically only has a single type.
- #infer_set_Version(o) ⇒ Object private
- #infer_String(o) ⇒ Object private
-
#infer_Symbol(o) ⇒ Object
private
Inference of :default as PDefaultType, and all other are Ruby.
- #infer_Timespan(o) ⇒ Object private
- #infer_Timestamp(o) ⇒ Object private
- #infer_TrueClass(o) ⇒ Object private
- #infer_URI(o) ⇒ Object private
- #infer_Version(o) ⇒ Object private
- #infer_VersionRange(o) ⇒ Object private
-
#initialize ⇒ TypeCalculator
constructor
A new instance of TypeCalculator.
-
#instance?(t, o) ⇒ Boolean
Answers ‘is o an instance of type t’.
-
#is_pnil?(t) ⇒ Boolean
Answers if t represents the puppet type PUndefType.
-
#is_ptype?(t) ⇒ Boolean
Answers if t is a puppet type.
-
#iterable(t) ⇒ Object
Returns an iterable if the t represents something that can be iterated.
- #max(a, b) ⇒ Object
- #min(a, b) ⇒ Object
-
#reduce_type(enumerable) ⇒ Object
Reduces an enumerable of types to a single common type.
- #size_as_type(collection) ⇒ Object
-
#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.
-
#superclasses(c) ⇒ Object
Produces the superclasses of the given class, including the class.
-
#to_s ⇒ Object
Debugging to_s to reduce the amount of output.
-
#tuple_entry_at(tuple_t, from, to, index) ⇒ Object
private
Produces the tuple entry at the given index given a tuple type, its from/to constraints on the last type, and an index.
-
#type(c) ⇒ Object
Answers ‘what is the Puppet Type corresponding to the given Ruby class’.
- #unwrap_single_variant(possible_variant) ⇒ Object
Constructor Details
#initialize ⇒ TypeCalculator
Returns a new instance of TypeCalculator.
147 148 149 150 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 147 def initialize @@infer_visitor ||= Visitor.new(nil, 'infer',0,0) @@extract_visitor ||= Visitor.new(nil, 'extract',0,0) end |
Class Method Details
.assignable?(t1, t2) ⇒ 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) ⇒ Boolean
Answers, does the given callable accept the arguments given in args (an array or a tuple)
114 115 116 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 114 def self.callable?(callable, args) singleton.callable?(callable, args) end |
.generalize(o) ⇒ Object
124 125 126 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 124 def self.generalize(o) singleton.generalize(o) end |
.infer(o) ⇒ Object
119 120 121 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 119 def self.infer(o) singleton.infer(o) end |
.infer_set(o) ⇒ Object
129 130 131 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 129 def self.infer_set(o) singleton.infer_set(o) end |
.instance?(t, o) ⇒ Boolean
Answers ‘is o an instance of type t’
279 280 281 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 279 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.
747 748 749 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 747 def self.is_kind_of_callable?(t, optional = true) t.is_a?(PAnyType) && t.kind_of_callable?(optional) end |
.iterable(t) ⇒ Object
134 135 136 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 134 def self.iterable(t) singleton.iterable(t) end |
.singleton ⇒ TypeCalculator
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.
141 142 143 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 141 def self.singleton @tc_instance ||= new 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.
157 158 159 160 161 162 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 157 def assignable?(t, t2) if t.is_a?(Module) t = type(t) end t.is_a?(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)
179 180 181 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 179 def callable?(callable, args) callable.is_a?(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
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 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 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 313 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?(PUnitType) return t2 elsif t2.is_a?(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?(PArrayType) && t2.is_a?(PArrayType) return 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?(PHashType) && t2.is_a?(PHashType) key_type = common_type(t1.key_type, t2.key_type) value_type = common_type(t1.value_type, t2.value_type) return PHashType.new(key_type, value_type) end # when both are host-classes, reduce to PHostClass[] (since one was not assignable to the other) if t1.is_a?(PClassType) && t2.is_a?(PClassType) return PClassType::DEFAULT end # when both are resources, reduce to Resource[T] or Resource[] (since one was not assignable to the other) if t1.is_a?(PResourceType) && t2.is_a?(PResourceType) # only Resource[] unless the type name is the same return t1.type_name == t2.type_name ? PResourceType.new(t1.type_name, nil) : PResourceType::DEFAULT end # Integers have range, expand the range to the common range if t1.is_a?(PIntegerType) && t2.is_a?(PIntegerType) return 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?(PFloatType) && t2.is_a?(PFloatType) return PFloatType.new([t1.numeric_from, t2.numeric_from].min, [t1.numeric_to, t2.numeric_to].max) end if t1.is_a?(PStringType) && (t2.is_a?(PStringType) || t2.is_a?(PEnumType)) if(t2.is_a?(PEnumType)) return t1.value.nil? ? PEnumType::DEFAULT : PEnumType.new(t2.values | [t1.value]) end if t1.size_type.nil? || t2.size_type.nil? return t1.value.nil? || t2.value.nil? ? PStringType::DEFAULT : PEnumType.new([t1.value, t2.value]) end return PStringType.new(common_type(t1.size_type, t2.size_type)) end if t1.is_a?(PPatternType) && t2.is_a?(PPatternType) return PPatternType.new(t1.patterns | t2.patterns) end if t1.is_a?(PEnumType) && (t2.is_a?(PStringType) || t2.is_a?(PEnumType)) # The common type is one that complies with either set if t2.is_a?(PEnumType) return PEnumType.new(t1.values | t2.values) end return t2.value.nil? ? PEnumType::DEFAULT : PEnumType.new(t1.values | [t2.value]) end if t1.is_a?(PVariantType) && t2.is_a?(PVariantType) # The common type is one that complies with either set return PVariantType.maybe_create(t1.types | t2.types) end if t1.is_a?(PRegexpType) && t2.is_a?(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 PRegexpType::DEFAULT end if t1.is_a?(PCallableType) && t2.is_a?(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 PCallableType::DEFAULT end # Common abstract types, from most specific to most general if common_numeric?(t1, t2) return PNumericType::DEFAULT end if common_scalar_data?(t1, t2) return PScalarDataType::DEFAULT end if common_scalar?(t1, t2) return PScalarType::DEFAULT end if common_data?(t1,t2) return TypeFactory.data end # Meta types Type[Integer] + Type[String] => Type[Data] if t1.is_a?(PTypeType) && t2.is_a?(PTypeType) return PTypeType.new(common_type(t1.type, t2.type)) end if common_rich_data?(t1,t2) return TypeFactory.rich_data end # If both are Runtime types if t1.is_a?(PRuntimeType) && t2.is_a?(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 = ClassLoader.provide_from_type(t1) c2 = 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 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?(PAnyType) && t2.is_a?(PAnyType) ? PAnyType::DEFAULT : nil end |
#enumerable(t) ⇒ Object
Returns an iterable if the t represents something that can be iterated
165 166 167 168 169 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 165 def enumerable(t) #TRANSLATOR 'TypeCalculator.enumerable' and 'iterable' are methods and should not be translated Puppet.deprecation_warning(_('TypeCalculator.enumerable is deprecated. Use iterable')) iterable(t) end |
#equals(left, right) ⇒ Object
Answers if the two given types describe the same type
184 185 186 187 188 189 190 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 184 def equals(left, right) return false unless left.is_a?(PAnyType) && right.is_a?(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.
231 232 233 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 231 def generalize(o) o.is_a?(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).
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 239 def infer(o) # Optimize the most common cases into direct calls. # Explicit if/elsif/else is faster than case if o.is_a?(String) infer_String(o) elsif o.is_a?(Integer) # need subclasses for Ruby < 2.4 infer_Integer(o) elsif o.is_a?(Array) infer_Array(o) elsif o.is_a?(Hash) infer_Hash(o) elsif o.is_a?(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
484 485 486 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 484 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.
647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 647 def infer_Array(o) if o.instance_of?(Array) if o.empty? PArrayType::EMPTY else PArrayType.new(infer_and_reduce_type(o), size_as_type(o)) end else infer_Object(o) end end |
#infer_Binary(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.
660 661 662 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 660 def infer_Binary(o) PBinaryType::DEFAULT 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.
496 497 498 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 496 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.
626 627 628 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 626 def infer_FalseClass(o) PBooleanType::FALSE 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.
548 549 550 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 548 def infer_Float(o) 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.
506 507 508 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 506 def infer_Function(o) o.class.dispatcher.to_type end |
#infer_generic(o) ⇒ Object
257 258 259 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 257 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.
675 676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 675 def infer_Hash(o) if o.instance_of?(Hash) if o.empty? PHashType::EMPTY else ktype = infer_and_reduce_type(o.keys) etype = infer_and_reduce_type(o.values) PHashType.new(ktype, etype, size_as_type(o)) end else infer_Object(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.
553 554 555 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 553 def infer_Integer(o) PIntegerType.new(o, o) end |
#infer_Iterator(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.
501 502 503 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 501 def infer_Iterator(o) PIteratorType.new(o.element_type) 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 PTypeType
491 492 493 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 491 def infer_Module(o) PTypeType::new(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.
563 564 565 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 563 def infer_NilClass(o) 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.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 511 def infer_Object(o) if o.is_a?(PuppetObject) o._pcore_type else name = o.class.name return PRuntimeType.new(:ruby, nil) if name.nil? # anonymous class that doesn't implement PuppetObject is impossible to infer ir = Loaders.implementation_registry type = ir.nil? ? nil : ir.type_for_module(name) return PRuntimeType.new(:ruby, name) if type.nil? if type.is_a?(PObjectType) && type.parameterized? type = PObjectTypeExtension.create_from_instance(type, o) end type end 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 PTypeType
530 531 532 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 530 def infer_PAnyType(o) PTypeType.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.
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 569 def infer_Proc(o) min = 0 max = 0 mapped_types = o.parameters.map do |p| case p[0] when :rest max = :default break PAnyType::DEFAULT when :req min += 1 end max += 1 PAnyType::DEFAULT end param_types = Types::PTupleType.new(mapped_types, Types::PIntegerType.new(min, max)) Types::PCallableType.new(param_types) end |
#infer_PTypeType(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 PTypeType This is the metatype short circuit.
538 539 540 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 538 def infer_PTypeType(o) PTypeType.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.
588 589 590 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 588 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.
558 559 560 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 558 def infer_Regexp(o) PRegexpType.new(o) 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
638 639 640 641 642 643 644 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 638 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 PTypeType.new(PResourceType.new(o.type.to_s, title)) end |
#infer_Sensitive(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.
606 607 608 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 606 def infer_Sensitive(o) PSensitiveType.new(infer(o.unwrap)) end |
#infer_set(o) ⇒ Object
Answers ‘what is the set of Puppet Types of o’
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 264 def infer_set(o) if o.instance_of?(Array) infer_set_Array(o) elsif o.instance_of?(Hash) infer_set_Hash(o) elsif o.instance_of?(SemanticPuppet::Version) infer_set_Version(o) else infer(o) end end |
#infer_set_Array(o) ⇒ Object
699 700 701 702 703 704 705 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 699 def infer_set_Array(o) if o.empty? PArrayType::EMPTY else PTupleType.new(o.map {|x| infer_set(x) }) end end |
#infer_set_Hash(o) ⇒ Object
707 708 709 710 711 712 713 714 715 716 717 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 707 def infer_set_Hash(o) if o.empty? PHashType::EMPTY elsif o.keys.all? {|k| PStringType::NON_EMPTY.instance?(k) } PStructType.new(o.each_pair.map { |k,v| PStructElement.new(PStringType.new(k), infer_set(v)) }) else ktype = PVariantType.maybe_create(o.keys.map {|k| infer_set(k) }) etype = PVariantType.maybe_create(o.values.map {|e| infer_set(e) }) 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
695 696 697 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 695 def infer_set_Object(o) infer(o) end |
#infer_set_Version(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.
720 721 722 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 720 def infer_set_Version(o) PSemVerType.new([SemanticPuppet::VersionRange.new(o, 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.
543 544 545 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 543 def infer_String(o) PStringType.new(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
594 595 596 597 598 599 600 601 602 603 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 594 def infer_Symbol(o) case o when :default PDefaultType::DEFAULT when :undef PUndefType::DEFAULT else infer_Object(o) end end |
#infer_Timespan(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.
611 612 613 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 611 def infer_Timespan(o) PTimespanType.new(o, o) end |
#infer_Timestamp(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.
616 617 618 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 616 def infer_Timestamp(o) PTimestampType.new(o, o) 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.
621 622 623 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 621 def infer_TrueClass(o) PBooleanType::TRUE end |
#infer_URI(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.
631 632 633 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 631 def infer_URI(o) PURIType.new(o) end |
#infer_Version(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.
665 666 667 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 665 def infer_Version(o) PSemVerType::DEFAULT end |
#infer_VersionRange(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.
670 671 672 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 670 def infer_VersionRange(o) PSemVerRangeType::DEFAULT end |
#instance?(t, o) ⇒ Boolean
Answers ‘is o an instance of type t’
286 287 288 289 290 291 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 286 def instance?(t, o) if t.is_a?(Module) t = type(t) end t.is_a?(PAnyType) ? t.instance?(o) : false end |
#is_pnil?(t) ⇒ Boolean
Answers if t represents the puppet type PUndefType
303 304 305 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 303 def is_pnil?(t) t.nil? || t.is_a?(PUndefType) end |
#is_ptype?(t) ⇒ Boolean
Answers if t is a puppet type
296 297 298 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 296 def is_ptype?(t) t.is_a?(PAnyType) end |
#iterable(t) ⇒ Object
Returns an iterable if the t represents something that can be iterated
172 173 174 175 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 172 def iterable(t) # Create an iterable on the type if possible Iterable.on(t) end |
#max(a, b) ⇒ Object
751 752 753 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 751 def max(a,b) a >=b ? a : b end |
#min(a, b) ⇒ Object
755 756 757 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 755 def min(a,b) a <= b ? a : b end |
#reduce_type(enumerable) ⇒ Object
Reduces an enumerable of types to a single common type.
477 478 479 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 477 def reduce_type(enumerable) enumerable.reduce(nil) {|memo, t| common_type(memo, t) } end |
#size_as_type(collection) ⇒ Object
689 690 691 692 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 689 def size_as_type(collection) size = collection.size 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
737 738 739 740 741 742 743 744 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 737 def size_range(range) return [1,1] if range.nil? from = range.from to = range.to x = from.nil? ? 1 : from y = to.nil? ? TheInfinity : to [x, y] end |
#superclasses(c) ⇒ Object
Produces the superclasses of the given class, including the class
465 466 467 468 469 470 471 472 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 465 def superclasses(c) result = [c] while s = c.superclass result << s c = s end result end |
#to_s ⇒ Object
Debugging to_s to reduce the amount of output
779 780 781 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 779 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
766 767 768 769 770 771 772 773 774 775 776 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 766 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’
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 196 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 = PIntegerType::DEFAULT when c == Float type = PFloatType::DEFAULT when c == Numeric type = PNumericType::DEFAULT when c == String type = PStringType::DEFAULT when c == Regexp type = PRegexpType::DEFAULT when c == NilClass type = PUndefType::DEFAULT when c == FalseClass, c == TrueClass type = PBooleanType::DEFAULT when c == Class type = PTypeType::DEFAULT when c == Array # Assume array of any type = PArrayType::DEFAULT when c == Hash # Assume hash of any type = PHashType::DEFAULT else type = PRuntimeType.new(:ruby, c.name) end type end |
#unwrap_single_variant(possible_variant) ⇒ Object
724 725 726 727 728 729 730 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 724 def unwrap_single_variant(possible_variant) if possible_variant.is_a?(PVariantType) && possible_variant.types.size == 1 possible_variant.types[0] else possible_variant end end |