Skip to content

Instantly share code, notes, and snippets.

@timyates
Last active January 6, 2016 22:43
Show Gist options
  • Save timyates/da6715be9407a93533d5 to your computer and use it in GitHub Desktop.
Save timyates/da6715be9407a93533d5 to your computer and use it in GitHub Desktop.
Map deepMerge (less readable remix)
def a = [foo: [foo: true, bar: true]]
def b = [foo: [bar: false, baz: true]]
Map.metaClass.deepMerge = { Map other ->
(delegate.keySet() + other.keySet()).collectEntries { key ->
def (d, o) = [delegate, other]*.get(key)
if(d instanceof Map && o instanceof Map) {
[key, d.deepMerge(o)]
}
else {
[key, o == null ? d : o]
}
}
}
a.deepMerge(b)
Map.metaClass.deepMerge = { Map other ->
[delegate, other]*.keySet().sum().collectEntries { k ->
[delegate, other]*.get(k).with { dv, ov ->
[k, [dv,ov].every { it instanceof Map } ? dv.deepMerge(ov) : (ov == null ? dv : ov)]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment