|
21 | 21 | require_relative '../../puppet_x/stdlib'
|
22 | 22 | require 'date'
|
23 | 23 |
|
24 |
| -module PuppetX::Stdlib |
25 |
| - # The Dumper class was blindly copied from https://github.com/emancu/toml-rb/blob/v2.0.1/lib/toml-rb/dumper.rb |
26 |
| - # This allows us to use the `to_toml` function as a `Deferred` function because the `toml-rb` gem is usually |
27 |
| - # installed on the agent and the `Deferred` function gets evaluated before the catalog gets applied. This |
28 |
| - # makes it in most scenarios impossible to install the gem before it is used. |
29 |
| - class TomlDumper |
30 |
| - attr_reader :toml_str |
31 |
| - |
32 |
| - def initialize(hash) |
33 |
| - @toml_str = '' |
34 |
| - |
35 |
| - visit(hash, []) |
36 |
| - end |
| 24 | +# The Dumper class was blindly copied from https://github.com/emancu/toml-rb/blob/v2.0.1/lib/toml-rb/dumper.rb |
| 25 | +# This allows us to use the `to_toml` function as a `Deferred` function because the `toml-rb` gem is usually |
| 26 | +# installed on the agent and the `Deferred` function gets evaluated before the catalog gets applied. This |
| 27 | +# makes it in most scenarios impossible to install the gem before it is used. |
| 28 | +class PuppetX::Stdlib::TomlDumper |
| 29 | + attr_reader :toml_str |
37 | 30 |
|
38 |
| - private |
| 31 | + def initialize(hash) |
| 32 | + @toml_str = '' |
39 | 33 |
|
40 |
| - def visit(hash, prefix, extra_brackets = false) |
41 |
| - simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash |
| 34 | + visit(hash, []) |
| 35 | + end |
42 | 36 |
|
43 |
| - print_prefix prefix, extra_brackets if prefix.any? && (simple_pairs.any? || hash.empty?) |
| 37 | + private |
44 | 38 |
|
45 |
| - dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix |
46 |
| - end |
| 39 | + def visit(hash, prefix, extra_brackets = false) |
| 40 | + simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash |
47 | 41 |
|
48 |
| - def sort_pairs(hash) |
49 |
| - nested_pairs = [] |
50 |
| - simple_pairs = [] |
51 |
| - table_array_pairs = [] |
52 |
| - |
53 |
| - hash.keys.sort.each do |key| |
54 |
| - val = hash[key] |
55 |
| - element = [key, val] |
56 |
| - |
57 |
| - if val.is_a? Hash |
58 |
| - nested_pairs << element |
59 |
| - elsif val.is_a?(Array) && val.first.is_a?(Hash) |
60 |
| - table_array_pairs << element |
61 |
| - else |
62 |
| - simple_pairs << element |
63 |
| - end |
64 |
| - end |
| 42 | + print_prefix prefix, extra_brackets if prefix.any? && (simple_pairs.any? || hash.empty?) |
65 | 43 |
|
66 |
| - [simple_pairs, nested_pairs, table_array_pairs] |
67 |
| - end |
68 |
| - |
69 |
| - def dump_pairs(simple, nested, table_array, prefix = []) |
70 |
| - # First add simple pairs, under the prefix |
71 |
| - dump_simple_pairs simple |
72 |
| - dump_nested_pairs nested, prefix |
73 |
| - dump_table_array_pairs table_array, prefix |
74 |
| - end |
| 44 | + dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix |
| 45 | + end |
75 | 46 |
|
76 |
| - def dump_simple_pairs(simple_pairs) |
77 |
| - simple_pairs.each do |key, val| |
78 |
| - key = quote_key(key) unless bare_key? key |
79 |
| - @toml_str << "#{key} = #{to_toml(val)}\n" |
80 |
| - end |
81 |
| - end |
| 47 | + def sort_pairs(hash) |
| 48 | + nested_pairs = [] |
| 49 | + simple_pairs = [] |
| 50 | + table_array_pairs = [] |
82 | 51 |
|
83 |
| - def dump_nested_pairs(nested_pairs, prefix) |
84 |
| - nested_pairs.each do |key, val| |
85 |
| - key = quote_key(key) unless bare_key? key |
| 52 | + hash.keys.sort.each do |key| |
| 53 | + val = hash[key] |
| 54 | + element = [key, val] |
86 | 55 |
|
87 |
| - visit val, prefix + [key], false |
| 56 | + if val.is_a? Hash |
| 57 | + nested_pairs << element |
| 58 | + elsif val.is_a?(Array) && val.first.is_a?(Hash) |
| 59 | + table_array_pairs << element |
| 60 | + else |
| 61 | + simple_pairs << element |
88 | 62 | end
|
89 | 63 | end
|
90 | 64 |
|
91 |
| - def dump_table_array_pairs(table_array_pairs, prefix) |
92 |
| - table_array_pairs.each do |key, val| |
93 |
| - key = quote_key(key) unless bare_key? key |
94 |
| - aux_prefix = prefix + [key] |
| 65 | + [simple_pairs, nested_pairs, table_array_pairs] |
| 66 | + end |
95 | 67 |
|
96 |
| - val.each do |child| |
97 |
| - print_prefix aux_prefix, true |
98 |
| - args = sort_pairs(child) << aux_prefix |
| 68 | + def dump_pairs(simple, nested, table_array, prefix = []) |
| 69 | + # First add simple pairs, under the prefix |
| 70 | + dump_simple_pairs simple |
| 71 | + dump_nested_pairs nested, prefix |
| 72 | + dump_table_array_pairs table_array, prefix |
| 73 | + end |
99 | 74 |
|
100 |
| - dump_pairs(*args) |
101 |
| - end |
102 |
| - end |
| 75 | + def dump_simple_pairs(simple_pairs) |
| 76 | + simple_pairs.each do |key, val| |
| 77 | + key = quote_key(key) unless bare_key? key |
| 78 | + @toml_str << "#{key} = #{to_toml(val)}\n" |
103 | 79 | end
|
| 80 | + end |
104 | 81 |
|
105 |
| - def print_prefix(prefix, extra_brackets = false) |
106 |
| - new_prefix = prefix.join('.') |
107 |
| - new_prefix = '[' + new_prefix + ']' if extra_brackets |
| 82 | + def dump_nested_pairs(nested_pairs, prefix) |
| 83 | + nested_pairs.each do |key, val| |
| 84 | + key = quote_key(key) unless bare_key? key |
108 | 85 |
|
109 |
| - @toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals |
| 86 | + visit val, prefix + [key], false |
110 | 87 | end
|
| 88 | + end |
111 | 89 |
|
112 |
| - def to_toml(obj) |
113 |
| - if obj.is_a?(Time) || obj.is_a?(DateTime) |
114 |
| - obj.strftime('%Y-%m-%dT%H:%M:%SZ') |
115 |
| - elsif obj.is_a?(Date) |
116 |
| - obj.strftime('%Y-%m-%d') |
117 |
| - elsif obj.is_a? Regexp |
118 |
| - obj.inspect.inspect |
119 |
| - elsif obj.is_a? String |
120 |
| - obj.inspect.gsub(/\\(#[$@{])/, '\1') # rubocop:disable Style/RegexpLiteral |
121 |
| - else |
122 |
| - obj.inspect |
| 90 | + def dump_table_array_pairs(table_array_pairs, prefix) |
| 91 | + table_array_pairs.each do |key, val| |
| 92 | + key = quote_key(key) unless bare_key? key |
| 93 | + aux_prefix = prefix + [key] |
| 94 | + |
| 95 | + val.each do |child| |
| 96 | + print_prefix aux_prefix, true |
| 97 | + args = sort_pairs(child) << aux_prefix |
| 98 | + |
| 99 | + dump_pairs(*args) |
123 | 100 | end
|
124 | 101 | end
|
| 102 | + end |
125 | 103 |
|
126 |
| - def bare_key?(key) |
127 |
| - # rubocop:disable Style/RegexpLiteral |
128 |
| - !!key.to_s.match(/^[a-zA-Z0-9_-]*$/) |
129 |
| - # rubocop:enable Style/RegexpLiteral |
130 |
| - end |
| 104 | + def print_prefix(prefix, extra_brackets = false) |
| 105 | + new_prefix = prefix.join('.') |
| 106 | + new_prefix = '[' + new_prefix + ']' if extra_brackets |
131 | 107 |
|
132 |
| - def quote_key(key) |
133 |
| - '"' + key.gsub('"', '\\"') + '"' |
| 108 | + @toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals |
| 109 | + end |
| 110 | + |
| 111 | + def to_toml(obj) |
| 112 | + case obj |
| 113 | + when Time, DateTime |
| 114 | + obj.strftime('%Y-%m-%dT%H:%M:%SZ') |
| 115 | + when Date |
| 116 | + obj.strftime('%Y-%m-%d') |
| 117 | + when Regexp |
| 118 | + obj.inspect.inspect |
| 119 | + when String |
| 120 | + obj.inspect.gsub(/\\(#[$@{])/, '\1') # rubocop:disable Style/RegexpLiteral |
| 121 | + else |
| 122 | + obj.inspect |
134 | 123 | end
|
135 | 124 | end
|
| 125 | + |
| 126 | + def bare_key?(key) |
| 127 | + # rubocop:disable Style/RegexpLiteral |
| 128 | + !!key.to_s.match(/^[a-zA-Z0-9_-]*$/) |
| 129 | + # rubocop:enable Style/RegexpLiteral |
| 130 | + end |
| 131 | + |
| 132 | + def quote_key(key) |
| 133 | + '"' + key.gsub('"', '\\"') + '"' |
| 134 | + end |
136 | 135 | end
|
0 commit comments