Class: Puppet::Pops::Types::PTypeSetType

Inherits:
PMetaType show all
Defined in:
lib/puppet/pops/types/p_type_set_type.rb

Defined Under Namespace

Classes: TypeSetLoader

Constant Summary collapse

TYPE_STRING_OR_VERSION =
TypeFactory.variant(PStringType::NON_EMPTY, TypeFactory.sem_ver)
TYPE_STRING_OR_RANGE =
TypeFactory.variant(PStringType::NON_EMPTY, TypeFactory.sem_ver_range)
TYPE_TYPE_REFERENCE_I12N =
TypeFactory.struct({
  KEY_NAME => Pcore::TYPE_QUALIFIED_REFERENCE,
  KEY_VERSION_RANGE => TYPE_STRING_OR_RANGE,
  TypeFactory.optional(KEY_NAME_AUTHORITY) => Pcore::TYPE_URI,
  TypeFactory.optional(KEY_ANNOTATIONS) => TYPE_ANNOTATIONS
})
TYPE_TYPESET_I12N =
TypeFactory.struct({
  TypeFactory.optional(Pcore::KEY_PCORE_URI) => Pcore::TYPE_URI,
  Pcore::KEY_PCORE_VERSION => TYPE_STRING_OR_VERSION,
  TypeFactory.optional(KEY_NAME_AUTHORITY) => Pcore::TYPE_URI,
  TypeFactory.optional(KEY_NAME) => Pcore::TYPE_QUALIFIED_REFERENCE,
  KEY_VERSION => TYPE_STRING_OR_VERSION,
  TypeFactory.optional(KEY_TYPES) => TypeFactory.hash_kv(Pcore::TYPE_SIMPLE_TYPE_NAME, PType::DEFAULT, PCollectionType::NOT_EMPTY_SIZE),
  TypeFactory.optional(KEY_REFERENCES) => TypeFactory.hash_kv(Pcore::TYPE_SIMPLE_TYPE_NAME, TYPE_TYPE_REFERENCE_I12N, PCollectionType::NOT_EMPTY_SIZE),
  TypeFactory.optional(KEY_ANNOTATIONS) => TYPE_ANNOTATIONS,
})
DEFAULT =
self.new({
  KEY_NAME => 'DefaultTypeSet',
  KEY_NAME_AUTHORITY => Pcore::RUNTIME_NAME_AUTHORITY,
  Pcore::KEY_PCORE_URI => Pcore::PCORE_URI,
  Pcore::KEY_PCORE_VERSION => Pcore::PCORE_VERSION,
  KEY_VERSION => SemanticPuppet::Version.new(0,0,0)
})

Constants included from Annotatable

Annotatable::TYPE_ANNOTATIONS, Annotatable::TYPE_ANNOTATION_KEY_TYPE, Annotatable::TYPE_ANNOTATION_VALUE_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PMetaType

register_ptype, #resolve_type_refs, #to_s

Methods included from Annotatable

#annotatable_accept, #init_annotatable

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, #create, #generalize, #instance?, #iterable?, #iterable_type, #kind_of_callable?, #new_function, new_function, #normalize, #really_instance?, register_ptype, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from PuppetObject

#_ptype

Constructor Details

#initialize(name, i12n_hash_expression) ⇒ PTypeSetType #initialize(i12n_hash) ⇒ PTypeSetType

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.

Initialize a TypeSet Type instance. The initialization will use either a name and an initialization hash expression, or a fully resolved initialization hash.

Overloads:

  • #initialize(name, i12n_hash_expression) ⇒ PTypeSetType

    Used when the TypeSet type is loaded using a type alias expression. When that happens, it is important that the actual resolution of the expression is deferred until all definitions have been made known to the current loader. The package will then be resolved when it is loaded by the TypeParser. “resolved” here, means that the hash expression is fully resolved, and then passed to the #initialize_from_hash method.

    Parameters:

    • name (String)

      The name of the type set

    • i12n_hash_expression (Model::LiteralHash)

      The hash describing the TypeSet features

    • name_authority (String)

      The default name authority for the type set

  • #initialize(i12n_hash) ⇒ PTypeSetType

    Used when the package is created by the TypeFactory. The i12n_hash must be fully resolved.

    Parameters:

    • i12n_hash (Hash{String=>Object})

      The hash describing the TypeSet features



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 83

def initialize(name_or_i12n_hash, i12n_hash_expression = nil, name_authority = nil)
  @types = EMPTY_HASH
  @references = EMPTY_HASH

  if name_or_i12n_hash.is_a?(Hash)
    initialize_from_hash(name_or_i12n_hash)
  else
    # Creation using "type XXX = TypeSet[{}]". This means that the name is given
    @name = TypeAsserter.assert_instance_of('TypeSet name', Pcore::TYPE_QUALIFIED_REFERENCE, name_or_i12n_hash)
    @name_authority = TypeAsserter.assert_instance_of('TypeSet name_authority', Pcore::TYPE_URI, name_authority, true)
    @i12n_hash_expression = i12n_hash_expression
  end
end

Instance Attribute Details

#annotationsObject (readonly)



64
65
66
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 64

def annotations
  @annotations
end

#nameObject (readonly)



60
61
62
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 60

def name
  @name
end

#name_authorityObject (readonly)



59
60
61
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 59

def name_authority
  @name_authority
end

#pcore_uriObject (readonly)



57
58
59
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 57

def pcore_uri
  @pcore_uri
end

#pcore_versionObject (readonly)



58
59
60
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 58

def pcore_version
  @pcore_version
end

#referencesObject (readonly)



63
64
65
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 63

def references
  @references
end

#typesObject (readonly)



62
63
64
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 62

def types
  @types
end

#versionObject (readonly)



61
62
63
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 61

def version
  @version
end

Instance Method Details

#[](qname) ⇒ PAnyType?

Resolve a type in this type set using a qualified name. The resolved type may either be a type defined in this type set or a type defined in a type set that is referenced by this type set (nesting may occur to any level). The name resolution is case insensitive.

Parameters:

  • qname (String)

    the qualified name of the type to resolve

Returns:

  • (PAnyType, nil)

    the resolved type, or ‘nil` in case no type could be found



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 185

def [](qname)
  type = @types[qname] || @types[@dc_to_cc_map[qname.downcase]]
  if type.nil? && !@references.empty?
    segments = qname.split(TypeFormatter::NAME_SEGMENT_SEPARATOR)
    first = segments[0]
    type_set_ref = @references[first] || @references[@dc_to_cc_map[first.downcase]]
    if type_set_ref.nil?
      nil
    else
      type_set = type_set_ref.type_set
      case segments.size
      when 1
        type_set
      when 2
        type_set[segments[1]]
      else
        segments.shift
        type_set[segments.join(TypeFormatter::NAME_SEGMENT_SEPARATOR)]
      end
    end
  else
    type
  end
end

#accept(visitor, guard) ⇒ Object



237
238
239
240
241
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 237

def accept(visitor, guard)
  super
  @types.each_value { |type| type.accept(visitor, guard) }
  @references.each_value { |ref| ref.accept(visitor, guard) }
end

#defines_type?(t) ⇒ Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 210

def defines_type?(t)
  !@types.key(t).nil?
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


317
318
319
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 317

def eql?(o)
  self.class == o.class && @name_authority == o.name_authority && @name == o.name && @version == o.version
end

#hashObject



313
314
315
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 313

def hash
  @name_authority.hash ^ @name.hash ^ @version.hash
end

#i12n_hashHash{String => 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.

Produce a hash suitable for the initializer

Returns:

  • (Hash{String => Object})

    the initialization hash



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 165

def i12n_hash
  result = super()
  result[Pcore::KEY_PCORE_URI] = @pcore_uri unless @pcore_uri.nil?
  result[Pcore::KEY_PCORE_VERSION] =  @pcore_version.to_s
  result[KEY_NAME_AUTHORITY] = @name_authority unless @name_authority.nil?
  result[KEY_NAME] = @name
  result[KEY_VERSION] = @version.to_s
  result[KEY_TYPES] = @types unless @types.empty?
  result[KEY_REFERENCES] = Hash[@references.map { |ref_alias, ref| [ref_alias, ref.i12n_hash] }] unless @references.empty?
  result
end

#initialize_from_hash(i12n_hash) ⇒ 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.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 98

def initialize_from_hash(i12n_hash)
  TypeAsserter.assert_instance_of('TypeSet initializer', TYPE_TYPESET_I12N, i12n_hash)

  # Name given to the loader have higher precedence than a name declared in the type
  @name ||= i12n_hash[KEY_NAME].freeze
  @name_authority ||= i12n_hash[KEY_NAME_AUTHORITY].freeze

  @pcore_version = PSemVerType.convert(i12n_hash[Pcore::KEY_PCORE_VERSION]).freeze
  unless Pcore::PARSABLE_PCORE_VERSIONS.include?(@pcore_version)
    raise ArgumentError,
      "The pcore version for TypeSet '#{@name}' is not understood by this runtime. Expected range #{Pcore::PARSABLE_PCORE_VERSIONS}, got #{@pcore_version}"
  end

  @pcore_uri = i12n_hash[Pcore::KEY_PCORE_URI].freeze
  @version = PSemVerType.convert(i12n_hash[KEY_VERSION])
  @types = i12n_hash[KEY_TYPES] || EMPTY_HASH
  @types.freeze

  # Map downcase names to their camel-cased equivalent
  @dc_to_cc_map = {}
  @types.keys.each { |key| @dc_to_cc_map[key.downcase] = key }

  refs = i12n_hash[KEY_REFERENCES]
  if refs.nil?
    @references = EMPTY_HASH
  else
    ref_map = {}
    root_map = Hash.new { |h, k| h[k] = {} }
    refs.each do |ref_alias, ref|
      ref = TypeSetReference.new(self, ref)

      # Protect against importing the exact same name_authority/name combination twice if the version ranges intersect
      ref_name = ref.name
      ref_na = ref.name_authority || @name_authority
      na_roots = root_map[ref_na]

      ranges = na_roots[ref_name]
      if ranges.nil?
        na_roots[ref_name] = [ref.version_range]
      else
        unless ranges.all? { |range| (range & ref.version_range).nil? }
          raise ArgumentError, "TypeSet '#{@name}' references TypeSet '#{ref_na}/#{ref_name}' more than once using overlapping version ranges"
        end
        ranges << ref.version_range
      end

      if ref_map.has_key?(ref_alias)
        raise ArgumentError, "TypeSet '#{@name}' references a TypeSet using alias '#{ref_alias}' more than once"
      end
      if @types.has_key?(ref_alias)
        raise ArgumentError, "TypeSet '#{@name}' references a TypeSet using alias '#{ref_alias}'. The alias collides with the name of a declared type"
      end
      ref_map[ref_alias] = ref

      @dc_to_cc_map[ref_alias.downcase] = ref_alias
      ref_map[ref_alias] = ref
    end
    @references = ref_map.freeze
  end
  @dc_to_cc_map.freeze
  init_annotatable(i12n_hash)
end

#labelObject

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.



244
245
246
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 244

def label
  "TypeSet '#{@name}'"
end

#name_for(t, default_name) ⇒ String

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 name by which the given type is referenced from within this type set

Parameters:

Returns:

  • (String)

    the name by which the type is referenced within this type set



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 219

def name_for(t, default_name)
  key = @types.key(t)
  if key.nil?
    qname = t.name
    if @references.empty?
      default_name
    else
      @references.each_pair do |ref_key, ref|
        ref_name = ref.type_set.name_for(t, nil)
        return "#{ref_key}::#{ref_name}" unless ref_name.nil?
      end
      default_name
    end
  else
    key
  end
end

#resolve(type_parser, loader) ⇒ 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.



249
250
251
252
253
254
255
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 249

def resolve(type_parser, loader)
  super
  @references.each_value { |ref| ref.resolve(type_parser, loader) }
  tsa_loader = TypeSetLoader.new(self, loader)
  @types.values.each { |type| type.resolve(type_parser, tsa_loader) }
  self
end

#resolve_hash(type_parser, loader, i12n_hash) ⇒ 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.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 292

def resolve_hash(type_parser, loader, i12n_hash)
  result = Hash[i12n_hash.map do |key, value|
    key = resolve_type_refs(type_parser, loader, key)
    value = resolve_type_refs(type_parser, loader, value) unless key == KEY_TYPES && value.is_a?(Hash)
    [key, value]
  end]
  name_auth = resolve_name_authority(result, loader)
  types = result[KEY_TYPES]
  if types.is_a?(Hash)
    types.each do |type_name, value|
      full_name = "#{@name}::#{type_name}".freeze
      typed_name = Loader::TypedName.new(:type, full_name, name_auth)
      meta_name = value.is_a?(Hash) ? 'Object' : 'TypeAlias'
      type = Loader::TypeDefinitionInstantiator.create_named_type(full_name, meta_name, value, name_auth)
      loader.set_entry(typed_name, type)
      types[type_name] = type
    end
  end
  result
end

#resolve_literal_hash(type_parser, loader, i12n_hash_expression) ⇒ 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.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/puppet/pops/types/p_type_set_type.rb', line 258

def resolve_literal_hash(type_parser, loader, i12n_hash_expression)
  result = {}
  i12n_hash_expression.entries.each do |entry|
    key = type_parser.interpret_any(entry.key, loader)
    if (key == KEY_TYPES || key == KEY_REFERENCES) && entry.value.is_a?(Model::LiteralHash)
      # Skip type parser interpretation and convert qualified references directly to String keys.
      hash = {}
      entry.value.entries.each do |he|
        kex = he.key
        name = kex.is_a?(Model::QualifiedReference) ? kex.cased_value : type_parser.interpret_any(kex, loader)
        hash[name] = key == KEY_TYPES ? he.value : type_parser.interpret_any(he.value, loader)
      end
      result[key] = hash
    else
      result[key] = type_parser.interpret_any(entry.value, loader)
    end
  end

  name_auth = resolve_name_authority(result, loader)

  types = result[KEY_TYPES]
  if types.is_a?(Hash)
    types.each do |type_name, value|
      full_name = "#{@name}::#{type_name}".freeze
      typed_name = Loader::TypedName.new(:type, full_name, name_auth)
      type = Loader::TypeDefinitionInstantiator.create_type(full_name, value, name_auth)
      loader.set_entry(typed_name, type, Adapters::SourcePosAdapter.adapt(value).to_uri)
      types[type_name] = type
    end
  end
  result
end