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
 7 def self.create(loader, typed_name, source_ref, pp_code_string)
 8   # parse and validate
 9   parser = Parser::EvaluatingParser.new()
10   model = parser.parse_string(pp_code_string, source_ref)
11   # Only one type is allowed (and no other definitions)
12 
13   name = typed_name.name
14   case model.definitions.size
15   when 0
16     raise ArgumentError, _("The code loaded from %{source_ref} does not define the type '%{name}' - it is empty.") % { source_ref: source_ref, name: name }
17   when 1
18     # ok
19   else
20     raise ArgumentError,
21       _("The code loaded from %{source_ref} must contain only the type '%{name}' - it has additional definitions.") % { source_ref: source_ref, name: name }
22   end
23   type_definition = model.definitions[0]
24 
25   unless type_definition.is_a?(Model::TypeAlias) || type_definition.is_a?(Model::TypeDefinition)
26     raise ArgumentError,
27       _("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 }
28   end
29 
30   actual_name = type_definition.name
31   unless name == actual_name.downcase
32     raise ArgumentError,
33       _("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 }
34   end
35 
36   unless model.body == type_definition
37     raise ArgumentError,
38       _("The code loaded from %{source_ref} contains additional logic - can only contain the type '%{name}'") % { source_ref: source_ref, name: name }
39   end
40 
41   # Adapt the type definition with loader - this is used from logic contained in its body to find the
42   # loader to use when resolving contained aliases API. Such logic have a hard time finding the closure (where
43   # the loader is known - hence this mechanism
44   private_loader = loader.private_loader
45   Adapters::LoaderAdapter.adapt(type_definition).loader_name = private_loader.loader_name
46   create_runtime_type(type_definition)
47 end
create_from_model(type_definition, loader) click to toggle source
   # File lib/puppet/pops/loader/type_definition_instantiator.rb
49 def self.create_from_model(type_definition, loader)
50   typed_name = TypedName.new(:type, type_definition.name)
51   type = create_runtime_type(type_definition)
52   loader.set_entry(
53     typed_name,
54     type,
55     type_definition.locator.to_uri(type_definition))
56   type
57 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
73 def self.create_named_type(name, type_name, type_expr, name_authority)
74   case type_name
75   when 'Object'
76     # No need for an alias. The Object type itself will receive the name instead
77     unless type_expr.is_a?(Model::LiteralHash)
78       type_expr = type_expr.keys.empty? ? nil : type_expr.keys[0] unless type_expr.is_a?(Hash)
79     end
80     Types::PObjectType.new(name, type_expr)
81   when 'TypeSet'
82     # No need for an alias. The Object type itself will receive the name instead
83     type_expr = type_expr.keys.empty? ? nil : type_expr.keys[0] unless type_expr.is_a?(Hash)
84     Types::PTypeSetType.new(name, type_expr, name_authority)
85   else
86     Types::PTypeAliasType.new(name, type_expr)
87   end
88 end
create_runtime_type(type_definition) click to toggle source

@api private

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

@api private

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

@api private

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

Public Instance Methods

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