I have a map with keys in dot notation, but I need it as a nested map.
[test.key.one: 'value1', text.key.two: 'value2']
Now the result should be
[
test: [
key: [
one: 'value1',
two: 'value2'
]
]
]
And here is my idea of code
def extract(String key, String value) {
if(key.contains(".")) {
def (String target, String subKey) = key.split('\\.', 2)
return ["$target": extract(subKey, value)]
} else {
return ["$key": extractType(value)]
}
}
But I want to know if there is any groovy magic doing this in a closure or witht he help of other goodies to make it simpler.
There is one handy class: groovy.util.ConfigSlurper
def map = ['test.key.one': 'value1', 'test.key.two': 'value2']
def props = new Properties()
props.putAll(map)
println new ConfigSlurper().parse(props) // [test:[key:[two:value2, one:value1]]]
The only drawback is that it expects java.util.Properties instance, so you need to create one from the map.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With