Last active
January 7, 2016 13:37
-
-
Save dmahapatro/a8ddcb7a2f9f4373dfda to your computer and use it in GitHub Desktop.
Deep Merge Groovy Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map.metaClass.merge = { Map rhs -> | |
def lhs = delegate // or delegate.clone() to make delegate immutable | |
rhs.each { k, v -> lhs[k] = lhs[k] in Map ? lhs[k].merge(v) : (lhs[k] != null ? lhs[k] && v : v) } | |
lhs | |
} | |
def a = [ foo: [ foo: true, bar: true ] ] | |
def b = [ foo: [ bar: false, baz: true ] ] | |
assert a.merge(b) == [ foo: [ foo: true, bar: false, baz: true ] ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment