动态替换map中的变量

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
def replaceVariableInMap(def oriValue, def key, def value) {
if (oriValue instanceof String) {
return oriValue.replace("\${${key}}", value.toString())
} else if (oriValue instanceof List) {
def tmpList = []
oriValue.each { cf ->
tmpList.add(replaceVariableInMap(cf, key, value))
}
return tmpList
} else if (oriValue instanceof Map) {
def tmpMap = [:]
oriValue.each { k, v->
tmpMap.put(k, replaceVariableInMap(v, key, value))
}
return tmpMap
} else {
return oriValue
}
}

def testMap = [general: [branch: "\${branch}"], stages: [tasks: [repos: [url: "\${url}", branch: "\${branch}", id: "domain"]]]]

def params = [branch: "test_branch", url: "https://git.test.com"]

params.each {k, v ->
testMap = replaceVariableInMap(testMap, k, v)
}
println testMap