Permalink
Browse files

* 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

fixes #114
  • Loading branch information...
1 parent cf82e48 commit 2025502c6a525ade0bd50d28877609eb2e020d30 @tenderlove tenderlove committed Jan 12, 2013
Showing with 78 additions and 6 deletions.
  1. +7 −0 CHANGELOG.rdoc
  2. +20 −6 lib/psych/visitors/to_ruby.rb
  3. +51 −0 test/psych/test_merge_keys.rb
View
@@ -1,3 +1,10 @@
+Sat Jan 12 08:58:47 2013 Aaron Patterson <aaron@tenderlovemaking.com>
+
+ * 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
+
Thu Jan 10 04:23:07 2013 Aaron Patterson <aaron@tenderlovemaking.com>
* ext/psych/lib/psych/scalar_scanner.rb: strip trailing dots from
@@ -263,28 +263,42 @@ def register_empty object
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
@@ -2,6 +2,57 @@
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:

1 comment on commit 2025502

@robmathews

thanks tl.

Please sign in to comment.