Last active
January 6, 2016 22:43
-
-
Save timyates/da6715be9407a93533d5 to your computer and use it in GitHub Desktop.
Map deepMerge (less readable remix)
This file contains 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
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) |
This file contains 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.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