This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
MapUtils.groovy
201 lines (187 loc) · 6.4 KB
/
MapUtils.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*-
* #%L
* wcm.io
* %%
* Copyright (C) 2017 wcm.io DevOps
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.devops.jenkins.pipeline.utils.maps
import com.cloudbees.groovy.cps.NonCPS
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import io.wcm.devops.jenkins.pipeline.utils.TypeUtils
import io.wcm.devops.jenkins.pipeline.utils.logging.Logger
import static io.wcm.devops.jenkins.pipeline.utils.ConfigConstants.MAP_MERGE_MODE
/**
* Utility functions for Map objects
*/
@SuppressWarnings("UnnecessaryQualifiedReference")
class MapUtils implements Serializable {
private static final long serialVersionUID = 1L
static Logger log = new Logger(this)
static typeUtils = new TypeUtils()
static MapUtils instance
/**
* Merges 0 to n Map objects recursively into one Map
*
* Overlapping keys will handled by the defined merge mode.
* Therefore a key with ConfigConstants.MAP_MERGE_MODE has to be present
* in one of the maps at the needed level.
*
* Per default the merge mode is "Merge" which will result in
* - merged arrays (unique)
* - merged maps
* - value overwriting
*
* E.g.
* map[0] has "key" with "value"
* map[1] has "key" with "newValue"
*
* Resulting will have "key" with "newValue"
*
* When MapMergeMode is REPLACE, overlapping keys from the n+1 map will overwrite
* keys from n map
*
* When MapMergeMode is SKIP, overlapping keys from n will not be overwritten by
* keys from n+1 map
*
* @param maps 0 to n maps that have to me merged.
* @return The merged map
*/
@NonCPS
@SuppressFBWarnings('SE_NO_SERIALVERSIONID')
static transient Map merge(Map... maps) {
Map result
instance = new MapUtils()
if (maps == null || maps.length == 0) {
result = [:]
} else if (maps.length == 1) {
result = maps[0]
} else {
result = [:]
maps.each { map ->
// skip null maps
if (map == null) return
// get the merge mode from current result
MapMergeMode mergeMode = (MapMergeMode) result[MAP_MERGE_MODE] ?: null
// get the merge mode from incoming map when not in result
mergeMode = mergeMode ?: (MapMergeMode) map[MAP_MERGE_MODE] ?: null
// set default merge mode when absolutely nothing was found
mergeMode = mergeMode ?: MapMergeMode.MERGE
// walk through map
map.each { k, v ->
try {
v = v.clone()
} catch (Exception ex) {
// do nothing here
}
if (result[k] != null && typeUtils.isMap(result[k]) && typeUtils.isMap(v)) {
// unnecessary qualified reference is necessary here otherwise CPS / Sandbox will be violated
result[k] = instance.mergeMap((Map) result[k], (Map) v, mergeMode)
} else if (result[k] != null && typeUtils.isList(result[k]) && typeUtils.isList(v)) {
// execute a list merge
List existingList = (List) result[k]
List incomingList = (List) v
result[k] = instance.mergeList(existingList, incomingList, mergeMode)
} else if (result[k] != null && v != null) {
result[k] = instance.mergeValue(result[k], v, mergeMode)
}
else {
result[k] = v
}
}
}
}
result
}
/**
* Merges two maps based on the provided merge mode
* - MapMergeMode.SKIP keeps the existing map untouched and returns it
* - MapMergeMode.REPLACE = returns the incoming map since it should replace everything
* - MapMergeMode.MERGE = will merge the two maps by calling another recursion level
*
* @param existing Existing map object
* @param incoming Incoming map object
* @param mergeMode The merge mode which should apply
* @return the resulting map based on the merge mode
*
* @see MapMergeMode
*/
@NonCPS
Map mergeMap(Map existing, Map incoming, MapMergeMode mergeMode) {
switch (mergeMode) {
case MapMergeMode.SKIP: return existing
case MapMergeMode.REPLACE: return incoming
// merge all other options
default:
return MapUtils.merge(existing, incoming)
break
}
}
/**
* Merge function for simple objects like string, boolean etc.
* - MapMergeMode.MERGE and REPLACE will replace the existing value by the incoming value
* - MapMergeMode.SKIP will keep the existing value
*
* @param existing The existing value
* @param incoming The incoming value
* @param mergeMode The merge mode which should apply
* @return The resulting value based on the merge mode
*
* @see MapMergeMode
*/
@NonCPS
Object mergeValue(Object existing, Object incoming, MapMergeMode mergeMode) {
// merge mode object are protected and can not be overwritten
if (typeUtils.isMapMergeMode(existing)) {
return existing
}
switch (mergeMode) {
case MapMergeMode.SKIP: return existing
// all other options are using the incoming value
default:
return incoming
break
}
}
/**
* Merge function for list objects
* - MapMergeMode.SKIP keeps the existing list untouched and returns it
* - MapMergeMode.REPLACE = returns the incoming list
* - MapMergeMode.MERGE = will merge the two lists
*
* @param existing The existing list
* @param incoming The incoming list
* @param mergeMode The merge mode which should apply
* @return The resulting list based on the merge mode
*
* @see MapMergeMode
*/
@NonCPS
List mergeList(List existing, List incoming, MapMergeMode mergeMode) {
switch (mergeMode) {
case MapMergeMode.SKIP: return existing
case MapMergeMode.REPLACE: return incoming
case MapMergeMode.MERGE:
for (Object incomingListItem : incoming) {
if (!existing.contains(incomingListItem))
existing.add(incomingListItem)
}
return existing
default:
return existing
break
}
}
}