Class: Puppet::Pops::Types::TypeCalculator
- Defined in:
- lib/puppet/pops/types/type_calculator.rb
Overview
In general, new instances of the wanted type should be created as they are assigned to models using containment, and a contained object can only be in one container at a time. Also, the type system may include more details in each type instance, such as if it may be nil, be empty, contain a certain count etc. Or put differently, the puppet types are not singletons.
The TypeCalculator can answer questions about puppet types.
The Puppet type system is primarily based on sub-classing. When asking the type calculator to infer types from Ruby in general, it may not provide the wanted answer; it does not for instance take module inclusions and extensions into account. In general the type system should be unsurprising for anyone being exposed to the notion of type. The type Data may require a bit more explanation; this is an abstract type that includes all scalar types, as well as Array with an element type compatible with Data, and Hash with key compatible with scalar and elements compatible with Data. Expressed differently; Data is what you typically express using JSON (with the exception that the Puppet type system also includes Pattern (regular expression) as a scalar.
Inference
The ‘infer(o)` method infers a Puppet type for scalar Ruby objects, and for Arrays and Hashes. The inference result is instance specific for single typed collections and allows answering questions about its embedded type. It does not however preserve multiple types in a collection, and can thus not answer questions like `[1,a].infer() =~ Array[Integer, String]` since the inference computes the common type Scalar when combining Integer and String.
The ‘infer_generic(o)` method infers a generic Puppet type for scalar Ruby object, Arrays and Hashes. This inference result does not contain instance specific information; e.g. Array where the integer range is the generic default. Just infer it also combines types into a common type.
The ‘infer_set(o)` method works like infer but preserves all type information. It does not do any reduction into common types or ranges. This method of inference is best suited for answering questions about an object being an instance of a type. It correctly answers: `[1,a].infer_set() =~ Array[Integer, String]`
The ‘generalize!(t)` method modifies an instance specific inference result to a generic. The method mutates the given argument. Basically, this removes string instances from String, and range from Integer and Float.
Assignability
The ‘assignable?(t1, t2)` method answers if t2 conforms to t1. The type t2 may be an instance, in which case its type is inferred, or a type.
Instance?
The ‘instance?(t, o)` method answers if the given object (instance) is an instance that is assignable to the given type.
String
Creates a string representation of a type.
Creation of Type instances
Instance of the classes in the type model are used to denote a specific type. It is most convenient to use the TypeFactory when creating instances.
All types support copy which should be used when assigning a type where it is unknown if it is bound or not to a parent type. A check can be made with ‘t.eContainer().nil?`
Equality and Hash
Type instances are equal in terms of Ruby eql? and ‘==` if they describe the same type, but they are not equal? if they are not the same type instance. Two types that describe the same type have identical hash - this makes them usable as hash keys.
Types and Subclasses
In general, the type calculator should be used to answer questions if a type is a subtype of another (using #assignable?, or #instance? if the question is if a given object is an instance of a given type (or is a subtype thereof). Many of the types also have a Ruby subtype relationship; e.g. PHashType and PArrayType are both subtypes of PCollectionType, and PIntegerType, PFloatType, PStringType,… are subtypes of PScalarType. Even if it is possible to answer certain questions about type by looking at the Ruby class of the types this is considered an implementation detail, and such checks should in general be performed by the type_calculator which implements the type system semantics.
The PRubyType
The PRubyType corresponds to a Ruby Class, except for the puppet types that are specialized (i.e. PRubyType should not be used for Integer, String, etc. since there are specialized types for those). When the type calculator deals with PRubyTypes and checks for assignability, it determines the “common ancestor class” of two classes. This check is made based on the superclasses of the two classes being compared. In order to perform this, the classes must be present (i.e. they are resolved from the string form in the PRubyType to a loaded, instantiated Ruby Class). In general this is not a problem, since the question to produce the common super type for two objects means that the classes must be present or there would have been no instances present in the first place. If however the classes are not present, the type calculator will fall back and state that the two types at least have Object in common.
Using the Type Calculator
The type calculator can be directly used via its class methods. If doing time critical work and doing many calls to the type calculator, it is more performant to create an instance and invoke the corresponding instance methods. Note that inference is an expensive operation, rather than infering the same thing several times, it is in general better to infer once and then copy the result if mutation to a more generic form is required.
Constant Summary collapse
- Types =
Puppet::Pops::Types
- TheInfinity =
because the Infinity symbol is not defined
1.0 / 0.0
Class Method Summary collapse
- .assignable?(t1, t2) ⇒ Boolean
-
.callable?(callable, args) ⇒ Boolan
Answers, does the given callable accept the arguments given in args (an array or a tuple).
- .copy_as_tuple(t) ⇒ Object
- .data_variant ⇒ Object
- .debug_string(t) ⇒ Object
- .enumerable(t) ⇒ Object
- .generalize!(o) ⇒ Object
- .infer(o) ⇒ Object
- .infer_set(o) ⇒ Object
-
.instance?(t, o) ⇒ Boolean
Answers ‘is o an instance of type t’.
- .singleton ⇒ Object private
-
.string(t) ⇒ String
Produces a String representation of the given type.
Instance Method Summary collapse
-
#assignable?(t, t2) ⇒ Boolean
Answers ‘can an instance of type t2 be assigned to a variable of type t’.
-
#assignable_Object(t, t2) ⇒ Object
private
False in general type calculator.
-
#assignable_PArrayType(t, t2) ⇒ Object
private
Array is assignable if t2 is an Array and t2’s element type is assignable, or if t2 is a Tuple where.
- #assignable_PBooleanType(t, t2) ⇒ Object private
- #assignable_PCallableType(t, t2) ⇒ Object private
- #assignable_PCatalogEntryType(t1, t2) ⇒ Object private
- #assignable_PCollectionType(t, t2) ⇒ Object private
-
#assignable_PDataType(t, t2) ⇒ Object
private
Data is assignable by other Data and by Array and Hash[Scalar, Data].
- #assignable_PEnumType(t, t2) ⇒ Object private
- #assignable_PFloatType(t, t2) ⇒ Object private
-
#assignable_PHashType(t, t2) ⇒ Object
private
Hash is assignable if t2 is a Hash and t2’s key and element types are assignable.
- #assignable_PHostClassType(t1, t2) ⇒ Object private
- #assignable_PIntegerType(t, t2) ⇒ Object private
- #assignable_PNilType(t, t2) ⇒ Object private
- #assignable_PNumericType(t, t2) ⇒ Object private
- #assignable_PObjectType(t, t2) ⇒ Object private
- #assignable_POptionalType(t, t2) ⇒ Object private
- #assignable_PPatternType(t, t2) ⇒ Object private
- #assignable_PRegexpType(t, t2) ⇒ Object private
- #assignable_PResourceType(t1, t2) ⇒ Object private
-
#assignable_PRubyType(t1, t2) ⇒ Object
private
Assignable if t2’s ruby class is same or subclass of t1’s ruby class.
- #assignable_PScalarType(t, t2) ⇒ Object private
- #assignable_PStringType(t, t2) ⇒ Object private
- #assignable_PStructType(t, t2) ⇒ Object private
- #assignable_PTupleType(t, t2) ⇒ Object
- #assignable_PType(t, t2) ⇒ Object private
- #assignable_PVariantType(t, t2) ⇒ Object private
-
#callable?(callable, args) ⇒ Boolean
Answers, does the given callable accept the arguments given in args (an array or a tuple).
-
#callable_Object(o, callable_t) ⇒ Object
Catch all not callable combinations.
- #callable_PArrayType(args_array, callable_t) ⇒ Object
- #callable_PTupleType(args_tuple, callable_t) ⇒ Object
-
#common_type(t1, t2) ⇒ Object
Answers, ‘What is the common type of t1 and t2?’.
-
#data ⇒ Object
Convenience method to get a data type for comparisons.
-
#data_variant ⇒ Object
Convenience method to get a variant compatible with the Data type.
-
#debug_string(t) ⇒ Object
Produces a debug string representing the type (possibly with more information that the regular string format).
- #debug_string_Object(t) ⇒ Object private
- #debug_string_PStringType(t) ⇒ Object private
-
#enumerable(t) ⇒ Object
Returns an enumerable if the t represents something that can be iterated.
-
#enumerable_Object(o) ⇒ Object
private
Catches all non enumerable types.
- #enumerable_PIntegerType(t) ⇒ Object private
-
#equals(left, right) ⇒ Object
Answers if the two given types describe the same type.
- #from_to_ordered(from, to) ⇒ Object private
-
#generalize!(o) ⇒ Object
Generalizes value specific types.
- #generalize_Object(o) ⇒ Object
- #generalize_PCollectionType(o) ⇒ Object
- #generalize_PFloatType(o) ⇒ Object
- #generalize_PIntegerType(o) ⇒ Object
- #generalize_PStringType(o) ⇒ Object
-
#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_Class(o) ⇒ Object
private
The type of all classes is PType.
- #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_NilClass(o) ⇒ Object private
- #infer_Object(o) ⇒ Object private
-
#infer_PAbstractType(o) ⇒ Object
private
The type of all types is PType.
-
#infer_PType(o) ⇒ Object
private
The type of all types is PType This is the metatype short circuit.
- #infer_Regexp(o) ⇒ Object private
-
#infer_Resource(o) ⇒ Object
private
A Puppet::Parser::Resource, or Puppet::Resource.
-
#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_String(o) ⇒ Object private
-
#infer_Symbol(o) ⇒ Object
private
Inference of :undef as PNilType, all other are Ruby.
- #infer_TrueClass(o) ⇒ Object private
-
#initialize ⇒ TypeCalculator
constructor
A new instance of TypeCalculator.
-
#injectable_class(klazz) ⇒ Class?
Answers the question ‘is it possible to inject an instance of the given class’ A class is injectable if it has a special *assisted inject* class method called
injecttaking an injector and a scope as argument, or if it has a zero argsinitializemethod. -
#instance?(t, o) ⇒ Boolean
Answers ‘is o an instance of type t’.
- #instance_of(t, o) ⇒ Object
- #instance_of_Object(t, o) ⇒ Object
- #instance_of_PArrayType(t, o) ⇒ Object
- #instance_of_PDataType(t, o) ⇒ Object
- #instance_of_PHashType(t, o) ⇒ Object
- #instance_of_PNilType(t, o) ⇒ Object
- #instance_of_POptionalType(t, o) ⇒ Object
- #instance_of_PStructType(t, o) ⇒ Object
- #instance_of_PTupleType(t, o) ⇒ Object
- #instance_of_PVariantType(t, o) ⇒ Object
-
#is_pnil?(t) ⇒ Boolean
Answers if t represents the puppet type PNilType.
-
#is_ptype?(t) ⇒ Boolean
Answers if t is a puppet type.
- #max(a, b) ⇒ Object
- #min(a, b) ⇒ Object
-
#range_array_part(t) ⇒ Object
private
Produces a string from an Integer range type that is used inside other type strings.
-
#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.
-
#string(t) ⇒ Object
Produces a string representing the type.
- #string_NilClass(t) ⇒ Object private
- #string_PArrayType(t) ⇒ Object private
- #string_PBooleanType(t) ⇒ Object private
- #string_PCallableType(t) ⇒ Object private
- #string_PCatalogEntryType(t) ⇒ Object private
- #string_PCollectionType(t) ⇒ Object private
- #string_PDataType(t) ⇒ Object private
- #string_PEnumType(t) ⇒ Object private
- #string_PFloatType(t) ⇒ Object private
- #string_PHashType(t) ⇒ Object private
- #string_PHostClassType(t) ⇒ Object private
- #string_PIntegerType(t) ⇒ Object private
- #string_PNilType(t) ⇒ Object private
- #string_PNumericType(t) ⇒ Object private
- #string_PObjectType(t) ⇒ Object private
- #string_POptionalType(t) ⇒ Object
- #string_PPatternType(t) ⇒ Object private
- #string_PRegexpType(t) ⇒ Object private
- #string_PResourceType(t) ⇒ Object private
- #string_PRubyType(t) ⇒ Object private
- #string_PScalarType(t) ⇒ Object private
- #string_PStringType(t) ⇒ Object private
- #string_PStructElement(t) ⇒ Object
- #string_PStructType(t) ⇒ Object private
- #string_PTupleType(t) ⇒ Object private
- #string_PType(t) ⇒ Object private
- #string_PVariantType(t) ⇒ Object private
- #string_String(t) ⇒ Object private
-
#superclasses(c) ⇒ Object
Produces the superclasses of the given class, including the class.
-
#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.
157 158 159 160 161 162 163 164 165 166 167 168 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 157 def initialize @@assignable_visitor ||= Puppet::Pops::Visitor.new(nil,"assignable",1,1) @@infer_visitor ||= Puppet::Pops::Visitor.new(nil,"infer",0,0) @@infer_set_visitor ||= Puppet::Pops::Visitor.new(nil,"infer_set",0,0) @@instance_of_visitor ||= Puppet::Pops::Visitor.new(nil,"instance_of",1,1) @@string_visitor ||= Puppet::Pops::Visitor.new(nil,"string",0,0) @@inspect_visitor ||= Puppet::Pops::Visitor.new(nil,"debug_string",0,0) @@enumerable_visitor ||= Puppet::Pops::Visitor.new(nil,"enumerable",0,0) @@extract_visitor ||= Puppet::Pops::Visitor.new(nil,"extract",0,0) @@generalize_visitor ||= Puppet::Pops::Visitor.new(nil,"generalize",0,0) @@callable_visitor ||= Puppet::Pops::Visitor.new(nil,"callable",1,1) da = Types::PArrayType.new() da.element_type = Types::PDataType.new() @data_array = da h = Types::PHashType.new() h.element_type = Types::PDataType.new() h.key_type = Types::PScalarType.new() @data_hash = h @data_t = Types::PDataType.new() @scalar_t = Types::PScalarType.new() @numeric_t = Types::PNumericType.new() @t = Types::PObjectType.new() # Data accepts a Tuple that has 0-infinity Data compatible entries (e.g. a Tuple equivalent to Array). data_tuple = Types::PTupleType.new() data_tuple.addTypes(Types::PDataType.new()) data_tuple.size_type = Types::PIntegerType.new() data_tuple.size_type.from = 0 data_tuple.size_type.to = nil # infinity @data_tuple_t = data_tuple # Variant type compatible with Data data_variant = Types::PVariantType.new() data_variant.addTypes(@data_hash.copy) data_variant.addTypes(@data_array.copy) data_variant.addTypes(Types::PScalarType.new) data_variant.addTypes(Types::PNilType.new) data_variant.addTypes(@data_tuple_t.copy) @data_variant_t = data_variant collection_default_size = Types::PIntegerType.new() collection_default_size.from = 0 collection_default_size.to = nil # infinity @collection_default_size_t = collection_default_size non_empty_string = Types::PStringType.new non_empty_string.size_type = Types::PIntegerType.new() non_empty_string.size_type.from = 1 non_empty_string.size_type.to = nil # infinity @non_empty_string_t = non_empty_string @nil_t = Types::PNilType.new end |
Class Method Details
.assignable?(t1, t2) ⇒ Boolean
101 102 103 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 101 def self.assignable?(t1, t2) singleton.assignable?(t1,t2) end |
.callable?(callable, args) ⇒ Boolan
Answers, does the given callable accept the arguments given in args (an array or a tuple)
111 112 113 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 111 def self.callable?(callable, args) singleton.callable?(callable, args) end |
.copy_as_tuple(t) ⇒ Object
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1555 def self.copy_as_tuple(t) case t when Types::PTupleType t.copy when Types::PArrayType # transform array to tuple result = Types::PTupleType.new result.addTypes(t.element_type.copy) result.size_type = t.size_type.nil? ? nil : t.size_type.copy result else raise ArgumentError, "Internal Error: Only Array and Tuple can be given to copy_as_tuple" end end |
.data_variant ⇒ Object
228 229 230 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 228 def self.data_variant singleton.data_variant end |
.debug_string(t) ⇒ Object
141 142 143 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 141 def self.debug_string(t) singleton.debug_string(t) end |
.enumerable(t) ⇒ Object
146 147 148 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 146 def self.enumerable(t) singleton.enumerable(t) end |
.generalize!(o) ⇒ Object
131 132 133 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 131 def self.generalize!(o) singleton.generalize!(o) end |
.infer(o) ⇒ Object
126 127 128 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 126 def self.infer(o) singleton.infer(o) end |
.infer_set(o) ⇒ Object
136 137 138 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 136 def self.infer_set(o) singleton.infer_set(o) end |
.instance?(t, o) ⇒ Boolean
Answers ‘is o an instance of type t’
459 460 461 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 459 def self.instance?(t, o) singleton.instance_of(t,o) end |
.singleton ⇒ 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.
151 152 153 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 151 def self.singleton() @tc_instance ||= new end |
.string(t) ⇒ String
Produces a String representation of the given type.
121 122 123 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 121 def self.string(t) singleton.string(t) 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.
260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 260 def assignable?(t, t2) if t.is_a?(Class) t = type(t) end if t2.is_a?(Class) t2 = type(t2) end @@assignable_visitor.visit_this_1(self, t, t2) end |
#assignable_Object(t, t2) ⇒ 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.
False in general type calculator
873 874 875 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 873 def assignable_Object(t, t2) false end |
#assignable_PArrayType(t, t2) ⇒ 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.
Array is assignable if t2 is an Array and t2’s element type is assignable, or if t2 is a Tuple where
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1223 def assignable_PArrayType(t, t2) if t2.is_a?(Types::PArrayType) return false unless assignable?(t.element_type, t2.element_type) assignable_PCollectionType(t, t2) elsif t2.is_a?(Types::PTupleType) return false unless t2.types.all? {|t2_element| assignable?(t.element_type, t2_element) } t2_regular = t2.types[0..-2] t2_ranged = t2.types[-1] t2_from, t2_to = size_range(t2.size_type) t2_required = t2_regular.size + t2_from t_entry = t.element_type # Tuple of anything can not be assigned (unless array is tuple of anything) - this case # was handled at the top of this method. # return false if t_entry.nil? # array type may be size constrained size_t = t.size_type || @collection_default_size_t min, max = size_t.range # Tuple with fewer min entries can not be assigned return false if t2_required < min # Tuple with more optionally available entries can not be assigned return false if t2_regular.size + t2_to > max # each tuple type must be assignable to the element type t2_required.times do |index| t2_entry = tuple_entry_at(t2, t2_from, t2_to, index) return false unless assignable?(t_entry, t2_entry) end # ... and so must the last, possibly optional (ranged) type return assignable?(t_entry, t2_ranged) else false end end |
#assignable_PBooleanType(t, t2) ⇒ 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.
1165 1166 1167 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1165 def assignable_PBooleanType(t, t2) t2.is_a?(Types::PBooleanType) end |
#assignable_PCallableType(t, t2) ⇒ 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.
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1175 def assignable_PCallableType(t, t2) return false unless t2.is_a?(Types::PCallableType) # nil param_types means, any other Callable is assignable return true if t.param_types.nil? return false unless assignable?(t.param_types, t2.param_types) # names are ignored, they are just information # Blocks must be compatible this_block_t = t.block_type || @nil_t that_block_t = t2.block_type || @nil_t assignable?(this_block_t, that_block_t) end |
#assignable_PCatalogEntryType(t1, t2) ⇒ 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.
1285 1286 1287 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1285 def assignable_PCatalogEntryType(t1, t2) t2.is_a?(Types::PCatalogEntryType) end |
#assignable_PCollectionType(t, t2) ⇒ 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.
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1188 def assignable_PCollectionType(t, t2) size_t = t.size_type || @collection_default_size_t case t2 when Types::PCollectionType size_t2 = t2.size_type || @collection_default_size_t assignable?(size_t, size_t2) when Types::PTupleType # compute the tuple's min/max size, and check if that size matches from, to = size_range(t2.size_type) t2s = Types::PIntegerType.new() t2s.from = t2.types.size - 1 + from t2s.to = t2.types.size - 1 + to assignable?(size_t, t2s) when Types::PStructType from = to = t2.elements.size t2s = Types::PIntegerType.new() t2s.from = from t2s.to = to assignable?(size_t, t2s) else false end end |
#assignable_PDataType(t, t2) ⇒ 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.
Data is assignable by other Data and by Array and Hash[Scalar, Data]
1309 1310 1311 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1309 def assignable_PDataType(t, t2) t2.is_a?(Types::PDataType) || assignable?(@data_variant_t, t2) end |
#assignable_PEnumType(t, t2) ⇒ 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.
1086 1087 1088 1089 1090 1091 1092 1093 1094 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1086 def assignable_PEnumType(t, t2) return true if t == t2 || (t.values.empty? && (t2.is_a?(Types::PStringType) || t2.is_a?(Types::PEnumType))) if t2.is_a?(Types::PStringType) # if the set of strings are all found in the set of enums t2.values.all? { |s| t.values.any? { |e| e == s }} else false end end |
#assignable_PFloatType(t, t2) ⇒ 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.
1156 1157 1158 1159 1160 1161 1162 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1156 def assignable_PFloatType(t, t2) return false unless t2.is_a?(Types::PFloatType) trange = from_to_ordered(t.from, t.to) t2range = from_to_ordered(t2.from, t2.to) # If t2 min and max are within the range of t trange[0] <= t2range[0] && trange[1] >= t2range[1] end |
#assignable_PHashType(t, t2) ⇒ 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.
Hash is assignable if t2 is a Hash and t2’s key and element types are assignable
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1263 def assignable_PHashType(t, t2) case t2 when Types::PHashType return false unless assignable?(t.key_type, t2.key_type) && assignable?(t.element_type, t2.element_type) assignable_PCollectionType(t, t2) when Types::PStructType # hash must accept String as key type # hash must accept all value types # hash must accept the size of the struct size_t = t.size_type || @collection_default_size_t min, max = size_t.range struct_size = t2.elements.size element_type = t.element_type ( struct_size >= min && struct_size <= max && assignable?(t.key_type, @non_empty_string_t) && t2.hashed_elements.all? {|k,v| assignable?(element_type, v) }) else false end end |
#assignable_PHostClassType(t1, t2) ⇒ 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.
1290 1291 1292 1293 1294 1295 1296 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1290 def assignable_PHostClassType(t1, t2) return false unless t2.is_a?(Types::PHostClassType) # Class = Class[name}, Class[name] != Class return true if t1.class_name.nil? # Class[name] = Class[name] return t1.class_name == t2.class_name end |
#assignable_PIntegerType(t, t2) ⇒ 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.
899 900 901 902 903 904 905 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 899 def assignable_PIntegerType(t, t2) return false unless t2.is_a?(Types::PIntegerType) trange = from_to_ordered(t.from, t.to) t2range = from_to_ordered(t2.from, t2.to) # If t2 min and max are within the range of t trange[0] <= t2range[0] && trange[1] >= t2range[1] end |
#assignable_PNilType(t, t2) ⇒ 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.
883 884 885 886 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 883 def assignable_PNilType(t, t2) # Only undef/nil is assignable to nil type t2.is_a?(Types::PNilType) end |
#assignable_PNumericType(t, t2) ⇒ 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.
894 895 896 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 894 def assignable_PNumericType(t, t2) t2.is_a?(Types::PNumericType) end |
#assignable_PObjectType(t, t2) ⇒ 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.
878 879 880 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 878 def assignable_PObjectType(t, t2) t2.is_a?(Types::PObjectType) end |
#assignable_POptionalType(t, t2) ⇒ 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.
1076 1077 1078 1079 1080 1081 1082 1083 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1076 def assignable_POptionalType(t, t2) return true if t2.is_a?(Types::PNilType) if t2.is_a?(Types::POptionalType) assignable?(t.optional_type, t2.optional_type) else assignable?(t.optional_type, t2) end end |
#assignable_PPatternType(t, t2) ⇒ 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.
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1141 def assignable_PPatternType(t, t2) return true if t == t2 return false unless t2.is_a?(Types::PStringType) || t2.is_a?(Types::PEnumType) if t2.values.empty? # Strings / Enums (unknown which ones) cannot all match a pattern, but if there is no pattern it is ok # (There should really always be a pattern, but better safe than sorry). return t.patterns.empty? ? true : false end # all strings in String/Enum type must match one of the patterns in Pattern type regexps = t.patterns.map {|p| p.regexp } t2.values.all? { |v| regexps.any? {|re| re.match(v) } } end |
#assignable_PRegexpType(t, t2) ⇒ 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.
1170 1171 1172 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1170 def assignable_PRegexpType(t, t2) t2.is_a?(Types::PRegexpType) && (t.pattern.nil? || t.pattern == t2.pattern) end |
#assignable_PResourceType(t1, t2) ⇒ 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.
1299 1300 1301 1302 1303 1304 1305 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1299 def assignable_PResourceType(t1, t2) return false unless t2.is_a?(Types::PResourceType) return true if t1.type_name.nil? return false if t1.type_name != t2.type_name return true if t1.title.nil? return t1.title == t2.title end |
#assignable_PRubyType(t1, t2) ⇒ 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.
Assignable if t2’s ruby class is same or subclass of t1’s ruby class
1315 1316 1317 1318 1319 1320 1321 1322 1323 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1315 def assignable_PRubyType(t1, t2) return false unless t2.is_a?(Types::PRubyType) return true if t1.ruby_class.nil? # t1 is wider return false if t2.ruby_class.nil? # t1 not nil, so t2 can not be wider c1 = class_from_string(t1.ruby_class) c2 = class_from_string(t2.ruby_class) return false unless c1.is_a?(Class) && c2.is_a?(Class) !!(c2 <= c1) end |
#assignable_PScalarType(t, t2) ⇒ 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.
889 890 891 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 889 def assignable_PScalarType(t, t2) t2.is_a?(Types::PScalarType) end |
#assignable_PStringType(t, t2) ⇒ 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.
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1097 def assignable_PStringType(t, t2) if t.values.empty? # A general string is assignable by any other string or pattern restricted string # if the string has a size constraint it does not match since there is no reasonable way # to compute the min/max length a pattern will match. For enum, it is possible to test that # each enumerator value is within range size_t = t.size_type || @collection_default_size_t case t2 when Types::PStringType # true if size compliant size_t2 = t2.size_type || @collection_default_size_t assignable?(size_t, size_t2) when Types::PPatternType # true if size constraint is at least 0 to +Infinity (which is the same as the default) assignable?(size_t, @collection_default_size_t) when Types::PEnumType if t2.values # true if all enum values are within range min, max = t2.values.map(&:size).minmax trange = from_to_ordered(size_t.from, size_t.to) t2range = [min, max] # If t2 min and max are within the range of t trange[0] <= t2range[0] && trange[1] >= t2range[1] else # no string can match this enum anyway since it does not accept anything false end else # no other type matches string false end elsif t2.is_a?(Types::PStringType) # A specific string acts as a set of strings - must have exactly the same strings # In this case, size does not matter since the definition is very precise anyway Set.new(t.values) == Set.new(t2.values) else # All others are false, since no other type describes the same set of specific strings false end end |
#assignable_PStructType(t, t2) ⇒ 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.
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1053 def assignable_PStructType(t, t2) return true if t == t2 || t.elements.empty? && (t2.is_a?(Types::PHashType)) h = t.hashed_elements if t2.is_a?(Types::PStructType) h2 = t2.hashed_elements h.size == h2.size && h.all? {|k, v| assignable?(v, h2[k]) } elsif t2.is_a?(Types::PHashType) size_t2 = t2.size_type || @collection_default_size_t size_t = Types::PIntegerType.new size_t.from = size_t.to = h.size # compatible size # hash key type must be string of min 1 size # hash value t must be assignable to each key element_type = t2.element_type assignable?(size_t, size_t2) && assignable?(@non_empty_string_t, t2.key_type) && h.all? {|k,v| assignable?(v, element_type) } else false end end |
#assignable_PTupleType(t, t2) ⇒ Object
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 999 def assignable_PTupleType(t, t2) return true if t == t2 || t.types.empty? && (t2.is_a?(Types::PArrayType)) size_t = t.size_type || Puppet::Pops::Types::TypeFactory.range(*t.size_range) if t2.is_a?(Types::PTupleType) size_t2 = t2.size_type || Puppet::Pops::Types::TypeFactory.range(*t2.size_range) # not assignable if the number of types in t2 is outside number of types in t1 return false unless assignable?(size_t, size_t2) max(t.types.size, t2.types.size).times do |index| return false unless assignable?((t.types[index] || t.types[-1]), (t2.types[index] || t2.types[-1])) end true elsif t2.is_a?(Types::PArrayType) t2_entry = t2.element_type # Array of anything can not be assigned (unless tuple is tuple of anything) - this case # was handled at the top of this method. # return false if t2_entry.nil? size_t = t.size_type || Puppet::Pops::Types::TypeFactory.range(*t.size_range) size_t2 = t2.size_type || @collection_default_size_t return false unless assignable?(size_t, size_t2) min(t.types.size, size_t2.range()[1]).times do |index| return false unless assignable?((t.types[index] || t.types[-1]), t2_entry) end true else false end end |
#assignable_PType(t, t2) ⇒ 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.
1213 1214 1215 1216 1217 1218 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1213 def assignable_PType(t, t2) return false unless t2.is_a?(Types::PType) return true if t.type.nil? # wide enough to handle all types return false if t2.type.nil? # wider than t assignable?(t.type, t2.type) end |
#assignable_PVariantType(t, t2) ⇒ 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.
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 937 def assignable_PVariantType(t, t2) # Data is a specific variant t2 = @data_variant_t if t2.is_a?(Types::PDataType) if t2.is_a?(Types::PVariantType) # A variant is assignable if all of its options are assignable to one of this type's options return true if t == t2 t2.types.all? do |other| # if the other is a Variant, all if its options, but be assignable to one of this type's options other = other.is_a?(Types::PDataType) ? @data_variant_t : other if other.is_a?(Types::PVariantType) assignable?(t, other) else t.types.any? {|option_t| assignable?(option_t, other) } end end else # A variant is assignable if t2 is assignable to any of its types t.types.any? { |option_t| assignable?(option_t, t2) } end end |
#callable?(callable, args) ⇒ Boolean
Answers, does the given callable accept the arguments given in args (an array or a tuple)
279 280 281 282 283 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 279 def callable?(callable, args) return false if !callable.is_a?(Types::PCallableType) # Note that polymorphism is for the args type, the callable is always a callable @@callable_visitor.visit_this_1(self, args, callable) end |
#callable_Object(o, callable_t) ⇒ Object
Catch all not callable combinations
959 960 961 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 959 def callable_Object(o, callable_t) false end |
#callable_PArrayType(args_array, callable_t) ⇒ Object
985 986 987 988 989 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 985 def callable_PArrayType(args_array, callable_t) return false unless assignable?(callable_t.param_types, args_array) # does not support calling with a block, but have to check that callable expects it assignable?(callable_t.block_type || @nil_t, @nil_t) end |
#callable_PTupleType(args_tuple, callable_t) ⇒ Object
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 963 def callable_PTupleType(args_tuple, callable_t) if args_tuple.size_type raise ArgumentError, "Callable tuple may not have a size constraint when used as args" end # Assume no block was given - i.e. it is nil, and its type is PNilType block_t = @nil_t if args_tuple.types.last.is_a?(Types::PCallableType) # a split is needed to make it possible to use required, optional, and varargs semantics # of the tuple type. # args_tuple = args_tuple.copy # to drop the callable, it must be removed explicitly since this is an rgen array args_tuple.removeTypes(block_t = args_tuple.types.last()) else # no block was given, if it is required, the below will fail end # unless argument types match parameter types return false unless assignable?(callable_t.param_types, args_tuple) # unless given block (or no block) matches expected block (or no block) assignable?(callable_t.block_type || @nil_t, block_t) 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
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 490 def common_type(t1, t2) raise ArgumentError, 'two types expected' unless (is_ptype?(t1) || is_pnil?(t1)) && (is_ptype?(t2) || is_pnil?(t2)) # if either is nil, the common type is the other if is_pnil?(t1) return t2 elsif is_pnil?(t2) 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?(Types::PArrayType) && t2.is_a?(Types::PArrayType) type = Types::PArrayType.new() type.element_type = common_type(t1.element_type, t2.element_type) return type end # when both are hashes, return a hash with common key- and element type if t1.is_a?(Types::PHashType) && t2.is_a?(Types::PHashType) type = Types::PHashType.new() type.key_type = common_type(t1.key_type, t2.key_type) type.element_type = common_type(t1.element_type, t2.element_type) return type end # when both are host-classes, reduce to PHostClass[] (since one was not assignable to the other) if t1.is_a?(Types::PHostClassType) && t2.is_a?(Types::PHostClassType) return Types::PHostClassType.new() end # when both are resources, reduce to Resource[T] or Resource[] (since one was not assignable to the other) if t1.is_a?(Types::PResourceType) && t2.is_a?(Types::PResourceType) result = Types::PResourceType.new() # only Resource[] unless the type name is the same if t1.type_name == t2.type_name then result.type_name = t1.type_name end # the cross assignability test above has already determined that they do not have the same type and title return result end # Integers have range, expand the range to the common range if t1.is_a?(Types::PIntegerType) && t2.is_a?(Types::PIntegerType) t1range = from_to_ordered(t1.from, t1.to) t2range = from_to_ordered(t2.from, t2.to) t = Types::PIntegerType.new() from = [t1range[0], t2range[0]].min to = [t1range[1], t2range[1]].max t.from = from unless from == TheInfinity t.to = to unless to == TheInfinity return t end # Floats have range, expand the range to the common range if t1.is_a?(Types::PFloatType) && t2.is_a?(Types::PFloatType) t1range = from_to_ordered(t1.from, t1.to) t2range = from_to_ordered(t2.from, t2.to) t = Types::PFloatType.new() from = [t1range[0], t2range[0]].min to = [t1range[1], t2range[1]].max t.from = from unless from == TheInfinity t.to = to unless to == TheInfinity return t end if t1.is_a?(Types::PStringType) && t2.is_a?(Types::PStringType) t = Types::PStringType.new() t.values = t1.values | t2.values return t end if t1.is_a?(Types::PPatternType) && t2.is_a?(Types::PPatternType) t = Types::PPatternType.new() # must make copies since patterns are contained types, not data-types t.patterns = (t1.patterns | t2.patterns).map {|p| p.copy } return t end if t1.is_a?(Types::PEnumType) && t2.is_a?(Types::PEnumType) # The common type is one that complies with either set t = Types::PEnumType.new t.values = t1.values | t2.values return t end if t1.is_a?(Types::PVariantType) && t2.is_a?(Types::PVariantType) # The common type is one that complies with either set t = Types::PVariantType.new t.types = (t1.types | t2.types).map {|opt_t| opt_t.copy } return t end if t1.is_a?(Types::PRegexpType) && t2.is_a?(Types::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 Types::PPatternType.new() end if t1.is_a?(Types::PCallableType) && t2.is_a?(Types::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 Types::PCallableType.new() end # Common abstract types, from most specific to most general if common_numeric?(t1, t2) return Types::PNumericType.new() end if common_scalar?(t1, t2) return Types::PScalarType.new() end if common_data?(t1,t2) return Types::PDataType.new() end # Meta types Type[Integer] + Type[String] => Type[Data] if t1.is_a?(Types::PType) && t2.is_a?(Types::PType) type = Types::PType.new() type.type = common_type(t1.type, t2.type) return type end if t1.is_a?(Types::PRubyType) && t2.is_a?(Types::PRubyType) if t1.ruby_class == t2.ruby_class return t1 end # finding the common super class requires that names are resolved to class c1 = Types::ClassLoader.provide_from_type(t1) c2 = Types::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 result = Types::PRubyType.new() result.ruby_class = c1_super.name return result end end end end end # If both are RubyObjects if common_pobject?(t1, t2) return Types::PObjectType.new() end end |
#data ⇒ Object
Convenience method to get a data type for comparisons
217 218 219 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 217 def data @data_t end |
#data_variant ⇒ Object
Convenience method to get a variant compatible with the Data type.
224 225 226 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 224 def data_variant @data_variant_t end |
#debug_string(t) ⇒ Object
Produces a debug string representing the type (possibly with more information that the regular string format)
666 667 668 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 666 def debug_string(t) @@inspect_visitor.visit_this_0(self, t) end |
#debug_string_Object(t) ⇒ 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.
1326 1327 1328 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1326 def debug_string_Object(t) string(t) end |
#debug_string_PStringType(t) ⇒ 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.
1407 1408 1409 1410 1411 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1407 def debug_string_PStringType(t) range = range_array_part(t.size_type) range_part = range.empty? ? '' : '[' << range.join(' ,') << '], ' "String[" << range_part << (t.values.map {|s| "'#{s}'" }).join(', ') << ']' end |
#enumerable(t) ⇒ Object
Returns an enumerable if the t represents something that can be iterated
273 274 275 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 273 def enumerable(t) @@enumerable_visitor.visit_this_0(self, t) end |
#enumerable_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.
Catches all non enumerable types
1544 1545 1546 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1544 def enumerable_Object(o) nil end |
#enumerable_PIntegerType(t) ⇒ 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.
1549 1550 1551 1552 1553 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1549 def enumerable_PIntegerType(t) # Not enumerable if representing an infinite range return nil if t.size == TheInfinity t end |
#equals(left, right) ⇒ Object
Answers if the two given types describe the same type
286 287 288 289 290 291 292 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 286 def equals(left, right) return false unless left.is_a?(Types::PAbstractType) && right.is_a?(Types::PAbstractType) # 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 |
#from_to_ordered(from, to) ⇒ 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.
926 927 928 929 930 931 932 933 934 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 926 def from_to_ordered(from, to) x = (from.nil? || from == :default) ? -TheInfinity : from y = (to.nil? || to == :default) ? TheInfinity : to if x < y [x, y] else [y, x] end end |
#generalize!(o) ⇒ Object
Generalizes value specific types. The given type is mutated and returned.
337 338 339 340 341 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 337 def generalize!(o) @@generalize_visitor.visit_this_0(self, o) o.eAllContents.each { |x| @@generalize_visitor.visit_this_0(self, x) } o end |
#generalize_Object(o) ⇒ Object
343 344 345 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 343 def generalize_Object(o) # do nothing, there is nothing to change for most types end |
#generalize_PCollectionType(o) ⇒ Object
353 354 355 356 357 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 353 def generalize_PCollectionType(o) # erase the size constraint from Array and Hash (if one exists, it is transformed to -Infinity - + Infinity, which is # not desirable. o.size_type = nil end |
#generalize_PFloatType(o) ⇒ Object
359 360 361 362 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 359 def generalize_PFloatType(o) o.to = nil o.from = nil end |
#generalize_PIntegerType(o) ⇒ Object
364 365 366 367 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 364 def generalize_PIntegerType(o) o.to = nil o.from = nil end |
#generalize_PStringType(o) ⇒ Object
347 348 349 350 351 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 347 def generalize_PStringType(o) o.values = [] o.size_type = nil [] 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).
373 374 375 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 373 def infer(o) @@infer_visitor.visit_this_0(self, o) end |
#infer_and_reduce_type(enumerable) ⇒ Object
Reduce an enumerable of objects to a single common type
681 682 683 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 681 def infer_and_reduce_type(enumerable) reduce_type(enumerable.collect() {|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.
794 795 796 797 798 799 800 801 802 803 804 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 794 def infer_Array(o) type = Types::PArrayType.new() type.element_type = if o.empty? Types::PNilType.new() else infer_and_reduce_type(o) end type.size_type = size_as_type(o) type end |
#infer_Class(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 classes is PType
688 689 690 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 688 def infer_Class(o) Types::PType.new() 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.
693 694 695 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 693 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.
776 777 778 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 776 def infer_FalseClass(o) Types::PBooleanType.new() 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.
737 738 739 740 741 742 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 737 def infer_Float(o) t = Types::PFloatType.new() t.from = o t.to = o t 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.
698 699 700 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 698 def infer_Function(o) o.class.dispatcher.to_type end |
#infer_generic(o) ⇒ Object
377 378 379 380 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 377 def infer_generic(o) result = generalize!(infer(o)) result 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.
807 808 809 810 811 812 813 814 815 816 817 818 819 820 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 807 def infer_Hash(o) type = Types::PHashType.new() if o.empty? ktype = Types::PNilType.new() etype = Types::PNilType.new() else ktype = infer_and_reduce_type(o.keys()) etype = infer_and_reduce_type(o.values()) end type.key_type = ktype type.element_type = etype type.size_type = size_as_type(o) type 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.
745 746 747 748 749 750 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 745 def infer_Integer(o) t = Types::PIntegerType.new() t.from = o t.to = o t 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.
760 761 762 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 760 def infer_NilClass(o) Types::PNilType.new() 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.
703 704 705 706 707 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 703 def infer_Object(o) type = Types::PRubyType.new() type.ruby_class = o.class.name type end |
#infer_PAbstractType(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 PType
712 713 714 715 716 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 712 def infer_PAbstractType(o) type = Types::PType.new() type.type = o.copy type end |
#infer_PType(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 PType This is the metatype short circuit.
722 723 724 725 726 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 722 def infer_PType(o) type = Types::PType.new() type.type = o.copy type 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.
753 754 755 756 757 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 753 def infer_Regexp(o) t = Types::PRegexpType.new() t.pattern = o.source t 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
783 784 785 786 787 788 789 790 791 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 783 def infer_Resource(o) t = Types::PResourceType.new() t.type_name = o.type.to_s # 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 t.title = (title == :undef ? '' : title) t end |
#infer_set(o) ⇒ Object
Answers ‘what is the set of Puppet Types of o’
385 386 387 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 385 def infer_set(o) @@infer_set_visitor.visit_this_0(self, o) end |
#infer_set_Array(o) ⇒ Object
835 836 837 838 839 840 841 842 843 844 845 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 835 def infer_set_Array(o) if o.empty? type = Types::PArrayType.new() type.element_type = Types::PNilType.new() type.size_type = size_as_type(o) else type = Types::PTupleType.new() type.types = o.map() {|x| infer_set(x) } end type end |
#infer_set_Hash(o) ⇒ Object
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 847 def infer_set_Hash(o) type = Types::PHashType.new() if o.empty? ktype = Types::PNilType.new() etype = Types::PNilType.new() else ktype = Types::PVariantType.new() ktype.types = o.keys.map() {|k| infer_set(k) } etype = Types::PVariantType.new() etype.types = o.values.map() {|e| infer_set(e) } end type.key_type = unwrap_single_variant(ktype) type.element_type = unwrap_single_variant(vtype) type.size_type = size_as_type(o) type end |
#infer_set_Object(o) ⇒ Object
Common case for everything that intrinsically only has a single type
831 832 833 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 831 def infer_set_Object(o) infer(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.
729 730 731 732 733 734 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 729 def infer_String(o) t = Types::PStringType.new() t.addValues(o) t.size_type = size_as_type(o) t 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 :undef as PNilType, all other are Ruby
766 767 768 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 766 def infer_Symbol(o) o == :undef ? infer_NilClass(o) : infer_Object(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.
771 772 773 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 771 def infer_TrueClass(o) Types::PBooleanType.new() end |
#injectable_class(klazz) ⇒ Class?
Answers the question ‘is it possible to inject an instance of the given class’ A class is injectable if it has a special *assisted inject* class method called inject taking an injector and a scope as argument, or if it has a zero args initialize method.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 240 def injectable_class(klazz) # Handle case when we get a PType instead of a class if klazz.is_a?(Types::PRubyType) klazz = Puppet::Pops::Types::ClassLoader.provide(klazz) end # data types can not be injected (check again, it is not safe to assume that given RubyType klazz arg was ok) return false unless type(klazz).is_a?(Types::PRubyType) if (klazz.respond_to?(:inject) && klazz.method(:inject).arity() == -4) || klazz.instance_method(:initialize).arity() == 0 klazz else nil end end |
#instance?(t, o) ⇒ Boolean
Answers ‘is o an instance of type t’
466 467 468 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 466 def instance?(t, o) instance_of(t,o) end |
#instance_of(t, o) ⇒ Object
389 390 391 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 389 def instance_of(t, o) @@instance_of_visitor.visit_this_1(self, t, o) end |
#instance_of_Object(t, o) ⇒ Object
393 394 395 396 397 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 393 def instance_of_Object(t, o) # Undef is Undef and Object, but nothing else when checking instance? return false if (o.nil? || o == :undef) && t.class != Types::PObjectType assignable?(t, infer(o)) end |
#instance_of_PArrayType(t, o) ⇒ Object
399 400 401 402 403 404 405 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 399 def instance_of_PArrayType(t, o) return false unless o.is_a?(Array) return false unless o.all? {|element| instance_of(t.element_type, element) } size_t = t.size_type || @collection_default_size_t size_t2 = size_as_type(o) assignable?(size_t, size_t2) end |
#instance_of_PDataType(t, o) ⇒ Object
438 439 440 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 438 def instance_of_PDataType(t, o) instance_of(@data_variant_t, o) end |
#instance_of_PHashType(t, o) ⇒ Object
428 429 430 431 432 433 434 435 436 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 428 def instance_of_PHashType(t, o) return false unless o.is_a?(Hash) key_t = t.key_type element_t = t.element_type return false unless o.keys.all? {|key| instance_of(key_t, key) } && o.values.all? {|value| instance_of(element_t, value) } size_t = t.size_type || @collection_default_size_t size_t2 = size_as_type(o) assignable?(size_t, size_t2) end |
#instance_of_PNilType(t, o) ⇒ Object
442 443 444 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 442 def instance_of_PNilType(t, o) return o.nil? || o == :undef end |
#instance_of_POptionalType(t, o) ⇒ Object
446 447 448 449 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 446 def instance_of_POptionalType(t, o) return true if (o.nil? || o == :undef) instance_of(t.optional_type, o) end |
#instance_of_PStructType(t, o) ⇒ Object
421 422 423 424 425 426 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 421 def instance_of_PStructType(t, o) return false unless o.is_a?(Hash) h = t.hashed_elements # all keys must be present and have a value (even if nil/undef) (o.keys - h.keys).empty? && h.all? { |k,v| instance_of(v, o[k]) } end |
#instance_of_PTupleType(t, o) ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 407 def instance_of_PTupleType(t, o) return false unless o.is_a?(Array) # compute the tuple's min/max size, and check if that size matches size_t = t.size_type || Puppet::Pops::Types::TypeFactory.range(*t.size_range) # compute the array's size as type size_t2 = size_as_type(o) return false unless assignable?(size_t, size_t2) o.each_with_index do |element, index| return false unless instance_of(t.types[index] || t.types[-1], element) end true end |
#instance_of_PVariantType(t, o) ⇒ Object
451 452 453 454 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 451 def instance_of_PVariantType(t, o) # instance of variant if o is instance? of any of variant's types t.types.any? { |option_t| instance_of(option_t, o) } end |
#is_pnil?(t) ⇒ Boolean
Answers if t represents the puppet type PNilType
480 481 482 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 480 def is_pnil?(t) return t.nil? || t.is_a?(Types::PNilType) end |
#is_ptype?(t) ⇒ Boolean
Answers if t is a puppet type
473 474 475 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 473 def is_ptype?(t) return t.is_a?(Types::PAbstractType) end |
#max(a, b) ⇒ Object
991 992 993 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 991 def max(a,b) a >=b ? a : b end |
#min(a, b) ⇒ Object
995 996 997 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 995 def min(a,b) a <= b ? a : b end |
#range_array_part(t) ⇒ 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 a string from an Integer range type that is used inside other type strings
1375 1376 1377 1378 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1375 def range_array_part(t) return [] if t.nil? || (t.from.nil? && t.to.nil?) [t.from.nil? ? 'default' : t.from , t.to.nil? ? 'default' : t.to ] end |
#reduce_type(enumerable) ⇒ Object
Reduces an enumerable of types to a single common type.
674 675 676 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 674 def reduce_type(enumerable) enumerable.reduce(nil) {|memo, t| common_type(memo, t) } end |
#size_as_type(collection) ⇒ Object
822 823 824 825 826 827 828 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 822 def size_as_type(collection) size = collection.size t = Types::PIntegerType.new() t.from = size t.to = size t 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
912 913 914 915 916 917 918 919 920 921 922 923 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 912 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 if x < y [x, y] else [y, x] end end |
#string(t) ⇒ Object
Produces a string representing the type
659 660 661 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 659 def string(t) @@string_visitor.visit_this_0(self, t) end |
#string_NilClass(t) ⇒ 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.
1340 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1340 def string_NilClass(t) ; '?' ; end |
#string_PArrayType(t) ⇒ 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.
1496 1497 1498 1499 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1496 def string_PArrayType(t) parts = [string(t.element_type)] + range_array_part(t.size_type) "Array[#{parts.join(', ')}]" end |
#string_PBooleanType(t) ⇒ 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.
1352 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1352 def string_PBooleanType(t) ; "Boolean" ; end |
#string_PCallableType(t) ⇒ 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.
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1438 def string_PCallableType(t) # generic return "Callable" if t.param_types.nil? if t.param_types.types.empty? range = [0, 0] else range = range_array_part(t.param_types.size_type) end types = t.param_types.types.map {|t2| string(t2) } params_part= types.join(', ') s = "Callable[" << types.join(', ') unless range.empty? (s << ', ') unless types.empty? s << range.join(', ') end # Add block T last (after min, max) if present) # unless t.block_type.nil? (s << ', ') unless types.empty? && range.empty? s << string(t.block_type) end s << "]" s end |
#string_PCatalogEntryType(t) ⇒ 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.
1508 1509 1510 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1508 def string_PCatalogEntryType(t) "CatalogEntry" end |
#string_PCollectionType(t) ⇒ 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.
1483 1484 1485 1486 1487 1488 1489 1490 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1483 def string_PCollectionType(t) range = range_array_part(t.size_type) unless range.empty? "Collection[#{range.join(', ')}]" else "Collection" end end |
#string_PDataType(t) ⇒ 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.
1358 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1358 def string_PDataType(t) ; "Data" ; end |
#string_PEnumType(t) ⇒ 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.
1414 1415 1416 1417 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1414 def string_PEnumType(t) return "Enum" if t.values.empty? "Enum[" << t.values.map {|s| "'#{s}'" }.join(', ') << ']' end |
#string_PFloatType(t) ⇒ 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.
1381 1382 1383 1384 1385 1386 1387 1388 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1381 def string_PFloatType(t) range = range_array_part(t) unless range.empty? "Float[#{range.join(', ')}]" else "Float" end end |
#string_PHashType(t) ⇒ 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.
1502 1503 1504 1505 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1502 def string_PHashType(t) parts = [string(t.key_type), string(t.element_type)] + range_array_part(t.size_type) "Hash[#{parts.join(', ')}]" end |
#string_PHostClassType(t) ⇒ 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.
1513 1514 1515 1516 1517 1518 1519 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1513 def string_PHostClassType(t) if t.class_name "Class[#{t.class_name}]" else "Class" end end |
#string_PIntegerType(t) ⇒ 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.
1364 1365 1366 1367 1368 1369 1370 1371 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1364 def string_PIntegerType(t) range = range_array_part(t) unless range.empty? "Integer[#{range.join(', ')}]" else "Integer" end end |
#string_PNilType(t) ⇒ 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.
1349 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1349 def string_PNilType(t) ; 'Undef' ; end |
#string_PNumericType(t) ⇒ 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.
1361 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1361 def string_PNumericType(t) ; "Numeric" ; end |
#string_PObjectType(t) ⇒ 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.
1346 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1346 def string_PObjectType(t) ; "Object" ; end |
#string_POptionalType(t) ⇒ Object
1534 1535 1536 1537 1538 1539 1540 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1534 def string_POptionalType(t) if t.optional_type.nil? "Optional" else "Optional[#{string(t.optional_type)}]" end end |
#string_PPatternType(t) ⇒ 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.
1477 1478 1479 1480 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1477 def string_PPatternType(t) return "Pattern" if t.patterns.empty? "Pattern[" << t.patterns.map {|s| "#{s.regexp.inspect}" }.join(', ') << ']' end |
#string_PRegexpType(t) ⇒ 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.
1391 1392 1393 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1391 def string_PRegexpType(t) t.pattern.nil? ? "Regexp" : "Regexp[#{t.regexp.inspect}]" end |
#string_PResourceType(t) ⇒ 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.
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1522 def string_PResourceType(t) if t.type_name if t.title "#{t.type_name.capitalize}['#{t.title}']" else "#{t.type_name.capitalize}" end else "Resource" end end |
#string_PRubyType(t) ⇒ 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.
1493 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1493 def string_PRubyType(t) ; "Ruby[#{string(t.ruby_class)}]" ; end |
#string_PScalarType(t) ⇒ 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.
1355 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1355 def string_PScalarType(t) ; "Scalar" ; end |
#string_PStringType(t) ⇒ 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.
1396 1397 1398 1399 1400 1401 1402 1403 1404 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1396 def string_PStringType(t) # skip values in regular output - see debug_string range = range_array_part(t.size_type) unless range.empty? "String[#{range.join(', ')}]" else "String" end end |
#string_PStructElement(t) ⇒ Object
1472 1473 1474 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1472 def string_PStructElement(t) "'#{t.name}'=>#{string(t.type)}" end |
#string_PStructType(t) ⇒ 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.
1467 1468 1469 1470 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1467 def string_PStructType(t) return "Struct" if t.elements.empty? "Struct[{" << t.elements.map {|element| string(element) }.join(', ') << "}]" end |
#string_PTupleType(t) ⇒ 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.
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1426 def string_PTupleType(t) range = range_array_part(t.size_type) return "Tuple" if t.types.empty? s = "Tuple[" << t.types.map {|t2| string(t2) }.join(', ') unless range.empty? s << ", " << range.join(', ') end s << "]" s end |
#string_PType(t) ⇒ 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.
1331 1332 1333 1334 1335 1336 1337 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1331 def string_PType(t) if t.type.nil? "Type" else "Type[#{string(t.type)}]" end end |
#string_PVariantType(t) ⇒ 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.
1420 1421 1422 1423 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1420 def string_PVariantType(t) return "Variant" if t.types.empty? "Variant[" << t.types.map {|t2| string(t2) }.join(', ') << ']' end |
#string_String(t) ⇒ 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.
1343 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1343 def string_String(t) ; t ; end |
#superclasses(c) ⇒ Object
Produces the superclasses of the given class, including the class
647 648 649 650 651 652 653 654 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 647 def superclasses(c) result = [c] while s = c.superclass result << s c = s end result 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
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 1039 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’
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 298 def type(c) raise ArgumentError, "Argument must be a Class" unless c.is_a? Class # Can't use a visitor here since we don't have an instance of the class case when c <= Integer type = Types::PIntegerType.new() when c == Float type = Types::PFloatType.new() when c == Numeric type = Types::PNumericType.new() when c == String type = Types::PStringType.new() when c == Regexp type = Types::PRegexpType.new() when c == NilClass type = Types::PNilType.new() when c == FalseClass, c == TrueClass type = Types::PBooleanType.new() when c == Class type = Types::PType.new() when c == Array # Assume array of data values type = Types::PArrayType.new() type.element_type = Types::PDataType.new() when c == Hash # Assume hash with scalar keys and data values type = Types::PHashType.new() type.key_type = Types::PScalarType.new() type.element_type = Types::PDataType.new() else type = Types::PRubyType.new() type.ruby_class = c.name end type end |
#unwrap_single_variant(possible_variant) ⇒ Object
864 865 866 867 868 869 870 |
# File 'lib/puppet/pops/types/type_calculator.rb', line 864 def unwrap_single_variant(possible_variant) if possible_variant.is_a?(Types::PVariantType) && possible_variant.types.size == 1 possible_variant.types[0] else possible_variant end end |