Skip to content

Instantly share code, notes, and snippets.

@dmahapatro
Last active January 7, 2016 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmahapatro/a8ddcb7a2f9f4373dfda to your computer and use it in GitHub Desktop.
Save dmahapatro/a8ddcb7a2f9f4373dfda to your computer and use it in GitHub Desktop.
Deep Merge Groovy Map
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