How do I convert an arbitrary Groovy map / list to the config style DSL syntax that Groovy provides?
Example:
def config = [
'test': 'lalala',
'nestedObject': [
foo1: 'foo1 val',
foo2: 'foo2 val',
nested2: [
anInt: 5,
anArray: ['a', 'b', 'c'],
anIntArray: [1, 2, 3]
]
]
]
To something like:
test = 'lalala'
nestedObject {
foo1 = 'foo1 val'
foo2 = 'foo2 val'
nested2 {
anInt = 5
anArray = ['a', 'b', 'c']
anIntArray = [1, 2, 3]
}
}
UPDATE:
- Re-appropriating this post to explicitly ask for a dynamic/generalized solution.
- This makes it a unique question and unlike the following which assumes a known map at its initialization state: How to create ConfigObject using only nested maps in Grails?
Just convert each Map into a ConfigObject and then pretty-print it:
All credit goes to: How to create ConfigObject using only nested maps in Grails?
(I just wanted people to know you can do this outside of Grails and initially I didn't realize how the pretty printing was invoked. I was confused with JsonOutput.prettyPrint())
Thanks @Steinar
If you know the nested Map structure in advance, your solution will work. If you need to do this on an unknown arbitrary nested Map structure, try something like this:
Given your input and the above closure definition, the following print statement:
Yields this output: