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

ruby-changes:26736

From: tenderlove <ko1@a...>
Date: Sat, 12 Jan 2013 09:09:08 +0900 (JST)
Subject: [ruby-changes:26736] tenderlove:r38788 (trunk): * ext/psych/lib/psych/visitors/to_ruby.rb: merge key values that

tenderlove	2013-01-12 09:00:12 +0900 (Sat, 12 Jan 2013)

  New Revision: 38788

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

  Log:
    * ext/psych/lib/psych/visitors/to_ruby.rb: merge key values that
      contain something besides a hash should be left in tact.
    
    * test/psych/test_merge_keys.rb: test for change

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/visitors/to_ruby.rb
    trunk/test/psych/test_merge_keys.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38787)
+++ ChangeLog	(revision 38788)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Jan 12 08:58:47 2013  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/visitors/to_ruby.rb: merge key values that
+	  contain something besides a hash should be left in tact.
+
+	* test/psych/test_merge_keys.rb: test for change
+
 Sat Jan 12 07:52:47 2013  Masaki Suketa <masaki.suketa@n...>
 
 	* ext/win32ole/win32ole.c (ole_set_byref): support VT_UI8|VT_BYREF,
Index: ext/psych/lib/psych/visitors/to_ruby.rb
===================================================================
--- ext/psych/lib/psych/visitors/to_ruby.rb	(revision 38787)
+++ ext/psych/lib/psych/visitors/to_ruby.rb	(revision 38788)
@@ -263,28 +263,42 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L263
       def revive_hash hash, o
         @st[o.anchor] = hash if o.anchor
 
-          o.children.each_slice(2) { |k,v|
+        o.children.each_slice(2) { |k,v|
           key = accept(k)
+          val = accept(v)
 
           if key == '<<'
             case v
             when Nodes::Alias
-              hash.merge! accept(v)
+              begin
+                hash.merge! val
+              rescue TypeError
+                hash[key] = val
+              end
             when Nodes::Sequence
-              accept(v).reverse_each do |value|
-                hash.merge! value
+              begin
+                h = {}
+                val.reverse_each do |value|
+                  h.merge! value
+                end
+                hash.merge! h
+              rescue TypeError
+                hash[key] = val
               end
             else
-              hash[key] = accept(v)
+              hash[key] = val
             end
           else
-            hash[key] = accept(v)
+            hash[key] = val
           end
 
         }
         hash
       end
 
+      def merge_key hash, key, val
+      end
+
       def revive klass, node
         s = klass.allocate
         @st[node.anchor] = s if node.anchor
Index: test/psych/test_merge_keys.rb
===================================================================
--- test/psych/test_merge_keys.rb	(revision 38787)
+++ test/psych/test_merge_keys.rb	(revision 38788)
@@ -2,6 +2,57 @@ require 'psych/helper' https://github.com/ruby/ruby/blob/trunk/test/psych/test_merge_keys.rb#L2
 
 module Psych
   class TestMergeKeys < TestCase
+    def test_merge_nil
+      yaml = <<-eoyml
+defaults: &defaults
+development:
+  <<: *defaults
+      eoyml
+      assert_equal({'<<' => nil }, Psych.load(yaml)['development'])
+    end
+
+    def test_merge_array
+      yaml = <<-eoyml
+foo: &hello
+- 1
+baz:
+  <<: *hello
+      eoyml
+      assert_equal({'<<' => [1]}, Psych.load(yaml)['baz'])
+    end
+
+    def test_merge_is_not_partial
+      yaml = <<-eoyml
+default: &default
+  hello: world
+foo: &hello
+- 1
+baz:
+  <<: [*hello, *default]
+      eoyml
+      doc = Psych.load yaml
+      refute doc['baz'].key? 'hello'
+      assert_equal({'<<' => [[1], {"hello"=>"world"}]}, Psych.load(yaml)['baz'])
+    end
+
+    def test_merge_seq_nil
+      yaml = <<-eoyml
+foo: &hello
+baz:
+  <<: [*hello]
+      eoyml
+      assert_equal({'<<' => [nil]}, Psych.load(yaml)['baz'])
+    end
+
+    def test_bad_seq_merge
+      yaml = <<-eoyml
+defaults: &defaults [1, 2, 3]
+development:
+  <<: *defaults
+      eoyml
+      assert_equal({'<<' => [1,2,3]}, Psych.load(yaml)['development'])
+    end
+
     def test_missing_merge_key
       yaml = <<-eoyml
 bar:

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

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