File: json_strict_literal_evaluator.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (82 lines) | stat: -rw-r--r-- 1,648 bytes parent folder | download | duplicates (3)
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
# Literal values for
#
#   * String
#   * Numbers
#   * Booleans
#   * Undef (produces nil)
#   * Array
#   * Hash where keys must be Strings
#   * QualifiedName
#
# Not considered literal:
#
#   * QualifiedReference  # i.e. File, FooBar
#   * Default is not accepted as being literal
#   * Regular Expression is not accepted as being literal
#   * Hash with non String keys
#   * String with interpolation
#
class Puppet::Pops::Evaluator::JsonStrictLiteralEvaluator
  #include Puppet::Pops::Utils

  COMMA_SEPARATOR = ', '.freeze

  def initialize
    @@literal_visitor ||= Puppet::Pops::Visitor.new(self, "literal", 0, 0)
  end

  def literal(ast)
    @@literal_visitor.visit_this_0(self, ast)
  end

  def literal_Object(o)
    throw :not_literal
  end

  def literal_Factory(o)
    literal(o.model)
  end

  def literal_Program(o)
    literal(o.body)
  end

  def literal_LiteralString(o)
    o.value
  end

  def literal_QualifiedName(o)
    o.value
  end

  def literal_LiteralNumber(o)
    o.value
  end

  def literal_LiteralBoolean(o)
    o.value
  end

  def literal_LiteralUndef(o)
    nil
  end

  def literal_ConcatenatedString(o)
    # use double quoted string value if there is no interpolation
    throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Puppet::Pops::Model::LiteralString)
    o.segments[0].value
  end

  def literal_LiteralList(o)
    o.values.map {|v| literal(v) }
  end

  def literal_LiteralHash(o)
    o.entries.reduce({}) do |result, entry|
      key = literal(entry.key)
      throw :not_literal unless key.is_a?(String)
      result[key] = literal(entry.value)
      result
    end
  end
end