[前][次][番号順一覧][スレッド一覧]

ruby-changes:35233

From: tenderlove <ko1@a...>
Date: Sat, 30 Aug 2014 06:02:33 +0900 (JST)
Subject: [ruby-changes:35233] tenderlove:r47315 (trunk): * ext/psych/lib/psych/visitors/to_ruby.rb: quoted "<<" strings

tenderlove	2014-08-30 06:02:19 +0900 (Sat, 30 Aug 2014)

  New Revision: 47315

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47315

  Log:
    * ext/psych/lib/psych/visitors/to_ruby.rb: quoted "<<" strings
      should not be treated as merge keys.
    * ext/psych/lib/psych/visitors/yaml_tree.rb: hashes with keys
      containing "<<" should roundtrip.
    * test/psych/test_merge_keys.rb: test for change. Fixes GH #203

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/visitors/to_ruby.rb
    trunk/ext/psych/lib/psych/visitors/yaml_tree.rb
    trunk/test/psych/test_merge_keys.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47314)
+++ ChangeLog	(revision 47315)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Aug 30 06:00:26 2014  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/visitors/to_ruby.rb: quoted "<<" strings
+	  should not be treated as merge keys.
+	* ext/psych/lib/psych/visitors/yaml_tree.rb: hashes with keys
+	  containing "<<" should roundtrip.
+	* test/psych/test_merge_keys.rb: test for change. Fixes GH #203
+
 Fri Aug 29 17:56:44 2014  SHIBATA Hiroshi  <shibata.hiroshi@g...>
 
 	* test/net/imap/test_imap_response_parser.rb: removed needless code.
Index: ext/psych/lib/psych/visitors/yaml_tree.rb
===================================================================
--- ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 47314)
+++ ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 47315)
@@ -16,10 +16,12 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L16
         def initialize
           @obj_to_id   = {}
           @obj_to_node = {}
+          @targets     = []
           @counter     = 0
         end
 
         def register target, node
+          @targets << target
           @obj_to_node[target.object_id] = node
         end
 
@@ -289,6 +291,11 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L291
           quote = false
         elsif o =~ /\n/
           style = Nodes::Scalar::LITERAL
+        elsif o == '<<'
+          style = Nodes::Scalar::SINGLE_QUOTED
+          tag   = 'tag:yaml.org,2002:str'
+          plain = false
+          quote = false
         elsif o =~ /^\W[^"]*$/
           style = Nodes::Scalar::DOUBLE_QUOTED
         else
Index: ext/psych/lib/psych/visitors/to_ruby.rb
===================================================================
--- ext/psych/lib/psych/visitors/to_ruby.rb	(revision 47314)
+++ ext/psych/lib/psych/visitors/to_ruby.rb	(revision 47315)
@@ -305,7 +305,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L305
           key = accept(k)
           val = accept(v)
 
-          if key == '<<'
+          if key == '<<' && k.tag != "tag:yaml.org,2002:str"
             case v
             when Nodes::Alias
               begin
Index: test/psych/test_merge_keys.rb
===================================================================
--- test/psych/test_merge_keys.rb	(revision 47314)
+++ test/psych/test_merge_keys.rb	(revision 47315)
@@ -6,6 +6,26 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_merge_keys.rb#L6
       attr_reader :bar
     end
 
+    def test_roundtrip_with_chevron_key
+      h = {}
+      v = { 'a' => h, '<<' => h }
+      assert_cycle v
+    end
+
+    def test_explicit_string
+      doc = Psych.load <<-eoyml
+a: &me { hello: world }
+b: { !!str '<<': *me }
+eoyml
+      expected = {
+        "a" => { "hello" => "world" },
+        "b" => {
+          "<<" => { "hello" => "world" }
+        }
+      }
+      assert_equal expected, doc
+    end
+
     def test_mergekey_with_object
       s = <<-eoyml
 foo: &foo

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]