mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-02-05 13:25:19 +02:00
move method to MapUtils (#709)
* move method to MapUtils * use MapUtils * fix indent * Update src/com/sap/piper/MapUtils.groovy * Update MapUtils.groovy * remove obsolete test case * fix typo
This commit is contained in:
parent
ed328b75af
commit
9db487d189
@ -6,6 +6,8 @@ import com.sap.piper.GenerateDocumentation
|
||||
import java.util.regex.Matcher
|
||||
import groovy.text.StreamingTemplateEngine
|
||||
|
||||
import com.sap.piper.MapUtils
|
||||
|
||||
//
|
||||
// Collects helper functions for rendering the documentation
|
||||
//
|
||||
@ -375,14 +377,6 @@ class Helper {
|
||||
return mappings
|
||||
}
|
||||
|
||||
static getValue(Map config, List pPath) {
|
||||
def p = config[pPath.head()]
|
||||
if(pPath.size() == 1) return p // there is no tail
|
||||
if(p in Map) getValue(p, pPath.tail())
|
||||
return null // there is a remaining path which could not be resolved.
|
||||
// the value we are looking for does not exist.
|
||||
}
|
||||
|
||||
static resolveDocuRelevantSteps(GroovyScriptEngine gse, File stepsDir) {
|
||||
|
||||
def docuRelevantSteps = []
|
||||
@ -641,7 +635,7 @@ def handleStep(stepName, prepareDefaultValuesStep, gse, customDefaults) {
|
||||
|
||||
it ->
|
||||
|
||||
def defaultValue = Helper.getValue(defaultConfig, it.tokenize('/'))
|
||||
def defaultValue = MapUtils.getByPath(defaultConfig, it)
|
||||
|
||||
def parameterProperties = [
|
||||
defaultValue: defaultValue,
|
||||
@ -676,7 +670,7 @@ def handleStep(stepName, prepareDefaultValuesStep, gse, customDefaults) {
|
||||
[
|
||||
dependentParameterKey: dependentParameterKey,
|
||||
key: possibleValue,
|
||||
value: Helper.getValue(defaultConfig.get(possibleValue), k.tokenize('/'))
|
||||
value: MapUtils.getByPath(defaultConfig.get(possibleValue), k)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -63,6 +63,17 @@ class MapUtils implements Serializable {
|
||||
m.putAll(updates)
|
||||
}
|
||||
|
||||
static private def getByPath(Map m, def key) {
|
||||
List path = key in CharSequence ? key.tokenize('/') : key
|
||||
|
||||
def value = m.get(path.head())
|
||||
|
||||
if (path.size() == 1) return value
|
||||
if (value in Map) return getByPath(value, path.tail())
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/*
|
||||
* Provides a new map with the same content like the original map.
|
||||
* Nested Collections and Maps are copied. Values with are not
|
||||
|
@ -3,6 +3,9 @@ package com.sap.piper
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
|
||||
import static org.hamcrest.Matchers.is
|
||||
import static org.junit.Assert.assertThat
|
||||
|
||||
class MapUtilsTest {
|
||||
|
||||
@Test
|
||||
@ -51,6 +54,15 @@ class MapUtilsTest {
|
||||
assert m == [a: 'replaced', m: [b: 'replaced', c: 'otherString']]
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetByPath() {
|
||||
Map m = [trees: [oak: 5, beech :1], flowers:[rose: 23]]
|
||||
|
||||
assertThat(MapUtils.getByPath(m, 'flowers'), is([rose: 23]))
|
||||
assertThat(MapUtils.getByPath(m, 'trees/oak'), is(5))
|
||||
assertThat(MapUtils.getByPath(m, 'trees/palm'), is(null))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeepCopy() {
|
||||
|
||||
|
@ -99,7 +99,7 @@ stages:
|
||||
stepConditions:
|
||||
firstStep:
|
||||
config: testGeneral
|
||||
testStage2:
|
||||
testStage2:
|
||||
stepConditions:
|
||||
secondStep:
|
||||
config: testStage
|
||||
@ -107,7 +107,7 @@ stages:
|
||||
stepConditions:
|
||||
thirdStep:
|
||||
config: testStep
|
||||
|
||||
|
||||
'''
|
||||
} else {
|
||||
return '''
|
||||
@ -155,23 +155,23 @@ stages:
|
||||
testStage1:
|
||||
stepConditions:
|
||||
firstStep:
|
||||
config:
|
||||
config:
|
||||
testGeneral:
|
||||
- myValx
|
||||
- myVal1
|
||||
testStage2:
|
||||
- myVal1
|
||||
testStage2:
|
||||
stepConditions:
|
||||
secondStep:
|
||||
config:
|
||||
testStage:
|
||||
config:
|
||||
testStage:
|
||||
- maValXyz
|
||||
testStage3:
|
||||
stepConditions:
|
||||
thirdStep:
|
||||
config:
|
||||
config:
|
||||
testStep:
|
||||
- myVal3
|
||||
|
||||
|
||||
'''
|
||||
} else {
|
||||
return '''
|
||||
@ -218,18 +218,18 @@ stages:
|
||||
testStage1:
|
||||
stepConditions:
|
||||
firstStep:
|
||||
configKeys:
|
||||
configKeys:
|
||||
- myKey1_1
|
||||
- myKey1_2
|
||||
testStage2:
|
||||
- myKey1_2
|
||||
testStage2:
|
||||
stepConditions:
|
||||
secondStep:
|
||||
configKeys:
|
||||
configKeys:
|
||||
- myKey2_1
|
||||
testStage3:
|
||||
stepConditions:
|
||||
thirdStep:
|
||||
configKeys:
|
||||
configKeys:
|
||||
- myKey3_1
|
||||
'''
|
||||
} else {
|
||||
@ -451,27 +451,4 @@ steps: {}
|
||||
assertThat(nullScript.commonPipelineEnvironment.configuration.runStage.Acceptance, is(true))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetConfigValue() {
|
||||
|
||||
def config = [
|
||||
invalidKey: 'invalidValue',
|
||||
stringKey: 'stringValue',
|
||||
listKey: [
|
||||
'listValue1',
|
||||
'listValue2'
|
||||
],
|
||||
nested: [
|
||||
key: 'nestedValue'
|
||||
]
|
||||
]
|
||||
|
||||
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'stringKey'), is('stringValue'))
|
||||
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'listKey'), is(['listValue1','listValue2']))
|
||||
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'nested/key'), is('nestedValue'))
|
||||
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'invalidKey/key'), is(nullValue()))
|
||||
|
||||
//assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'nested/key'), is('nestedValue'))
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import com.sap.piper.ConfigurationLoader
|
||||
import static com.sap.piper.Prerequisites.checkScript
|
||||
|
||||
import com.sap.piper.ConfigurationHelper
|
||||
import com.sap.piper.MapUtils
|
||||
import groovy.transform.Field
|
||||
|
||||
@Field String STEP_NAME = getClass().getName()
|
||||
@ -65,27 +66,27 @@ void call(Map parameters = [:]) {
|
||||
case 'config':
|
||||
if (condition.getValue() instanceof Map) {
|
||||
condition.getValue().each {configCondition ->
|
||||
if (getConfigValue(stepConfig, configCondition.getKey()) in configCondition.getValue()) {
|
||||
if (MapUtils.getByPath(stepConfig, configCondition.getKey()) in configCondition.getValue()) {
|
||||
stepActive = true
|
||||
}
|
||||
}
|
||||
} else if (getConfigValue(stepConfig, condition.getValue())) {
|
||||
} else if (MapUtils.getByPath(stepConfig, condition.getValue())) {
|
||||
stepActive = true
|
||||
}
|
||||
break
|
||||
case 'configKeys':
|
||||
if (condition.getValue() instanceof List) {
|
||||
condition.getValue().each {configKey ->
|
||||
if (getConfigValue(stepConfig, configKey)) {
|
||||
if (MapUtils.getByPath(stepConfig, configKey)) {
|
||||
stepActive = true
|
||||
}
|
||||
}
|
||||
} else if (getConfigValue(stepConfig, condition.getValue())) {
|
||||
} else if (MapUtils.getByPath(stepConfig, condition.getValue())) {
|
||||
stepActive = true
|
||||
}
|
||||
break
|
||||
case 'filePatternFromConfig':
|
||||
def conditionValue = getConfigValue(stepConfig, condition.getValue())
|
||||
def conditionValue = MapUtils.getByPath(stepConfig, condition.getValue())
|
||||
if (conditionValue && findFiles(glob: conditionValue)) {
|
||||
stepActive = true
|
||||
}
|
||||
@ -110,16 +111,3 @@ void call(Map parameters = [:]) {
|
||||
echo "[${STEP_NAME}] Debug - Run Step Configuration: ${script.commonPipelineEnvironment.configuration.runStep}"
|
||||
}
|
||||
}
|
||||
|
||||
private def getConfigValue(Map stepConfig, def configKey) {
|
||||
if (stepConfig == null) return null
|
||||
|
||||
List configPath = configKey instanceof String ? configKey.tokenize('/') : configKey
|
||||
|
||||
def configValue = stepConfig[configPath.head()]
|
||||
|
||||
if (configPath.size() == 1) return configValue
|
||||
if (configValue in Map) return getConfigValue(configValue, configPath.tail())
|
||||
|
||||
return null
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user