File: leaf.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 (84 lines) | stat: -rw-r--r-- 2,284 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
# frozen_string_literal: true

# The base class for all of the leaves of the parse trees.  These
# basically just have types and values.  Both of these parameters
# are simple values, not AST objects.
#
class Puppet::Parser::AST::Leaf < Puppet::Parser::AST
  attr_accessor :value, :type

  # Return our value.
  def evaluate(scope)
    @value
  end

  def match(value)
    @value == value
  end

  def to_s
    @value.to_s unless @value.nil?
  end

  def initialize(value: nil, file: nil, line: nil, pos: nil)
    @value = value
    super(file: file, line: line, pos: pos)
  end
end

# Host names, either fully qualified or just the short name, or even a regex
#
class Puppet::Parser::AST::HostName < Puppet::Parser::AST::Leaf
  def initialize(value: nil, file: nil, line: nil, pos: nil)
    super(value: value, file: file, line: line, pos: pos)

    # Note that this is an AST::Regex, not a Regexp
    unless @value.is_a?(Regex)
      @value = @value.to_s.downcase
      if @value =~ /[^-\w.]/
        raise Puppet::DevError, _("'%{value}' is not a valid hostname") % { value: @value }
      end
    end
  end

  # implementing eql? and hash so that when an HostName is stored
  # in a hash it has the same hashing properties as the underlying value
  def eql?(value)
    @value.eql?(value.is_a?(HostName) ? value.value : value)
  end

  def hash
    @value.hash
  end
end

class Puppet::Parser::AST::Regex < Puppet::Parser::AST::Leaf
  def initialize(value: nil, file: nil, line: nil, pos: nil)
    super(value: value, file: file, line: line, pos: pos)

    # transform value from hash options unless it is already a regular expression
    @value = Regexp.new(@value) unless @value.is_a?(Regexp)
  end

  # we're returning self here to wrap the regexp and to be used in places
  # where a string would have been used, without modifying any client code.
  # For instance, in many places we have the following code snippet:
  #  val = @val.safeevaluate(@scope)
  #  if val.match(otherval)
  #      ...
  #  end
  # this way, we don't have to modify this test specifically for handling
  # regexes.
  #
  def evaluate(scope)
    self
  end

  def match(value)
    @value.match(value)
  end

  def to_s
    Puppet::Pops::Types::PRegexpType.regexp_to_s_with_delimiters(@value)
  end
end