diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2019-07-24 16:01:20 -0400 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2019-11-28 09:35:26 +0000 |
commit | 7dae24894df275549c37b2d810541a7e7ea306b3 (patch) | |
tree | 7392678721cd2a71c216bfc1598ee10a2133231d /lib | |
parent | 04f97f7a56d1f0db987d8c6d6ba1b18a77a7c5e9 (diff) | |
download | psych-7dae24894df275549c37b2d810541a7e7ea306b3.zip |
Implement `freeze` option for Pysch.load
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 68452e5..a922f90 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -32,16 +32,18 @@ module Psych def accept target result = super - return result if @domain_types.empty? || !target.tag - key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:') - key = "tag:#{key}" unless key =~ /^(?:tag:|x-private)/ + unless @domain_types.empty? || !target.tag + key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:') + key = "tag:#{key}" unless key =~ /^(?:tag:|x-private)/ - if @domain_types.key? key - value, block = @domain_types[key] - return block.call value, result + if @domain_types.key? key + value, block = @domain_types[key] + result = block.call value, result + end end + result = deduplicate(result).freeze if @freeze result end @@ -341,7 +343,7 @@ module Psych key = accept(k) if @symbolize_names key = key.to_sym - else + elsif !@freeze key = deduplicate(key) end val = accept(v) @@ -378,6 +380,8 @@ module Psych if RUBY_VERSION < '2.7' def deduplicate key if key.is_a?(String) + # It is important to untaint the string, otherwise it won't + # be deduplicated into an fstring, but simply frozen. -(key.untaint) else key |