psych 2.0.12 → 5.2.3
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 +5 -5
- data/CONTRIBUTING.md +24 -0
- data/{ext/psych/yaml/LICENSE → LICENSE} +9 -7
- data/README.md +80 -0
- data/ext/psych/depend +14 -0
- data/ext/psych/extconf.rb +43 -28
- data/ext/psych/psych.c +6 -4
- data/ext/psych/psych.h +0 -3
- data/ext/psych/psych_emitter.c +165 -132
- data/ext/psych/psych_parser.c +298 -331
- data/ext/psych/psych_to_ruby.c +0 -5
- data/ext/psych/psych_yaml_tree.c +0 -13
- data/lib/psych/class_loader.rb +11 -8
- data/lib/psych/coder.rb +1 -0
- data/lib/psych/core_ext.rb +3 -19
- data/lib/psych/exception.rb +17 -2
- data/lib/psych/handler.rb +8 -2
- data/lib/psych/handlers/document_stream.rb +2 -1
- data/lib/psych/handlers/recorder.rb +2 -1
- data/lib/psych/json/ruby_events.rb +1 -0
- data/lib/psych/json/stream.rb +3 -2
- data/lib/psych/json/tree_builder.rb +2 -1
- data/lib/psych/json/yaml_events.rb +1 -0
- data/lib/psych/nodes/alias.rb +3 -0
- data/lib/psych/nodes/document.rb +3 -0
- data/lib/psych/nodes/mapping.rb +3 -0
- data/lib/psych/nodes/node.rb +25 -5
- data/lib/psych/nodes/scalar.rb +4 -1
- data/lib/psych/nodes/sequence.rb +3 -0
- data/lib/psych/nodes/stream.rb +3 -0
- data/lib/psych/nodes.rb +8 -7
- data/lib/psych/omap.rb +1 -0
- data/lib/psych/parser.rb +14 -0
- data/lib/psych/scalar_scanner.rb +41 -49
- data/lib/psych/set.rb +1 -0
- data/lib/psych/stream.rb +1 -0
- data/lib/psych/streaming.rb +1 -0
- data/lib/psych/syntax_error.rb +2 -1
- data/lib/psych/tree_builder.rb +48 -7
- data/lib/psych/versions.rb +10 -0
- data/lib/psych/visitors/depth_first.rb +1 -0
- data/lib/psych/visitors/emitter.rb +1 -0
- data/lib/psych/visitors/json_tree.rb +2 -1
- data/lib/psych/visitors/to_ruby.rb +64 -33
- data/lib/psych/visitors/visitor.rb +18 -3
- data/lib/psych/visitors/yaml_tree.rb +128 -149
- data/lib/psych/visitors.rb +7 -6
- data/lib/psych/y.rb +1 -0
- data/lib/psych.rb +360 -95
- metadata +36 -169
- data/.autotest +0 -18
- data/.gemtest +0 -0
- data/.travis.yml +0 -11
- data/CHANGELOG.rdoc +0 -562
- data/Manifest.txt +0 -112
- data/README.rdoc +0 -71
- data/Rakefile +0 -74
- data/ext/psych/yaml/api.c +0 -1415
- data/ext/psych/yaml/config.h +0 -10
- data/ext/psych/yaml/dumper.c +0 -394
- data/ext/psych/yaml/emitter.c +0 -2329
- data/ext/psych/yaml/loader.c +0 -459
- data/ext/psych/yaml/parser.c +0 -1370
- data/ext/psych/yaml/reader.c +0 -469
- data/ext/psych/yaml/scanner.c +0 -3583
- data/ext/psych/yaml/writer.c +0 -141
- data/ext/psych/yaml/yaml.h +0 -1971
- data/ext/psych/yaml/yaml_private.h +0 -664
- data/lib/psych/deprecated.rb +0 -85
- data/test/psych/handlers/test_recorder.rb +0 -25
- data/test/psych/helper.rb +0 -114
- data/test/psych/json/test_stream.rb +0 -109
- data/test/psych/nodes/test_enumerable.rb +0 -43
- data/test/psych/test_alias_and_anchor.rb +0 -96
- data/test/psych/test_array.rb +0 -57
- data/test/psych/test_boolean.rb +0 -36
- data/test/psych/test_class.rb +0 -36
- data/test/psych/test_coder.rb +0 -184
- data/test/psych/test_date_time.rb +0 -38
- data/test/psych/test_deprecated.rb +0 -214
- data/test/psych/test_document.rb +0 -46
- data/test/psych/test_emitter.rb +0 -93
- data/test/psych/test_encoding.rb +0 -259
- data/test/psych/test_exception.rb +0 -157
- data/test/psych/test_hash.rb +0 -94
- data/test/psych/test_json_tree.rb +0 -65
- data/test/psych/test_marshalable.rb +0 -54
- data/test/psych/test_merge_keys.rb +0 -180
- data/test/psych/test_nil.rb +0 -18
- data/test/psych/test_null.rb +0 -19
- data/test/psych/test_numeric.rb +0 -45
- data/test/psych/test_object.rb +0 -44
- data/test/psych/test_object_references.rb +0 -71
- data/test/psych/test_omap.rb +0 -75
- data/test/psych/test_parser.rb +0 -339
- data/test/psych/test_psych.rb +0 -168
- data/test/psych/test_safe_load.rb +0 -97
- data/test/psych/test_scalar.rb +0 -11
- data/test/psych/test_scalar_scanner.rb +0 -106
- data/test/psych/test_serialize_subclasses.rb +0 -38
- data/test/psych/test_set.rb +0 -49
- data/test/psych/test_stream.rb +0 -93
- data/test/psych/test_string.rb +0 -226
- data/test/psych/test_struct.rb +0 -49
- data/test/psych/test_symbol.rb +0 -25
- data/test/psych/test_tainted.rb +0 -130
- data/test/psych/test_to_yaml_properties.rb +0 -63
- data/test/psych/test_tree_builder.rb +0 -79
- data/test/psych/test_yaml.rb +0 -1288
- data/test/psych/test_yamldbm.rb +0 -193
- data/test/psych/test_yamlstore.rb +0 -85
- data/test/psych/visitors/test_depth_first.rb +0 -49
- data/test/psych/visitors/test_emitter.rb +0 -144
- data/test/psych/visitors/test_to_ruby.rb +0 -326
- data/test/psych/visitors/test_yaml_tree.rb +0 -173
@@ -1,6 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative '../tree_builder'
|
3
|
+
require_relative '../scalar_scanner'
|
4
|
+
require_relative '../class_loader'
|
4
5
|
|
5
6
|
module Psych
|
6
7
|
module Visitors
|
@@ -14,29 +15,25 @@ module Psych
|
|
14
15
|
class YAMLTree < Psych::Visitors::Visitor
|
15
16
|
class Registrar # :nodoc:
|
16
17
|
def initialize
|
17
|
-
@obj_to_id = {}
|
18
|
-
@obj_to_node = {}
|
19
|
-
@targets = []
|
18
|
+
@obj_to_id = {}.compare_by_identity
|
19
|
+
@obj_to_node = {}.compare_by_identity
|
20
20
|
@counter = 0
|
21
21
|
end
|
22
22
|
|
23
23
|
def register target, node
|
24
|
-
@
|
25
|
-
@obj_to_node[target.object_id] = node
|
24
|
+
@obj_to_node[target] = node
|
26
25
|
end
|
27
26
|
|
28
27
|
def key? target
|
29
|
-
@obj_to_node.key? target
|
30
|
-
rescue NoMethodError
|
31
|
-
false
|
28
|
+
@obj_to_node.key? target
|
32
29
|
end
|
33
30
|
|
34
31
|
def id_for target
|
35
|
-
@obj_to_id[target
|
32
|
+
@obj_to_id[target] ||= (@counter += 1)
|
36
33
|
end
|
37
34
|
|
38
35
|
def node_for target
|
39
|
-
@obj_to_node[target
|
36
|
+
@obj_to_node[target]
|
40
37
|
end
|
41
38
|
end
|
42
39
|
|
@@ -51,15 +48,6 @@ module Psych
|
|
51
48
|
new(emitter, ss, options)
|
52
49
|
end
|
53
50
|
|
54
|
-
def self.new emitter = nil, ss = nil, options = nil
|
55
|
-
return super if emitter && ss && options
|
56
|
-
|
57
|
-
if $VERBOSE
|
58
|
-
warn "This API is deprecated, please pass an emitter, scalar scanner, and options or call #{self}.create() (#{caller.first})"
|
59
|
-
end
|
60
|
-
create emitter, ss
|
61
|
-
end
|
62
|
-
|
63
51
|
def initialize emitter, ss, options
|
64
52
|
super()
|
65
53
|
@started = false
|
@@ -69,6 +57,15 @@ module Psych
|
|
69
57
|
@ss = ss
|
70
58
|
@options = options
|
71
59
|
@line_width = options[:line_width]
|
60
|
+
if @line_width && @line_width < 0
|
61
|
+
if @line_width == -1
|
62
|
+
# Treat -1 as unlimited line-width, same as libyaml does.
|
63
|
+
@line_width = nil
|
64
|
+
else
|
65
|
+
fail(ArgumentError, "Invalid line_width #{@line_width}, must be non-negative or -1 for unlimited.")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
@stringify_names = options[:stringify_names]
|
72
69
|
@coders = []
|
73
70
|
|
74
71
|
@dispatch_cache = Hash.new do |h,klass|
|
@@ -79,7 +76,7 @@ module Psych
|
|
79
76
|
raise(TypeError, "Can't dump #{target.class}") unless method
|
80
77
|
|
81
78
|
h[klass] = method
|
82
|
-
end
|
79
|
+
end.compare_by_identity
|
83
80
|
end
|
84
81
|
|
85
82
|
def start encoding = Nodes::Stream::UTF8
|
@@ -129,24 +126,6 @@ module Psych
|
|
129
126
|
return @emitter.alias anchor
|
130
127
|
end
|
131
128
|
|
132
|
-
if target.respond_to?(:to_yaml)
|
133
|
-
begin
|
134
|
-
loc = target.method(:to_yaml).source_location.first
|
135
|
-
if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/
|
136
|
-
unless target.respond_to?(:encode_with)
|
137
|
-
if $VERBOSE
|
138
|
-
warn "implementing to_yaml is deprecated, please implement \"encode_with\""
|
139
|
-
end
|
140
|
-
|
141
|
-
target.to_yaml(:nodump => true)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
rescue
|
145
|
-
# public_method or source_location might be overridden,
|
146
|
-
# and it's OK to skip it since it's only to emit a warning
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
129
|
if target.respond_to?(:encode_with)
|
151
130
|
dump_coder target
|
152
131
|
else
|
@@ -155,7 +134,7 @@ module Psych
|
|
155
134
|
end
|
156
135
|
|
157
136
|
def visit_Psych_Omap o
|
158
|
-
seq = @emitter.start_sequence(nil, '
|
137
|
+
seq = @emitter.start_sequence(nil, 'tag:yaml.org,2002:omap', false, Nodes::Sequence::BLOCK)
|
159
138
|
register(o, seq)
|
160
139
|
|
161
140
|
o.each { |k,v| visit_Hash k => v }
|
@@ -181,6 +160,8 @@ module Psych
|
|
181
160
|
@emitter.end_mapping
|
182
161
|
end
|
183
162
|
|
163
|
+
alias :visit_Delegator :visit_Object
|
164
|
+
|
184
165
|
def visit_Struct o
|
185
166
|
tag = ['!ruby/struct', o.class.name].compact.join(':')
|
186
167
|
|
@@ -196,53 +177,24 @@ module Psych
|
|
196
177
|
end
|
197
178
|
|
198
179
|
def visit_Exception o
|
199
|
-
|
200
|
-
|
201
|
-
@emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK
|
202
|
-
|
203
|
-
{
|
204
|
-
'message' => private_iv_get(o, 'mesg'),
|
205
|
-
'backtrace' => private_iv_get(o, 'backtrace'),
|
206
|
-
}.each do |k,v|
|
207
|
-
next unless v
|
208
|
-
@emitter.scalar k, nil, nil, true, false, Nodes::Scalar::ANY
|
209
|
-
accept v
|
210
|
-
end
|
211
|
-
|
212
|
-
dump_ivars o
|
213
|
-
|
214
|
-
@emitter.end_mapping
|
180
|
+
dump_exception o, o.message.to_s
|
215
181
|
end
|
216
182
|
|
217
183
|
def visit_NameError o
|
218
|
-
|
219
|
-
|
220
|
-
@emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK
|
221
|
-
|
222
|
-
{
|
223
|
-
'message' => o.message.to_s,
|
224
|
-
'backtrace' => private_iv_get(o, 'backtrace'),
|
225
|
-
}.each do |k,v|
|
226
|
-
next unless v
|
227
|
-
@emitter.scalar k, nil, nil, true, false, Nodes::Scalar::ANY
|
228
|
-
accept v
|
229
|
-
end
|
230
|
-
|
231
|
-
dump_ivars o
|
232
|
-
|
233
|
-
@emitter.end_mapping
|
184
|
+
dump_exception o, o.message.to_s
|
234
185
|
end
|
235
186
|
|
236
187
|
def visit_Regexp o
|
237
188
|
register o, @emitter.scalar(o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY)
|
238
189
|
end
|
239
190
|
|
191
|
+
def visit_Date o
|
192
|
+
register o, visit_Integer(o.gregorian)
|
193
|
+
end
|
194
|
+
|
240
195
|
def visit_DateTime o
|
241
|
-
|
242
|
-
|
243
|
-
else
|
244
|
-
o.strftime("%Y-%m-%d %H:%M:%S.%9N %:z".freeze)
|
245
|
-
end
|
196
|
+
t = o.italy
|
197
|
+
formatted = format_time t, t.offset.zero?
|
246
198
|
tag = '!ruby/object:DateTime'
|
247
199
|
register o, @emitter.scalar(formatted, nil, tag, false, false, Nodes::Scalar::ANY)
|
248
200
|
end
|
@@ -280,7 +232,6 @@ module Psych
|
|
280
232
|
end
|
281
233
|
alias :visit_TrueClass :visit_Integer
|
282
234
|
alias :visit_FalseClass :visit_Integer
|
283
|
-
alias :visit_Date :visit_Integer
|
284
235
|
|
285
236
|
def visit_Float o
|
286
237
|
if o.nan?
|
@@ -304,29 +255,31 @@ module Psych
|
|
304
255
|
tag = nil
|
305
256
|
|
306
257
|
if binary?(o)
|
307
|
-
o = [o].pack('
|
258
|
+
o = [o].pack('m0')
|
308
259
|
tag = '!binary' # FIXME: change to below when syck is removed
|
309
260
|
#tag = 'tag:yaml.org,2002:binary'
|
310
261
|
style = Nodes::Scalar::LITERAL
|
311
262
|
plain = false
|
312
263
|
quote = false
|
313
|
-
elsif o
|
264
|
+
elsif o.match?(/\n(?!\Z)/) # match \n except blank line at the end of string
|
314
265
|
style = Nodes::Scalar::LITERAL
|
315
266
|
elsif o == '<<'
|
316
267
|
style = Nodes::Scalar::SINGLE_QUOTED
|
317
268
|
tag = 'tag:yaml.org,2002:str'
|
318
269
|
plain = false
|
319
270
|
quote = false
|
271
|
+
elsif o == 'y' || o == 'Y' || o == 'n' || o == 'N'
|
272
|
+
style = Nodes::Scalar::DOUBLE_QUOTED
|
320
273
|
elsif @line_width && o.length > @line_width
|
321
274
|
style = Nodes::Scalar::FOLDED
|
322
|
-
elsif o
|
275
|
+
elsif o.match?(/^[^[:word:]][^"]*$/)
|
323
276
|
style = Nodes::Scalar::DOUBLE_QUOTED
|
324
|
-
elsif not String === @ss.tokenize(o)
|
277
|
+
elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/.match?(o)
|
325
278
|
style = Nodes::Scalar::SINGLE_QUOTED
|
326
279
|
end
|
327
280
|
|
328
281
|
is_primitive = o.class == ::String
|
329
|
-
ivars =
|
282
|
+
ivars = is_primitive ? [] : o.instance_variables
|
330
283
|
|
331
284
|
if ivars.empty?
|
332
285
|
unless is_primitive
|
@@ -336,7 +289,7 @@ module Psych
|
|
336
289
|
end
|
337
290
|
@emitter.scalar o, nil, tag, plain, quote, style
|
338
291
|
else
|
339
|
-
maptag = '!ruby/string'
|
292
|
+
maptag = '!ruby/string'.dup
|
340
293
|
maptag << ":#{o.class}" unless o.class == ::String
|
341
294
|
|
342
295
|
register o, @emitter.start_mapping(nil, maptag, false, Nodes::Mapping::BLOCK)
|
@@ -371,7 +324,7 @@ module Psych
|
|
371
324
|
if o.class == ::Hash
|
372
325
|
register(o, @emitter.start_mapping(nil, nil, true, Psych::Nodes::Mapping::BLOCK))
|
373
326
|
o.each do |k,v|
|
374
|
-
accept k
|
327
|
+
accept(@stringify_names && Symbol === k ? k.to_s : k)
|
375
328
|
accept v
|
376
329
|
end
|
377
330
|
@emitter.end_mapping
|
@@ -384,7 +337,7 @@ module Psych
|
|
384
337
|
register(o, @emitter.start_mapping(nil, '!set', false, Psych::Nodes::Mapping::BLOCK))
|
385
338
|
|
386
339
|
o.each do |k,v|
|
387
|
-
accept k
|
340
|
+
accept(@stringify_names && Symbol === k ? k.to_s : k)
|
388
341
|
accept v
|
389
342
|
end
|
390
343
|
|
@@ -393,14 +346,18 @@ module Psych
|
|
393
346
|
|
394
347
|
def visit_Array o
|
395
348
|
if o.class == ::Array
|
396
|
-
|
397
|
-
o.each { |c| accept c }
|
398
|
-
@emitter.end_sequence
|
349
|
+
visit_Enumerator o
|
399
350
|
else
|
400
351
|
visit_array_subclass o
|
401
352
|
end
|
402
353
|
end
|
403
354
|
|
355
|
+
def visit_Enumerator o
|
356
|
+
register o, @emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK)
|
357
|
+
o.each { |c| accept c }
|
358
|
+
@emitter.end_sequence
|
359
|
+
end
|
360
|
+
|
404
361
|
def visit_NilClass o
|
405
362
|
@emitter.scalar('', nil, 'tag:yaml.org,2002:null', true, false, Nodes::Scalar::ANY)
|
406
363
|
end
|
@@ -426,15 +383,9 @@ module Psych
|
|
426
383
|
end
|
427
384
|
|
428
385
|
private
|
429
|
-
# FIXME: Remove the index and count checks in Psych 3.0
|
430
|
-
NULL = "\x00"
|
431
|
-
BINARY_RANGE = "\x00-\x7F"
|
432
|
-
WS_RANGE = "^ -~\t\r\n"
|
433
386
|
|
434
387
|
def binary? string
|
435
|
-
|
436
|
-
string.index(NULL) ||
|
437
|
-
string.count(BINARY_RANGE, WS_RANGE).fdiv(string.length) > 0.3
|
388
|
+
string.encoding == Encoding::ASCII_8BIT && !string.ascii_only?
|
438
389
|
end
|
439
390
|
|
440
391
|
def visit_array_subclass o
|
@@ -475,15 +426,6 @@ module Psych
|
|
475
426
|
node = @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK)
|
476
427
|
register(o, node)
|
477
428
|
|
478
|
-
# Dump the elements
|
479
|
-
accept 'elements'
|
480
|
-
@emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
|
481
|
-
o.each do |k,v|
|
482
|
-
accept k
|
483
|
-
accept v
|
484
|
-
end
|
485
|
-
@emitter.end_mapping
|
486
|
-
|
487
429
|
# Dump the ivars
|
488
430
|
accept 'ivars'
|
489
431
|
@emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
|
@@ -493,6 +435,15 @@ module Psych
|
|
493
435
|
end
|
494
436
|
@emitter.end_mapping
|
495
437
|
|
438
|
+
# Dump the elements
|
439
|
+
accept 'elements'
|
440
|
+
@emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
|
441
|
+
o.each do |k,v|
|
442
|
+
accept k
|
443
|
+
accept v
|
444
|
+
end
|
445
|
+
@emitter.end_mapping
|
446
|
+
|
496
447
|
@emitter.end_mapping
|
497
448
|
else
|
498
449
|
tag = "!ruby/hash:#{o.class}"
|
@@ -509,46 +460,30 @@ module Psych
|
|
509
460
|
def dump_list o
|
510
461
|
end
|
511
462
|
|
512
|
-
|
513
|
-
|
514
|
-
def format_time time
|
515
|
-
formatted = time.strftime("%Y-%m-%d %H:%M:%S.%9N")
|
463
|
+
def dump_exception o, msg
|
464
|
+
tag = ['!ruby/exception', o.class.name].join ':'
|
516
465
|
|
517
|
-
|
518
|
-
formatted += " Z"
|
519
|
-
else
|
520
|
-
zone = time.strftime('%z')
|
521
|
-
formatted += " #{zone[0,3]}:#{zone[3,5]}"
|
522
|
-
end
|
466
|
+
@emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK
|
523
467
|
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
def format_time time
|
528
|
-
if time.utc?
|
529
|
-
time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")
|
530
|
-
else
|
531
|
-
time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")
|
532
|
-
end
|
468
|
+
if msg
|
469
|
+
@emitter.scalar 'message', nil, nil, true, false, Nodes::Scalar::ANY
|
470
|
+
accept msg
|
533
471
|
end
|
472
|
+
|
473
|
+
@emitter.scalar 'backtrace', nil, nil, true, false, Nodes::Scalar::ANY
|
474
|
+
accept o.backtrace
|
475
|
+
|
476
|
+
dump_ivars o
|
477
|
+
|
478
|
+
@emitter.end_mapping
|
534
479
|
end
|
535
480
|
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
if $VERBOSE
|
542
|
-
warn "#{loc}: to_yaml_properties is deprecated, please implement \"encode_with(coder)\""
|
543
|
-
end
|
544
|
-
return target.to_yaml_properties
|
545
|
-
end
|
546
|
-
rescue
|
547
|
-
# public_method or source_location might be overridden,
|
548
|
-
# and it's OK to skip it since it's only to emit a warning.
|
481
|
+
def format_time time, utc = time.utc?
|
482
|
+
if utc
|
483
|
+
time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")
|
484
|
+
else
|
485
|
+
time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")
|
549
486
|
end
|
550
|
-
|
551
|
-
is_primitive ? [] : target.instance_variables
|
552
487
|
end
|
553
488
|
|
554
489
|
def register target, yaml_obj
|
@@ -566,21 +501,21 @@ module Psych
|
|
566
501
|
|
567
502
|
c = Psych::Coder.new(tag)
|
568
503
|
o.encode_with(c)
|
569
|
-
emit_coder c
|
504
|
+
emit_coder c, o
|
570
505
|
end
|
571
506
|
|
572
|
-
def emit_coder c
|
507
|
+
def emit_coder c, o
|
573
508
|
case c.type
|
574
509
|
when :scalar
|
575
|
-
@emitter.scalar c.scalar, nil, c.tag, c.tag.nil?, false,
|
510
|
+
@emitter.scalar c.scalar, nil, c.tag, c.tag.nil?, false, c.style
|
576
511
|
when :seq
|
577
|
-
@emitter.start_sequence nil, c.tag, c.tag.nil?,
|
512
|
+
@emitter.start_sequence nil, c.tag, c.tag.nil?, c.style
|
578
513
|
c.seq.each do |thing|
|
579
514
|
accept thing
|
580
515
|
end
|
581
516
|
@emitter.end_sequence
|
582
517
|
when :map
|
583
|
-
@emitter.start_mapping
|
518
|
+
register o, @emitter.start_mapping(nil, c.tag, c.implicit, c.style)
|
584
519
|
c.map.each do |k,v|
|
585
520
|
accept k
|
586
521
|
accept v
|
@@ -592,13 +527,57 @@ module Psych
|
|
592
527
|
end
|
593
528
|
|
594
529
|
def dump_ivars target
|
595
|
-
|
596
|
-
|
597
|
-
ivars.each do |iv|
|
530
|
+
target.instance_variables.each do |iv|
|
598
531
|
@emitter.scalar("#{iv.to_s.sub(/^@/, '')}", nil, nil, true, false, Nodes::Scalar::ANY)
|
599
532
|
accept target.instance_variable_get(iv)
|
600
533
|
end
|
601
534
|
end
|
602
535
|
end
|
536
|
+
|
537
|
+
class RestrictedYAMLTree < YAMLTree
|
538
|
+
DEFAULT_PERMITTED_CLASSES = {
|
539
|
+
TrueClass => true,
|
540
|
+
FalseClass => true,
|
541
|
+
NilClass => true,
|
542
|
+
Integer => true,
|
543
|
+
Float => true,
|
544
|
+
String => true,
|
545
|
+
Array => true,
|
546
|
+
Hash => true,
|
547
|
+
}.compare_by_identity.freeze
|
548
|
+
|
549
|
+
def initialize emitter, ss, options
|
550
|
+
super
|
551
|
+
@permitted_classes = DEFAULT_PERMITTED_CLASSES.dup
|
552
|
+
Array(options[:permitted_classes]).each do |klass|
|
553
|
+
@permitted_classes[klass] = true
|
554
|
+
end
|
555
|
+
@permitted_symbols = {}.compare_by_identity
|
556
|
+
Array(options[:permitted_symbols]).each do |symbol|
|
557
|
+
@permitted_symbols[symbol] = true
|
558
|
+
end
|
559
|
+
@aliases = options.fetch(:aliases, false)
|
560
|
+
end
|
561
|
+
|
562
|
+
def accept target
|
563
|
+
if !@aliases && @st.key?(target)
|
564
|
+
raise BadAlias, "Tried to dump an aliased object"
|
565
|
+
end
|
566
|
+
|
567
|
+
unless Symbol === target || @permitted_classes[target.class]
|
568
|
+
raise DisallowedClass.new('dump', target.class.name || target.class.inspect)
|
569
|
+
end
|
570
|
+
|
571
|
+
super
|
572
|
+
end
|
573
|
+
|
574
|
+
def visit_Symbol sym
|
575
|
+
unless @permitted_classes[Symbol] || @permitted_symbols[sym]
|
576
|
+
raise DisallowedClass.new('dump', "Symbol(#{sym.inspect})")
|
577
|
+
end
|
578
|
+
|
579
|
+
super
|
580
|
+
end
|
581
|
+
end
|
603
582
|
end
|
604
583
|
end
|
data/lib/psych/visitors.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative 'visitors/visitor'
|
3
|
+
require_relative 'visitors/to_ruby'
|
4
|
+
require_relative 'visitors/emitter'
|
5
|
+
require_relative 'visitors/yaml_tree'
|
6
|
+
require_relative 'visitors/json_tree'
|
7
|
+
require_relative 'visitors/depth_first'
|
data/lib/psych/y.rb
CHANGED