File: static_loader.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (133 lines) | stat: -rw-r--r-- 3,238 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
# frozen_string_literal: true

# Static Loader contains constants, basic data types and other types required for the system
# to boot.
#
module Puppet::Pops
module Loader
class StaticLoader < Loader
  BUILTIN_TYPE_NAMES = %w[
    Component
    Exec
    File
    Filebucket
    Group
    Node
    Notify
    Package
    Resources
    Schedule
    Service
    Stage
    Tidy
    User
    Whit
  ].freeze

  BUILTIN_TYPE_NAMES_LC = Set.new(BUILTIN_TYPE_NAMES.map(&:downcase)).freeze

  BUILTIN_ALIASES = {
    'Data' => 'Variant[ScalarData,Undef,Hash[String,Data],Array[Data]]',
    'RichDataKey' => 'Variant[String,Numeric]',
    'RichData' => 'Variant[Scalar,SemVerRange,Binary,Sensitive,Type,TypeSet,URI,Object,Undef,Default,Hash[RichDataKey,RichData],Array[RichData]]',

    # Backward compatible aliases.
    'Puppet::LookupKey' => 'RichDataKey',
    'Puppet::LookupValue' => 'RichData'
  }.freeze

  attr_reader :loaded

  def initialize
    @loaded = {}
    @runtime_3_initialized = false
    create_built_in_types
  end

  def discover(type, error_collector = nil, name_authority = Pcore::RUNTIME_NAME_AUTHORITY)
    # Static loader only contains runtime types
    return EMPTY_ARRAY unless type == :type && name_authority == Pcore::RUNTIME_NAME_AUTHORITY

    typed_names = @loaded.keys
    block_given? ? typed_names.select { |tn| yield(tn) } : typed_names
  end

  def load_typed(typed_name)
    load_constant(typed_name)
  end

  def get_entry(typed_name)
    load_constant(typed_name)
  end

  def set_entry(typed_name, value, origin = nil)
    @loaded[typed_name] = Loader::NamedEntry.new(typed_name, value, origin)
  end

  def find(name)
    # There is nothing to search for, everything this loader knows about is already available
    nil
  end

  def parent
    nil # at top of the hierarchy
  end

  def to_s
    "(StaticLoader)"
  end

  def loaded_entry(typed_name, check_dependencies = false)
    @loaded[typed_name]
  end

  def runtime_3_init
    unless @runtime_3_initialized
      @runtime_3_initialized = true
      create_resource_type_references
    end
    nil
  end

  def register_aliases
    aliases = BUILTIN_ALIASES.map { |name, string| add_type(name, Types::PTypeAliasType.new(name, Types::TypeFactory.type_reference(string), nil)) }
    aliases.each { |type| type.resolve(self) }
  end

  private

  def load_constant(typed_name)
    @loaded[typed_name]
  end

  def create_built_in_types
    origin_uri = URI("puppet:Puppet-Type-System/Static-Loader")
    type_map = Puppet::Pops::Types::TypeParser.type_map
    type_map.each do |name, type|
      set_entry(TypedName.new(:type, name), type, origin_uri)
    end
  end

  def create_resource_type_references
    # These needs to be done quickly and we do not want to scan the file system for these
    # We are also not interested in their definition only that they exist.
    # These types are in all environments.
    #
    BUILTIN_TYPE_NAMES.each { |name| create_resource_type_reference(name) }
  end

  def add_type(name, type)
    set_entry(TypedName.new(:type, name), type)
    type
  end

  def create_resource_type_reference(name)
    add_type(name, Types::TypeFactory.resource(name))
  end

  def synchronize(&block)
    yield
  end
end
end
end