I have two maps ,
def configmap = [ "env" :["required": ["team" : "k", "test": "sample"], "optional" : ["anotherkey" : ""]]]
def valuesReplacementMap=[ "env.required.team":"s","env.required.test":"new", "env.optional.anotherkey":"newvalue" ]
valuesReplacementMap
has path to a key in configmap
and the new value which needs to be updated.
i need to update the value in configmap
to the value given in valuesReplacementMap
here is my code, which works
def m = [ "env" :["required": ["team" : "k", "test": "sample"], "optional" : ["anotherkey" : ""]]]
def valuesReplacementMap=[ "env.required.team":"s",
"env.required.test":"new",
"env.optional.anotherkey":"newvalue" ]
def copyofOrigMap
valuesReplacementMap.each{ key, value->
copyofOrigMap = m
key.tokenize(".").inject{ attr, val ->
if(copyofOrigMap[attr][val] instanceof java.lang.String ){
copyofOrigMap[attr][val] = value
}else
copyofOrigMap = copyofOrigMap[attr]
val
}
}
My question is
- is there a way to avoid checking
instanceof java.lang.String
, this i am doing to see if leaf node/key is reached - is there any simpler way to achive this entire functionality
I went through some of the earlier posted questions , but didnt find any optimized way