-
Notifications
You must be signed in to change notification settings - Fork 1
/
groovyHelpers.groovy
31 lines (28 loc) · 1.08 KB
/
groovyHelpers.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
library (
base: "driver",
author: "jvm33",
category: "zwave",
description: "Some useful groovy tools",
name: "groovyHelpeTools",
namespace: "zwaveTools",
documentationLink: "https://github.com/jvmahon/HubitatDriverTools",
version:"0.0.1",
dependencies: "",
librarySource:""
)
// nested map merge.
// deep merges the Map specified on the left into the one on the right. https://e.printstacktrace.blog/how-to-merge-two-maps-in-groovy/
def nestedMerge(Map lhs, Map rhs) {
return rhs.inject(lhs.clone()) { map, entry ->
if (map[entry.key] instanceof Map && entry.value instanceof Map) {
map[entry.key] = nestedMerge(map[entry.key], entry.value)
} else if (map[entry.key] instanceof Collection && entry.value instanceof Collection) {
map[entry.key] += entry.value
} else {
map[entry.key] = entry.value
}
return map
}
}
Map.metaClass.nestedMerge << { Map rhs -> deepMerge(delegate, rhs) }
// usage Map e = a.deepMerge(b)