class Puppet::Pops::Types::PStructType

@api public

Constants

DEFAULT

Public Class Methods

new(elements) click to toggle source
     # File lib/puppet/pops/types/types.rb
1995 def initialize(elements)
1996   @elements = elements.freeze
1997 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
1991 def self.register_ptype(loader, ir)
1992   create_ptype(loader, ir, 'AnyType', 'elements' => PArrayType.new(PTypeReferenceType.new('Pcore::StructElement')))
1993 end

Public Instance Methods

accept(visitor, guard) click to toggle source
Calls superclass method Puppet::Pops::Types::PAnyType#accept
     # File lib/puppet/pops/types/types.rb
1999 def accept(visitor, guard)
2000   super
2001   @elements.each { |elem| elem.accept(visitor, guard) }
2002 end
each() { |elem| ... } click to toggle source
     # File lib/puppet/pops/types/types.rb
2004 def each
2005   if block_given?
2006     elements.each { |elem| yield elem }
2007   else
2008     elements.to_enum
2009   end
2010 end
elements() click to toggle source
     # File lib/puppet/pops/types/types.rb
2066 def elements
2067   @elements
2068 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
2062 def eql?(o)
2063   self.class == o.class && @elements == o.elements
2064 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
2012 def generalize
2013   if @elements.empty?
2014     DEFAULT
2015   else
2016     alter_type_array(@elements, :generalize) { |altered| PStructType.new(altered) }
2017   end
2018 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
2032 def hash
2033   @elements.hash
2034 end
hashed_elements() click to toggle source
     # File lib/puppet/pops/types/types.rb
2028 def hashed_elements
2029   @hashed ||= @elements.reduce({}) {|memo, e| memo[e.name] = e; memo }
2030 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2070 def instance?(o, guard = nil)
2071   # The inferred type of a class derived from Hash is either Runtime or Object. It's not assignable to the Struct type.
2072   return false unless o.instance_of?(Hash)
2073   matched = 0
2074   @elements.all? do |e|
2075     key = e.name
2076     v = o[key]
2077     if v.nil? && !o.include?(key)
2078       # Entry is missing. Only OK when key is optional
2079       e.key_type.assignable?(PUndefType::DEFAULT, guard)
2080     else
2081       matched += 1
2082       e.value_type.instance?(v, guard)
2083     end
2084   end && matched == o.size
2085 end
iterable?(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2036 def iterable?(guard = nil)
2037   true
2038 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2040 def iterable_type(guard = nil)
2041   if self == DEFAULT
2042     PIterableType.new(PHashType::DEFAULT_KEY_PAIR_TUPLE)
2043   else
2044     PIterableType.new(
2045       PTupleType.new([
2046         PVariantType.maybe_create(@elements.map {|se| se.key_type }),
2047         PVariantType.maybe_create(@elements.map {|se| se.value_type })],
2048         PHashType::KEY_PAIR_TUPLE_SIZE))
2049   end
2050 end
new_function() click to toggle source
     # File lib/puppet/pops/types/types.rb
2087 def new_function
2088   # Simply delegate to Hash type and let the higher level assertion deal with
2089   # compliance with the Struct type regarding the produced result.
2090   PHashType.new_function(self)
2091 end
normalize(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2020 def normalize(guard = nil)
2021   if @elements.empty?
2022     DEFAULT
2023   else
2024     alter_type_array(@elements, :normalize, guard) { |altered| PStructType.new(altered) }
2025   end
2026 end
resolve(loader) click to toggle source
     # File lib/puppet/pops/types/types.rb
2052 def resolve(loader)
2053   changed = false
2054   relements = @elements.map do |elem|
2055     relem = elem.resolve(loader)
2056     changed ||= !relem.equal?(elem)
2057     relem
2058   end
2059   changed ? self.class.new(relements) : self
2060 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
2098 def _assignable?(o, guard)
2099   if o.is_a?(Types::PStructType)
2100     h2 = o.hashed_elements
2101     matched = 0
2102     elements.all? do |e1|
2103       e2 = h2[e1.name]
2104       if e2.nil?
2105         e1.key_type.assignable?(PUndefType::DEFAULT, guard)
2106       else
2107         matched += 1
2108         e1.key_type.assignable?(e2.key_type, guard) && e1.value_type.assignable?(e2.value_type, guard)
2109       end
2110     end && matched == h2.size
2111   elsif o.is_a?(Types::PHashType)
2112     required = 0
2113     required_elements_assignable = elements.all? do |e|
2114       key_type = e.key_type
2115       if key_type.assignable?(PUndefType::DEFAULT)
2116         # Element is optional so Hash does not need to provide it
2117         true
2118       else
2119         required += 1
2120         if e.value_type.assignable?(o.value_type, guard)
2121           # Hash must have something that is assignable. We don't care about the name or size of the key though
2122           # because we have no instance of a hash to compare against.
2123           key_type.generalize.assignable?(o.key_type)
2124         else
2125           false
2126         end
2127       end
2128     end
2129     if required_elements_assignable
2130       size_o = o.size_type || PCollectionType::DEFAULT_SIZE
2131       PIntegerType.new(required, elements.size).assignable?(size_o, guard)
2132     else
2133       false
2134     end
2135   else
2136     false
2137   end
2138 end