Method: Puppet::Pops::Types::TypeFactory.struct
- Defined in:
- lib/puppet/pops/types/type_factory.rb
.struct(hash = {}) ⇒ PStructType
Produces the Struct type, either a non parameterized instance representing all structs (i.e. all hashes) or a hash with entries where the key is either a literal String, an Enum with one entry, or a String representing exactly one value. The key type may also be wrapped in a NotUndef or an Optional.
The value can be a ruby class, a String (interpreted as the name of a ruby class) or a Type.
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 |
# File 'lib/puppet/pops/types/type_factory.rb', line 172 def self.struct(hash = {}) tc = @type_calculator elements = hash.map do |key_type, value_type| value_type = type_of(value_type) raise ArgumentError, 'Struct element value_type must be a Type' unless value_type.is_a?(PAnyType) # TODO: Should have stricter name rule if key_type.is_a?(String) raise ArgumentError, 'Struct element key cannot be an empty String' if key_type.empty? key_type = string(key_type) # Must make key optional if the value can be Undef key_type = optional(key_type) if tc.assignable?(value_type, PUndefType::DEFAULT) else # assert that the key type is one of String[1], NotUndef[String[1]] and Optional[String[1]] case key_type when PNotUndefType # We can loose the NotUndef wrapper here since String[1] isn't optional anyway key_type = key_type.type s = key_type when POptionalType s = key_type.optional_type when PStringType s = key_type when PEnumType s = key_type.values.size == 1 ? PStringType.new(key_type.values[0]) : nil else raise ArgumentError, "Illegal Struct member key type. Expected NotUndef, Optional, String, or Enum. Got: #{key_type.class.name}" end unless s.is_a?(PStringType) && !s.value.nil? raise ArgumentError, "Unable to extract a non-empty literal string from Struct member key type #{tc.string(key_type)}" end end PStructElement.new(key_type, value_type) end PStructType.new(elements) end |