ruby-changes:61574
From: Jean <ko1@a...>
Date: Fri, 5 Jun 2020 11:50:18 +0900 (JST)
Subject: [ruby-changes:61574] 591a3326e3 (master): [ruby/psych] Refactor `symbolize_names`
https://git.ruby-lang.org/ruby.git/commit/?id=591a3326e3 From 591a3326e33889858276d66b157125273491edee Mon Sep 17 00:00:00 2001 From: Jean Boussier <jean.boussier@g...> Date: Wed, 24 Jul 2019 15:20:40 -0400 Subject: [ruby/psych] Refactor `symbolize_names` https://github.com/ruby/psych/commit/3e472ab2d7 diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb index c719b03..67024fb 100644 --- a/ext/psych/lib/psych.rb +++ b/ext/psych/lib/psych.rb @@ -276,8 +276,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych.rb#L276 result = parse(yaml, filename: filename) return fallback unless result - result = result.to_ruby if result - symbolize_names!(result) if symbolize_names + result = result.to_ruby(symbolize_names: symbolize_names) if result result end @@ -353,12 +352,11 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych.rb#L352 permitted_symbols.map(&:to_s)) scanner = ScalarScanner.new class_loader visitor = if aliases - Visitors::ToRuby.new scanner, class_loader + Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names else - Visitors::NoAliasRuby.new scanner, class_loader + Visitors::NoAliasRuby.new scanner, class_loader, symbolize_names: symbolize_names end result = visitor.accept result - symbolize_names!(result) if symbolize_names result end @@ -604,19 +602,6 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych.rb#L602 @dump_tags[klass] = tag end - def self.symbolize_names!(result) - case result - when Hash - result.keys.each do |key| - result[key.to_sym] = symbolize_names!(result.delete(key)) - end - when Array - result.map! { |r| symbolize_names!(r) } - end - result - end - private_class_method :symbolize_names! - # Workaround for emulating `warn '...', uplevel: 1` in Ruby 2.4 or lower. def self.warn_with_uplevel(message, uplevel: 1) at = parse_caller(caller[uplevel]).join(':') diff --git a/ext/psych/lib/psych/nodes/node.rb b/ext/psych/lib/psych/nodes/node.rb index f59fb89..7e414e7 100644 --- a/ext/psych/lib/psych/nodes/node.rb +++ b/ext/psych/lib/psych/nodes/node.rb @@ -46,8 +46,8 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/nodes/node.rb#L46 # Convert this node to Ruby. # # See also Psych::Visitors::ToRuby - def to_ruby - Visitors::ToRuby.create.accept(self) + def to_ruby(symbolize_names: false) + Visitors::ToRuby.create(symbolize_names: symbolize_names).accept(self) end alias :transform :to_ruby diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index b72fb4a..3021aa7 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -12,20 +12,21 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L12 ### # This class walks a YAML AST, converting each node to Ruby class ToRuby < Psych::Visitors::Visitor - def self.create + def self.create(symbolize_names: false) class_loader = ClassLoader.new scanner = ScalarScanner.new class_loader - new(scanner, class_loader) + new(scanner, class_loader, symbolize_names: symbolize_names) end attr_reader :class_loader - def initialize ss, class_loader + def initialize ss, class_loader, symbolize_names: false super() @st = {} @ss = ss @domain_types = Psych.domain_types @class_loader = class_loader + @symbolize_names = symbolize_names end def accept target @@ -336,7 +337,12 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L337 SHOVEL = '<<' def revive_hash hash, o o.children.each_slice(2) { |k,v| - key = deduplicate(accept(k)) + key = accept(k) + if @symbolize_names + key = key.to_sym + else + key = deduplicate(key) + end val = accept(v) if key == SHOVEL && k.tag != "tag:yaml.org,2002:str" -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/