1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00

Merge pull request #701 from marcusholl/pr/defensiveCopy

Provide a deep config copy from ConfigHelper.use()
This commit is contained in:
Marcus Holl 2019-05-15 15:06:09 +02:00 committed by GitHub
commit ed328b75af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 1 deletions

View File

@ -127,9 +127,11 @@ class ConfigurationHelper implements Serializable {
handleValidationFailures()
MapUtils.traverse(config, { v -> (v instanceof GString) ? v.toString() : v })
if(config.verbose) step.echo "[${name}] Configuration: ${config}"
return config
return MapUtils.deepCopy(config)
}
/* private */ def getConfigPropertyNested(key) {
return getConfigPropertyNested(config, key)
}

View File

@ -62,4 +62,45 @@ class MapUtils implements Serializable {
}
m.putAll(updates)
}
/*
* Provides a new map with the same content like the original map.
* Nested Collections and Maps are copied. Values with are not
* Collections/Maps are not copied/cloned.
* <paranoia>&/ltThe keys are also not copied/cloned, even if they are
* Maps or Collections;paranoia>
*/
static deepCopy(Map original) {
Map copy = [:]
for (def e : original.entrySet()) {
if(e.value == null) {
copy.put(e.key, e.value)
} else {
copy.put(e.key, deepCopy(e.value))
}
}
copy
}
/* private */ static deepCopy(Set original) {
Set copy = []
for(def e : original)
copy << deepCopy(e)
copy
}
/* private */ static deepCopy(List original) {
List copy = []
for(def e : original)
copy << deepCopy(e)
copy
}
/*
* In fact not a copy, but a catch all for everything not matching
* with the other signatures
*/
/* private */ static deepCopy(def original) {
original
}
}

View File

@ -50,4 +50,35 @@ class MapUtilsTest {
MapUtils.traverse(m, { s -> (s.startsWith('x')) ? "replaced" : s})
assert m == [a: 'replaced', m: [b: 'replaced', c: 'otherString']]
}
@Test
void testDeepCopy() {
List l = ['a', 'b', 'c']
def original = [
list: l,
set: (Set)['1', '2'],
nextLevel: [
list: ['x', 'y'],
duplicate: l,
set: (Set)[9, 8, 7]
]
]
def copy = MapUtils.deepCopy(original)
assert ! copy.is(original)
assert ! copy.list.is(original.list)
assert ! copy.set.is(original.set)
assert ! copy.nextLevel.list.is(original.nextLevel.list)
assert ! copy.nextLevel.set.is(original.nextLevel.set)
assert ! copy.nextLevel.duplicate.is(original.nextLevel.duplicate)
// Within the original identical list is used twice, but the
// assuption is that there are different lists in the copy.
assert ! copy.nextLevel.duplicate.is(copy.list)
assert copy == original
}
}