deep_merge and hiera

169 views
Skip to first unread message

Fred Gansevles

unread,
May 20, 2013, 5:53:20 AM5/20/13
to puppet...@googlegroups.com
I was playing around with the new deep-merge hiera feature to check it's usability

I found some unexpected behaviour with array resolution

the following yaml files get parsed

top.yaml:
  ----
  arr:
    - 1
  hsh:
    arr:
      - 1

mid.yaml:
  ----
  arr:
    - 2
  hsh:
    arr:
      - 2

bot.yaml:
  ----
  arr:
    - 3
  hsh:
    arr:
      - 3

with :merge_behavior: deep (or deeper) I get the following result

$ hiera -a arr
[1, 2, 3]                     # I expected this

$ hiera -h hsh
{"arr"=>[3, 2, 1]}         # I expected {"arr"=>[1, 2, 3]}

I've located the curl-pit in the deep_merge gem, but I am not sure if this must be fixed there (the deep_merge gem is probably used by others)

any thoughts?

Fred.

Fred Gansevles

unread,
May 20, 2013, 12:27:01 PM5/20/13
to puppet...@googlegroups.com
After some more digging, I found that the logic of
Hash.merge is reversed with the logic of DeepHash.merge,
so to get the expected results, the hiera/backend.rb must swap its hashes

When you change the merge_answer in hiera/backand.rb
from:

    def merge_answer(left,right)
      case Config[:merge_behavior]
      when :deeper,'deeper'
        left.deep_merge!(right)
      when :deep,'deep'
        left.deep_merge(right)
      else # Native and undefined
        left.merge(right)
      end
    end

to:

    def merge_answer(left,right)
      case Config[:merge_behavior]
      when :deeper,'deeper'
        right.deep_merge!(left)         # <- swapped left/right
      when :deep,'deep'
        right.deep_merge(left)          # <- swapped left/right
      else # Native and undefined
        left.merge(right)
      end
    end

then the results are what I expected

Fred.


Op maandag 20 mei 2013 11:53:20 UTC+2 schreef Fred Gansevles het volgende:
Reply all
Reply to author
Forward
0 new messages