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
1996 def initialize(elements)
1997   @elements = elements.freeze
1998 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
1992 def self.register_ptype(loader, ir)
1993   create_ptype(loader, ir, 'AnyType', 'elements' => PArrayType.new(PTypeReferenceType.new('Pcore::StructElement')))
1994 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
2000 def accept(visitor, guard)
2001   super
2002   @elements.each { |elem| elem.accept(visitor, guard) }
2003 end
each() { |elem| ... } click to toggle source
     # File lib/puppet/pops/types/types.rb
2005 def each
2006   if block_given?
2007     elements.each { |elem| yield elem }
2008   else
2009     elements.to_enum
2010   end
2011 end
elements() click to toggle source
     # File lib/puppet/pops/types/types.rb
2067 def elements
2068   @elements
2069 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
2063 def eql?(o)
2064   self.class == o.class && @elements == o.elements
2065 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
2013 def generalize
2014   if @elements.empty?
2015     DEFAULT
2016   else
2017     alter_type_array(@elements, :generalize) { |altered| PStructType.new(altered) }
2018   end
2019 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
2033 def hash
2034   @elements.hash
2035 end
hashed_elements() click to toggle source
     # File lib/puppet/pops/types/types.rb
2029 def hashed_elements
2030   @hashed ||= @elements.reduce({}) {|memo, e| memo[e.name] = e; memo }
2031 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2071 def instance?(o, guard = nil)
2072   # The inferred type of a class derived from Hash is either Runtime or Object. It's not assignable to the Struct type.
2073   return false unless o.instance_of?(Hash)
2074   matched = 0
2075   @elements.all? do |e|
2076     key = e.name
2077     v = o[key]
2078     if v.nil? && !o.include?(key)
2079       # Entry is missing. Only OK when key is optional
2080       e.key_type.assignable?(PUndefType::DEFAULT, guard)
2081     else
2082       matched += 1
2083       e.value_type.instance?(v, guard)
2084     end
2085   end && matched == o.size
2086 end
iterable?(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2037 def iterable?(guard = nil)
2038   true
2039 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2041 def iterable_type(guard = nil)
2042   if self == DEFAULT
2043     PIterableType.new(PHashType::DEFAULT_KEY_PAIR_TUPLE)
2044   else
2045     PIterableType.new(
2046       PTupleType.new([
2047         PVariantType.maybe_create(@elements.map {|se| se.key_type }),
2048         PVariantType.maybe_create(@elements.map {|se| se.value_type })],
2049         PHashType::KEY_PAIR_TUPLE_SIZE))
2050   end
2051 end
new_function() click to toggle source
     # File lib/puppet/pops/types/types.rb
2088 def new_function
2089   # Simply delegate to Hash type and let the higher level assertion deal with
2090   # compliance with the Struct type regarding the produced result.
2091   PHashType.new_function(self)
2092 end
normalize(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2021 def normalize(guard = nil)
2022   if @elements.empty?
2023     DEFAULT
2024   else
2025     alter_type_array(@elements, :normalize, guard) { |altered| PStructType.new(altered) }
2026   end
2027 end
resolve(loader) click to toggle source
     # File lib/puppet/pops/types/types.rb
2053 def resolve(loader)
2054   changed = false
2055   relements = @elements.map do |elem|
2056     relem = elem.resolve(loader)
2057     changed ||= !relem.equal?(elem)
2058     relem
2059   end
2060   changed ? self.class.new(relements) : self
2061 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

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