libo11y 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +170 -21
- data/README.md +77 -8
- data/lib/libo11y/{proc.rb → child_process.rb} +12 -2
- data/lib/libo11y/db.rb +22 -10
- data/lib/libo11y/detached.rb +241 -29
- data/lib/libo11y/http_client.rb +48 -15
- data/lib/libo11y/internal/bridges.rb +3553 -255
- data/lib/libo11y/internal/endpoint.rb +7 -1
- data/lib/libo11y/internal/env.rb +177 -59
- data/lib/libo11y/internal/error_type.rb +17 -0
- data/lib/libo11y/internal/histogram_buckets.rb +42 -0
- data/lib/libo11y/internal/http_common.rb +2 -0
- data/lib/libo11y/internal/identity.rb +10 -1
- data/lib/libo11y/internal/injected.rb +178 -0
- data/lib/libo11y/internal/logging_device.rb +141 -32
- data/lib/libo11y/internal/path_match.rb +148 -0
- data/lib/libo11y/internal/pii.rb +185 -65
- data/lib/libo11y/internal/pipeline.rb +141 -34
- data/lib/libo11y/internal/policy.rb +556 -52
- data/lib/libo11y/internal/propagators.rb +29 -6
- data/lib/libo11y/internal/quiet_inspect.rb +20 -0
- data/lib/libo11y/internal/resource.rb +187 -52
- data/lib/libo11y/internal/sdk_load.rb +64 -0
- data/lib/libo11y/internal/sql_text.rb +365 -67
- data/lib/libo11y/internal/summary.rb +2 -1
- data/lib/libo11y/options.rb +159 -1
- data/lib/libo11y/rack.rb +257 -44
- data/lib/libo11y/record.rb +70 -7
- data/lib/libo11y/redact.rb +87 -21
- data/lib/libo11y/signals.rb +70 -29
- data/lib/libo11y/spec/app_registry.rb +97 -61
- data/lib/libo11y/spec/yaml_v3.rb +179 -111
- data/lib/libo11y/telemetry.rb +315 -53
- data/lib/libo11y/testing.rb +31 -19
- data/lib/libo11y/version.rb +1 -1
- data/lib/libo11y.rb +1 -1
- metadata +94 -39
data/lib/libo11y/record.rb
CHANGED
|
@@ -1,32 +1,74 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "internal/policy"
|
|
4
|
+
|
|
3
5
|
module Libo11y
|
|
4
6
|
# Wide event accumulator attached to the current Fiber/execution context.
|
|
5
7
|
class Record
|
|
6
8
|
def initialize
|
|
7
9
|
@mutex = Mutex.new
|
|
8
10
|
@attrs = {}
|
|
11
|
+
@origin_of = {}
|
|
9
12
|
end
|
|
10
13
|
|
|
11
14
|
def set(key, value)
|
|
12
|
-
|
|
15
|
+
sk = Internal::SensitivityPolicy.normalize_key(key)
|
|
16
|
+
@mutex.synchronize do
|
|
17
|
+
@attrs[sk] = value
|
|
18
|
+
@origin_of.delete(sk)
|
|
19
|
+
end
|
|
13
20
|
self
|
|
14
21
|
end
|
|
15
22
|
|
|
16
23
|
def set_all(hash) # rubocop:disable Naming/AccessorMethodName -- it merges into the record and returns it, as set does
|
|
17
24
|
return self if hash.nil?
|
|
18
25
|
|
|
26
|
+
origin = hash.is_a?(Hash) ? hash : nil
|
|
19
27
|
@mutex.synchronize do
|
|
20
|
-
hash.each
|
|
28
|
+
hash.each do |k, v|
|
|
29
|
+
sk = Internal::SensitivityPolicy.normalize_key(k)
|
|
30
|
+
@attrs[sk] = v
|
|
31
|
+
if origin
|
|
32
|
+
@origin_of[sk] = origin
|
|
33
|
+
else
|
|
34
|
+
@origin_of.delete(sk)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
21
37
|
end
|
|
22
38
|
self
|
|
23
39
|
end
|
|
24
40
|
|
|
25
41
|
def add(key, delta = 1)
|
|
26
|
-
k = key
|
|
42
|
+
k = Internal::SensitivityPolicy.normalize_key(key)
|
|
27
43
|
@mutex.synchronize do
|
|
28
44
|
cur = @attrs[k]
|
|
29
45
|
@attrs[k] = (cur.is_a?(Numeric) ? cur : 0) + delta
|
|
46
|
+
@origin_of.delete(k)
|
|
47
|
+
end
|
|
48
|
+
self
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Keeps the larger of the value under key and value, as a Float: the
|
|
52
|
+
# slowest downstream call of a request, for example. NaN is ignored. The
|
|
53
|
+
# rule is Go's Record.Max: a Float under the key that is at least as
|
|
54
|
+
# large stays as it is, and anything else under the key, an Integer set
|
|
55
|
+
# by add included, is replaced.
|
|
56
|
+
#
|
|
57
|
+
# @raise [ArgumentError] for a value that is not a real number
|
|
58
|
+
def max(key, value)
|
|
59
|
+
# Complex is a Numeric too, and has no order.
|
|
60
|
+
raise ArgumentError, "libo11y: max takes a real number, got #{value.inspect}" unless value.is_a?(Numeric) && value.real?
|
|
61
|
+
|
|
62
|
+
candidate = value.to_f
|
|
63
|
+
return self if candidate.nan?
|
|
64
|
+
|
|
65
|
+
k = Internal::SensitivityPolicy.normalize_key(key)
|
|
66
|
+
@mutex.synchronize do
|
|
67
|
+
current = @attrs[k]
|
|
68
|
+
unless current.is_a?(Float) && current >= candidate
|
|
69
|
+
@attrs[k] = candidate
|
|
70
|
+
@origin_of.delete(k)
|
|
71
|
+
end
|
|
30
72
|
end
|
|
31
73
|
self
|
|
32
74
|
end
|
|
@@ -36,13 +78,26 @@ module Libo11y
|
|
|
36
78
|
end
|
|
37
79
|
|
|
38
80
|
def emit_event(telemetry, name, extra = nil)
|
|
39
|
-
merged =
|
|
40
|
-
extra
|
|
41
|
-
|
|
81
|
+
merged, origins = @mutex.synchronize { [@attrs.dup, @origin_of.dup] }
|
|
82
|
+
if extra
|
|
83
|
+
extra_origin = extra.is_a?(Hash) ? extra : nil
|
|
84
|
+
extra.each do |k, v|
|
|
85
|
+
sk = Internal::SensitivityPolicy.normalize_key(k)
|
|
86
|
+
merged[sk] = v
|
|
87
|
+
if extra_origin
|
|
88
|
+
origins[sk] = extra_origin
|
|
89
|
+
else
|
|
90
|
+
origins.delete(sk)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
event_name = name.to_s
|
|
95
|
+
telemetry.logger.log_with_attrs(::Logger::INFO, event_name, merged, event_name: event_name, force: true, key_origins: origins)
|
|
42
96
|
end
|
|
43
97
|
end
|
|
44
98
|
|
|
45
99
|
RECORD_FIBER_KEY = :libo11y_current_record
|
|
100
|
+
private_constant :RECORD_FIBER_KEY
|
|
46
101
|
|
|
47
102
|
def self.current_record
|
|
48
103
|
Thread.current[RECORD_FIBER_KEY]
|
|
@@ -56,10 +111,18 @@ module Libo11y
|
|
|
56
111
|
raise ArgumentError, "libo11y: start_record needs a block" unless block_given?
|
|
57
112
|
|
|
58
113
|
record = Record.new
|
|
114
|
+
with_record(record) { yield record }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Runs the block with record as the current one again, as the Rack
|
|
118
|
+
# middleware does while a response body is produced.
|
|
119
|
+
#
|
|
120
|
+
# @api private
|
|
121
|
+
def self.with_record(record)
|
|
59
122
|
previous = Thread.current[RECORD_FIBER_KEY]
|
|
60
123
|
Thread.current[RECORD_FIBER_KEY] = record
|
|
61
124
|
begin
|
|
62
|
-
yield
|
|
125
|
+
yield
|
|
63
126
|
ensure
|
|
64
127
|
Thread.current[RECORD_FIBER_KEY] = previous
|
|
65
128
|
end
|
data/lib/libo11y/redact.rb
CHANGED
|
@@ -13,6 +13,8 @@ module Libo11y
|
|
|
13
13
|
|
|
14
14
|
# Nesting deeper than this renders as REDACTED.
|
|
15
15
|
MAX_DEPTH = 32
|
|
16
|
+
# Maximum nodes examined in one Redact.render call.
|
|
17
|
+
MAX_NODES = 2048
|
|
16
18
|
|
|
17
19
|
PASSTHROUGH = 0
|
|
18
20
|
MASK = 1
|
|
@@ -59,6 +61,11 @@ module Libo11y
|
|
|
59
61
|
REDACTED.inspect
|
|
60
62
|
end
|
|
61
63
|
|
|
64
|
+
# pp prints a Struct member by member unless the class says otherwise.
|
|
65
|
+
def pretty_print(printer)
|
|
66
|
+
printer.text(inspect)
|
|
67
|
+
end
|
|
68
|
+
|
|
62
69
|
def to_json(*)
|
|
63
70
|
REDACTED.to_json(*)
|
|
64
71
|
end
|
|
@@ -91,6 +98,11 @@ module Libo11y
|
|
|
91
98
|
to_s.inspect
|
|
92
99
|
end
|
|
93
100
|
|
|
101
|
+
# pp prints a Struct member by member unless the class says otherwise.
|
|
102
|
+
def pretty_print(printer)
|
|
103
|
+
printer.text(inspect)
|
|
104
|
+
end
|
|
105
|
+
|
|
94
106
|
def to_json(*)
|
|
95
107
|
to_s.to_json(*)
|
|
96
108
|
end
|
|
@@ -156,6 +168,11 @@ module Libo11y
|
|
|
156
168
|
"#<#{self.class.name || "struct"} #{redacted_hash.map { |k, v| "#{k}=#{v.inspect}" }.join(" ")}>"
|
|
157
169
|
end
|
|
158
170
|
|
|
171
|
+
# pp prints a Struct member by member unless the class says otherwise.
|
|
172
|
+
def pretty_print(printer)
|
|
173
|
+
printer.text(inspect)
|
|
174
|
+
end
|
|
175
|
+
|
|
159
176
|
def to_json(*)
|
|
160
177
|
redacted_hash.to_json(*)
|
|
161
178
|
end
|
|
@@ -169,56 +186,105 @@ module Libo11y
|
|
|
169
186
|
value.is_a?(Secret) || value.is_a?(Masked) || value.is_a?(Fields)
|
|
170
187
|
end
|
|
171
188
|
|
|
189
|
+
# Returns [name, floor_tag] pairs for the non-omitted fields of a Fields
|
|
190
|
+
# object under floor (:passthrough, :mask, or :redact), without invoking
|
|
191
|
+
# any field getter.
|
|
192
|
+
#
|
|
193
|
+
# @api private
|
|
194
|
+
def self.field_specs(value, floor = :passthrough)
|
|
195
|
+
floor_level = floor == :passthrough ? PASSTHROUGH : LEVELS.fetch(floor)
|
|
196
|
+
levels = value.class.o11y_redact_levels
|
|
197
|
+
names = value.respond_to?(:members) ? value.members : value.instance_variables.map { |iv| iv.to_s.delete_prefix("@").to_sym }
|
|
198
|
+
names.filter_map do |name|
|
|
199
|
+
level = [levels.fetch(name.to_sym, PASSTHROUGH), floor_level].max
|
|
200
|
+
next if level == OMIT
|
|
201
|
+
|
|
202
|
+
[name, TAG_OF.fetch(level, :passthrough)]
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Reads one field of a Fields object by name.
|
|
207
|
+
#
|
|
208
|
+
# @api private
|
|
209
|
+
def self.read_field(value, name)
|
|
210
|
+
value.respond_to?(name) ? value.public_send(name) : value.instance_variable_get(:"@#{name}")
|
|
211
|
+
end
|
|
212
|
+
|
|
172
213
|
# Returns value with every Secret, Masked and Fields object inside it
|
|
173
|
-
# rendered. Hashes and Arrays are walked
|
|
174
|
-
# reference back into the value being walked
|
|
175
|
-
|
|
176
|
-
|
|
214
|
+
# rendered. Hashes and Arrays are walked within MAX_DEPTH and MAX_NODES.
|
|
215
|
+
# Nesting past MAX_DEPTH and a reference back into the value being walked
|
|
216
|
+
# (or any active ancestor passed in active) render as REDACTED. Composite
|
|
217
|
+
# entries past MAX_NODES are omitted so traversal stays bounded.
|
|
218
|
+
def self.render(value, active: nil)
|
|
219
|
+
seen = {}.compare_by_identity
|
|
220
|
+
active&.each_key { |obj| seen[obj] = true }
|
|
221
|
+
protect(value, PASSTHROUGH, 0, seen, [MAX_NODES])
|
|
177
222
|
end
|
|
178
223
|
|
|
179
224
|
# floor is the strictest protection of the fields that hold value.
|
|
180
|
-
def self.protect(value, floor, depth, active)
|
|
181
|
-
|
|
225
|
+
def self.protect(value, floor, depth, active, budget)
|
|
226
|
+
budget[0] -= 1
|
|
227
|
+
return REDACTED if budget[0].negative? || floor >= REDACT || depth > MAX_DEPTH
|
|
182
228
|
|
|
183
229
|
case value
|
|
184
230
|
when nil then nil
|
|
185
231
|
when Secret then REDACTED
|
|
186
232
|
when Masked then value.to_s
|
|
187
|
-
when Fields, Hash, Array then protect_composite(value, floor, depth, active)
|
|
233
|
+
when Fields, Hash, Array then protect_composite(value, floor, depth, active, budget)
|
|
188
234
|
else floor == MASK ? mask_string(value) : value
|
|
189
235
|
end
|
|
190
236
|
end
|
|
191
237
|
|
|
192
|
-
def self.protect_composite(value, floor, depth, active)
|
|
238
|
+
def self.protect_composite(value, floor, depth, active, budget)
|
|
193
239
|
return REDACTED if active.key?(value)
|
|
194
240
|
|
|
195
241
|
active[value] = true
|
|
196
242
|
begin
|
|
197
243
|
case value
|
|
198
|
-
when Fields then protect_fields(value, floor, depth, active)
|
|
199
|
-
when Hash then value
|
|
200
|
-
else value
|
|
244
|
+
when Fields then protect_fields(value, floor, depth, active, budget)
|
|
245
|
+
when Hash then protect_hash(value, floor, depth, active, budget)
|
|
246
|
+
else protect_array(value, floor, depth, active, budget)
|
|
201
247
|
end
|
|
202
248
|
ensure
|
|
203
249
|
active.delete(value)
|
|
204
250
|
end
|
|
205
251
|
end
|
|
206
252
|
|
|
207
|
-
def self.
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
253
|
+
def self.protect_hash(value, floor, depth, active, budget)
|
|
254
|
+
out = {}
|
|
255
|
+
value.each do |k, v|
|
|
256
|
+
break if budget[0] <= 0
|
|
257
|
+
|
|
258
|
+
out[k.to_s] = protect(v, floor, depth + 1, active, budget)
|
|
259
|
+
end
|
|
260
|
+
out
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def self.protect_array(value, floor, depth, active, budget)
|
|
264
|
+
out = []
|
|
265
|
+
value.each do |v|
|
|
266
|
+
break if budget[0] <= 0
|
|
267
|
+
|
|
268
|
+
out << protect(v, floor, depth + 1, active, budget)
|
|
269
|
+
end
|
|
270
|
+
out
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def self.protect_fields(value, floor, depth, active, budget)
|
|
274
|
+
out = {}
|
|
275
|
+
field_specs(value, TAG_OF.fetch(floor, :passthrough)).each do |name, tag|
|
|
276
|
+
break if budget[0] <= 0
|
|
213
277
|
|
|
214
|
-
out[name.to_s] = if
|
|
278
|
+
out[name.to_s] = if tag == :redact
|
|
279
|
+
budget[0] -= 1
|
|
215
280
|
REDACTED
|
|
216
281
|
else
|
|
217
|
-
|
|
218
|
-
protect(
|
|
282
|
+
level = tag == :passthrough ? PASSTHROUGH : LEVELS.fetch(tag)
|
|
283
|
+
protect(read_field(value, name), level, depth + 1, active, budget)
|
|
219
284
|
end
|
|
220
285
|
end
|
|
286
|
+
out
|
|
221
287
|
end
|
|
222
|
-
private_class_method :protect, :protect_composite, :protect_fields
|
|
288
|
+
private_class_method :protect, :protect_composite, :protect_hash, :protect_array, :protect_fields
|
|
223
289
|
end
|
|
224
290
|
end
|
data/lib/libo11y/signals.rb
CHANGED
|
@@ -4,44 +4,85 @@ module Libo11y
|
|
|
4
4
|
module Signals
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
|
-
#
|
|
8
|
-
# before_shutdown
|
|
7
|
+
# Blocks until SIGINT or SIGTERM arrives, restores the previous handlers,
|
|
8
|
+
# then runs before_shutdown and the telemetry shutdown under one shared
|
|
9
|
+
# deadline, and returns the ShutdownReport.
|
|
10
|
+
#
|
|
11
|
+
# before_shutdown is where the application stops taking work, such as
|
|
12
|
+
# stopping its HTTP server. When it is still running at the deadline,
|
|
13
|
+
# telemetry shuts down without waiting for it. When it raises, telemetry
|
|
14
|
+
# still shuts down, and the error is raised afterwards.
|
|
15
|
+
#
|
|
16
|
+
# @param timeout [Numeric, nil] seconds for both steps together. nil
|
|
17
|
+
# and a timeout that is not positive use the handle's shutdown_timeout.
|
|
18
|
+
# @raise [ArgumentError] before it waits, for a timeout that is not a
|
|
19
|
+
# finite number, for an empty list of signals, and for a
|
|
20
|
+
# before_shutdown that cannot be called
|
|
9
21
|
def shutdown_on_signals(telemetry, timeout: nil, signals: %w[INT TERM], before_shutdown: nil)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
22
|
+
# The rule of Telemetry#shutdown: nil and a timeout that is not
|
|
23
|
+
# positive give the handle's shutdown_timeout.
|
|
24
|
+
secs = telemetry.resolve_timeout(timeout)
|
|
25
|
+
names = distinct(Array(signals))
|
|
26
|
+
raise ArgumentError, "libo11y: signals must name at least one signal" if names.empty?
|
|
27
|
+
unless before_shutdown.nil? || before_shutdown.respond_to?(:call)
|
|
28
|
+
raise ArgumentError, "libo11y: before_shutdown must respond to call, got #{before_shutdown.class}"
|
|
29
|
+
end
|
|
13
30
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
31
|
+
wait_for_signal(names)
|
|
32
|
+
|
|
33
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + secs
|
|
34
|
+
error = run_before_shutdown(before_shutdown, secs)
|
|
35
|
+
report = telemetry.shutdown_by(deadline, secs)
|
|
36
|
+
raise error if error
|
|
37
|
+
|
|
38
|
+
report
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The signals without repeats. "USR2", "SIGUSR2" and the number are one
|
|
42
|
+
# signal, and a second handler for it would save the first as the one
|
|
43
|
+
# to put back. A name Ruby does not know stays, for Signal.trap to refuse.
|
|
44
|
+
def distinct(signals)
|
|
45
|
+
signals.uniq do |signal|
|
|
46
|
+
name = signal.to_s.delete_prefix("SIG")
|
|
47
|
+
Signal.list.fetch(name) { signal.is_a?(Integer) ? signal : name }
|
|
20
48
|
end
|
|
49
|
+
end
|
|
21
50
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
51
|
+
# Signal handlers may not take locks, so they write one byte to a pipe
|
|
52
|
+
# that this method reads.
|
|
53
|
+
#
|
|
54
|
+
# @raise [ArgumentError] for a signal name Ruby does not know. The
|
|
55
|
+
# handlers already replaced are put back first.
|
|
56
|
+
def wait_for_signal(signals)
|
|
57
|
+
previous = {}
|
|
58
|
+
read_io, write_io = IO.pipe
|
|
59
|
+
signals.each do |name|
|
|
60
|
+
previous[name] = Signal.trap(name) do
|
|
61
|
+
write_io.write_nonblock("x")
|
|
62
|
+
rescue IOError, SystemCallError
|
|
28
63
|
nil
|
|
29
64
|
end
|
|
30
|
-
read_io.close unless read_io.closed?
|
|
31
|
-
write_io.close unless write_io.closed?
|
|
32
65
|
end
|
|
66
|
+
read_io.read(1)
|
|
67
|
+
ensure
|
|
68
|
+
previous.each { |name, handler| Signal.trap(name, handler || "DEFAULT") }
|
|
69
|
+
read_io&.close
|
|
70
|
+
write_io&.close
|
|
71
|
+
end
|
|
33
72
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
73
|
+
# Runs the callback for at most secs seconds. Returns the error it raised, if any.
|
|
74
|
+
def run_before_shutdown(callback, secs)
|
|
75
|
+
return nil if callback.nil?
|
|
76
|
+
|
|
77
|
+
error = nil
|
|
78
|
+
worker = Thread.new do
|
|
79
|
+
callback.call
|
|
80
|
+
rescue StandardError => e
|
|
81
|
+
error = e
|
|
41
82
|
end
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
83
|
+
worker.report_on_exception = false
|
|
84
|
+
worker.join(secs)
|
|
85
|
+
error
|
|
45
86
|
end
|
|
46
87
|
end
|
|
47
88
|
end
|
|
@@ -116,22 +116,39 @@ module Libo11y
|
|
|
116
116
|
|
|
117
117
|
private
|
|
118
118
|
|
|
119
|
-
def decode_node(raw
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
def decode_node(raw)
|
|
120
|
+
node, entered = enter_aliases(raw)
|
|
121
|
+
yield node
|
|
122
|
+
ensure
|
|
123
|
+
unwind_aliases(entered) if entered
|
|
124
|
+
end
|
|
122
125
|
|
|
123
|
-
|
|
124
|
-
|
|
126
|
+
def unwind_aliases(entered)
|
|
127
|
+
return if entered.empty?
|
|
125
128
|
|
|
126
|
-
|
|
127
|
-
@expanding
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
129
|
+
@alias_depth -= entered.length
|
|
130
|
+
entered.each { |aid| @expanding.delete(aid) }
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def enter_aliases(raw)
|
|
134
|
+
entered = []
|
|
135
|
+
node = raw
|
|
136
|
+
loop do
|
|
137
|
+
count
|
|
138
|
+
break unless node.is_a?(YamlV3::AliasNode)
|
|
139
|
+
|
|
140
|
+
aid = node.object_id
|
|
141
|
+
raise YamlV3::SyntaxError, "anchor '#{node.anchor}' value contains itself" if @expanding[aid]
|
|
142
|
+
|
|
143
|
+
node = @aliases.fetch(node)
|
|
144
|
+
@expanding[aid] = true
|
|
145
|
+
@alias_depth += 1
|
|
146
|
+
entered << aid
|
|
134
147
|
end
|
|
148
|
+
[node, entered]
|
|
149
|
+
rescue StandardError
|
|
150
|
+
unwind_aliases(entered)
|
|
151
|
+
raise
|
|
135
152
|
end
|
|
136
153
|
|
|
137
154
|
def resolve(node)
|
|
@@ -196,68 +213,85 @@ module Libo11y
|
|
|
196
213
|
end
|
|
197
214
|
end
|
|
198
215
|
|
|
199
|
-
def decode_mapping(raw,
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
end
|
|
207
|
-
return if duplicates?(node)
|
|
208
|
-
|
|
209
|
-
merge_node = nil
|
|
210
|
-
has_merge = false
|
|
211
|
-
node.pairs.each do |k, v|
|
|
212
|
-
if YamlV3.merge_key?(k)
|
|
213
|
-
merge_node = v
|
|
214
|
-
has_merge = true
|
|
216
|
+
def decode_mapping(raw, &)
|
|
217
|
+
stack = [[:enter_map, raw, nil, false]]
|
|
218
|
+
active_aliases = []
|
|
219
|
+
begin
|
|
220
|
+
while (frame = stack.pop)
|
|
221
|
+
if frame[0] == :exit_aliases
|
|
222
|
+
unwind_aliases(active_aliases.pop)
|
|
215
223
|
next
|
|
216
224
|
end
|
|
217
|
-
|
|
218
|
-
next if name.nil?
|
|
219
|
-
|
|
220
|
-
if merged
|
|
221
|
-
next if merged[name]
|
|
222
|
-
|
|
223
|
-
merged[name] = true
|
|
224
|
-
end
|
|
225
|
-
on_key.call(name, v)
|
|
225
|
+
process_mapping_frame(frame, stack, active_aliases, &)
|
|
226
226
|
end
|
|
227
|
-
|
|
227
|
+
ensure
|
|
228
|
+
unwind_aliases(active_aliases.pop) until active_aliases.empty?
|
|
228
229
|
end
|
|
229
230
|
end
|
|
230
231
|
|
|
231
|
-
def
|
|
232
|
-
|
|
233
|
-
if
|
|
234
|
-
parent.pairs.each do |pair|
|
|
235
|
-
ks = decode_node(pair[0]) do |n|
|
|
236
|
-
next nil unless n.is_a?(YamlV3::ScalarNode)
|
|
232
|
+
def process_mapping_frame(frame, stack, active_aliases, &)
|
|
233
|
+
_, raw, merged, verify_merge = frame
|
|
234
|
+
raise YamlV3::SyntaxError, "map merge requires map or sequence of maps as the value" if verify_merge && !merge_map?(raw)
|
|
237
235
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
end
|
|
236
|
+
node, entered = enter_aliases(raw)
|
|
237
|
+
unless entered.empty?
|
|
238
|
+
active_aliases << entered
|
|
239
|
+
stack << [:exit_aliases]
|
|
243
240
|
end
|
|
241
|
+
return if null_or_wrong_scalar?(node)
|
|
242
|
+
return (@wrong_type = true) unless node.is_a?(YamlV3::MappingNode)
|
|
243
|
+
return if duplicates?(node)
|
|
244
|
+
|
|
245
|
+
merge_node, has_merge = emit_mapping_pairs(node, merged, &)
|
|
246
|
+
enqueue_merge(stack, node, merge_node, merged) if has_merge && !merge_node.nil?
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def emit_mapping_pairs(node, merged)
|
|
250
|
+
merge_node = nil
|
|
251
|
+
has_merge = false
|
|
252
|
+
node.pairs.each do |k, v|
|
|
253
|
+
if YamlV3.merge_key?(k)
|
|
254
|
+
merge_node = v
|
|
255
|
+
has_merge = true
|
|
256
|
+
next
|
|
257
|
+
end
|
|
258
|
+
name = decode_string(k)
|
|
259
|
+
next if name.nil? || merged&.[](name)
|
|
244
260
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
target.is_a?(YamlV3::MappingNode)
|
|
261
|
+
merged[name] = true if merged
|
|
262
|
+
yield name, v
|
|
248
263
|
end
|
|
264
|
+
[merge_node, has_merge]
|
|
265
|
+
end
|
|
249
266
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
267
|
+
def merge_map?(node)
|
|
268
|
+
target = node.is_a?(YamlV3::AliasNode) ? @aliases[node] : node
|
|
269
|
+
target.is_a?(YamlV3::MappingNode)
|
|
270
|
+
end
|
|
253
271
|
|
|
254
|
-
|
|
255
|
-
|
|
272
|
+
def enqueue_merge(stack, parent, source, merged)
|
|
273
|
+
keys = merged || collect_merge_keys(parent)
|
|
274
|
+
if source.is_a?(YamlV3::SequenceNode)
|
|
275
|
+
source.items.reverse_each { |item| stack << [:enter_map, item, keys, true] }
|
|
256
276
|
return
|
|
257
277
|
end
|
|
258
|
-
raise YamlV3::SyntaxError, "map merge requires map or sequence of maps as the value" unless
|
|
278
|
+
raise YamlV3::SyntaxError, "map merge requires map or sequence of maps as the value" unless merge_map?(source)
|
|
279
|
+
|
|
280
|
+
stack << [:enter_map, source, keys, false]
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def collect_merge_keys(parent)
|
|
284
|
+
keys = {}
|
|
285
|
+
parent.pairs.each do |pair|
|
|
286
|
+
ks = decode_node(pair[0]) do |n|
|
|
287
|
+
next nil unless n.is_a?(YamlV3::ScalarNode)
|
|
259
288
|
|
|
260
|
-
|
|
289
|
+
v = resolve(n)
|
|
290
|
+
!v.is_null && v.is_string ? v.text : nil
|
|
291
|
+
end
|
|
292
|
+
keys[ks] = true unless ks.nil?
|
|
293
|
+
end
|
|
294
|
+
keys
|
|
261
295
|
end
|
|
262
296
|
|
|
263
297
|
def decode_struct(raw, fields, &on_field)
|
|
@@ -319,15 +353,17 @@ module Libo11y
|
|
|
319
353
|
end
|
|
320
354
|
|
|
321
355
|
def parse(text)
|
|
322
|
-
docs = YamlV3.parse_documents(text)
|
|
356
|
+
docs, later_syntax = YamlV3.parse_documents(text)
|
|
323
357
|
raise ParseError, "empty" if docs.empty?
|
|
324
358
|
|
|
325
359
|
first = docs[0]
|
|
326
360
|
decoder = Decoder.new(first.aliases)
|
|
327
361
|
decoder.count
|
|
328
362
|
reg = decoder.decode_registry(first.root)
|
|
363
|
+
# A problem in the first document wins over anything in a later one.
|
|
329
364
|
problem = decoder.problem
|
|
330
365
|
raise ParseError, problem unless problem.nil?
|
|
366
|
+
raise ParseError.new("syntax", later_syntax.message) if later_syntax
|
|
331
367
|
raise ParseError, "second-document" if docs.length > 1
|
|
332
368
|
|
|
333
369
|
reg
|