class Puppet::Pops::Loader::TypeDefinitionInstantiator

Public Class Methods

create(loader, typed_name, source_ref, pp_code_string) click to toggle source
   # File lib/puppet/pops/loader/type_definition_instantiator.rb
 6 def self.create(loader, typed_name, source_ref, pp_code_string)
 7   # parse and validate
 8   parser = Parser::EvaluatingParser.new()
 9   model = parser.parse_string(pp_code_string, source_ref)
10   # Only one type is allowed (and no other definitions)
11 
12   name = typed_name.name
13   case model.definitions.size
14   when 0
15     raise ArgumentError, _("The code loaded from %{source_ref} does not define the type '%{name}' - it is empty.") % { source_ref: source_ref, name: name }
16   when 1
17     # ok
18   else
19     raise ArgumentError,
20       _("The code loaded from %{source_ref} must contain only the type '%{name}' - it has additional definitions.") % { source_ref: source_ref, name: name }
21   end
22   type_definition = model.definitions[0]
23 
24   unless type_definition.is_a?(Model::TypeAlias) || type_definition.is_a?(Model::TypeDefinition)
25     raise ArgumentError,
26       _("The code loaded from %{source_ref} does not define the type '%{name}' - no type alias or type definition found.") % { source_ref: source_ref, name: name }
27   end
28 
29   actual_name = type_definition.name
30   unless name == actual_name.downcase
31     raise ArgumentError,
32       _("The code loaded from %{source_ref} produced type with the wrong name, expected '%{name}', actual '%{actual_name}'") % { source_ref: source_ref, name: name, actual_name: actual_name }
33   end
34 
35   unless model.body == type_definition
36     raise ArgumentError,
37       _("The code loaded from %{source_ref} contains additional logic - can only contain the type '%{name}'") % { source_ref: source_ref, name: name }
38   end
39 
40   # Adapt the type definition with loader - this is used from logic contained in its body to find the
41   # loader to use when resolving contained aliases API. Such logic have a hard time finding the closure (where
42   # the loader is known - hence this mechanism
43   private_loader = loader.private_loader
44   Adapters::LoaderAdapter.adapt(type_definition).loader_name = private_loader.loader_name
45   create_runtime_type(type_definition)
46 end
create_from_model(type_definition, loader) click to toggle source
   # File lib/puppet/pops/loader/type_definition_instantiator.rb
48 def self.create_from_model(type_definition, loader)
49   typed_name = TypedName.new(:type, type_definition.name)
50   type = create_runtime_type(type_definition)
51   loader.set_entry(
52     typed_name,
53     type,
54     type_definition.locator.to_uri(type_definition))
55   type
56 end
create_named_type(name, type_name, type_expr, name_authority) click to toggle source

@api private

   # File lib/puppet/pops/loader/type_definition_instantiator.rb
72 def self.create_named_type(name, type_name, type_expr, name_authority)
73   case type_name
74   when 'Object'
75     # No need for an alias. The Object type itself will receive the name instead
76     unless type_expr.is_a?(Model::LiteralHash)
77       type_expr = type_expr.keys.empty? ? nil : type_expr.keys[0] unless type_expr.is_a?(Hash)
78     end
79     Types::PObjectType.new(name, type_expr)
80   when 'TypeSet'
81     # No need for an alias. The Object type itself will receive the name instead
82     type_expr = type_expr.keys.empty? ? nil : type_expr.keys[0] unless type_expr.is_a?(Hash)
83     Types::PTypeSetType.new(name, type_expr, name_authority)
84   else
85     Types::PTypeAliasType.new(name, type_expr)
86   end
87 end
create_runtime_type(type_definition) click to toggle source

@api private

   # File lib/puppet/pops/loader/type_definition_instantiator.rb
59 def self.create_runtime_type(type_definition)
60   # Using the RUNTIME_NAME_AUTHORITY as the name_authority is motivated by the fact that the type
61   # alias name (managed by the runtime) becomes the name of the created type
62   #
63   create_type(type_definition.name, type_definition.type_expr, Pcore::RUNTIME_NAME_AUTHORITY)
64 end
create_type(name, type_expr, name_authority) click to toggle source

@api private

   # File lib/puppet/pops/loader/type_definition_instantiator.rb
67 def self.create_type(name, type_expr, name_authority)
68   create_named_type(name, named_definition(type_expr), type_expr, name_authority)
69 end
named_definition(te) click to toggle source

@api private

   # File lib/puppet/pops/loader/type_definition_instantiator.rb
90 def self.named_definition(te)
91   return 'Object' if te.is_a?(Model::LiteralHash)
92   te.is_a?(Model::AccessExpression) && (left = te.left_expr).is_a?(Model::QualifiedReference) ? left.cased_value : nil
93 end

Public Instance Methods

several_paths?() click to toggle source
   # File lib/puppet/pops/loader/type_definition_instantiator.rb
95 def several_paths?
96   false
97 end