1. Introduction

This Library is a config writer for the groovy format used, by the config slurper. I don’t make any claims that this covers all edge cases, in fact there are a few hacks to make this work with a sample from Grails application.yml. If you come across edge cases that don’t work be sure to raise an issue on github: https://www.youtube.com/watch?v=aro3_RZqgtU

I wrote this to be used in a script to provide a conversion from yml to groovy, which I though there should be, because I find the groovy format to be more flexible, and readable, then yml. I found it Strange when Graeme Rocher said: "But to answer the question more concretely we wanted a format that is machine writable as well as readable" - https://www.youtube.com/watch?v=aro3_RZqgtU

Surely since there is a Config Slurper, there has to be a config writer right? Well I was wrong, partially…​ There is something, but it is limited, and didn’t provide the closure based syntax.

2. Version History

  • 2.0

    • Updated all dependencies:

      • Java 11

      • Groovy 3.0.9

      • Gradle 7.3.3

  • 1.2

    • Fixed another issue where yml names that include dashes can include more that primitives/strings, they now can support lists and maps.

  • 1.1

    • was re-relase to maven central using new coordinates 'io.github.virtualdogbert:GroovyConfigWriter:1.1'

    • Fixed and issue where yml key names that have dashes in them show up like this:

      template {
          template['check-template-location'] = false
      }
    • Updated unit tests.

    • Updated documentation.

  • 1.0

    • People have been using it for quite a while with no major complaints, so it’s time to kick it to 1.0 with some updates

    • Added in a asClosure option, which defaults to true, however, it you change it to false you will get a map based output instead.

    • Updated the dependencies, and made them compileOnly, so in your script/application you will have to provide your own:

      • 'org.codehaus.groovy:groovy-all:2.5.4',

      • 'org.slf4j:slf4j-api:1.6.1',

      • 'ch.qos.logback:logback-classic:0.9.28'

    • Also updated min version of Java to be Java 8. If you want to use the library with Java 7 continue to use version 0.2/

  • 0.2

    • A user wanted Java 7 support so I dropped the requirement that it was a Java 8 lib back to 7. However the next release will be for java 8+

  • 0.1

    • Initial Release looking for feedback.

3. Getting Started

  1. Add the following dependency: .build.gradle

    compile 'io.github.virtualdogbert:GroovyConfigWriter:2.0'
  2. Parameters

    • fileName - Optional file name to write the output to. By default is not specified the output will be written to System.out.

    • config - An Optional parameter to pass in config to be written. You can also directly call the writeToGroovy, which could be useful for writing in multiple chunks. This might be useful if your config comes from a yml file, which has multiple documents.

    • indentSpacer - Optional parameter the spacer to be used for indenting, by default four spaces is used.

    • quoteValues - values that will be quoted.

    • asClosure - true by default using the closure syntax, vs the map based syntax for the groovy config.

  3. Some sample code:

    @Grab('io.github.virtualdogbert:GroovyConfigWriter:2.0')
    @Grab('org.yaml:snakeyaml:1.18')
    @Grab('org.slf4j:slf4j-api:1.7.33')
    @Grab('ch.qos.logback:logback-classic:1.2.10')
    
    
    import com.virtualdogbert.GroovyConfigWriter
    import org.yaml.snakeyaml.Yaml
    //Some sample code using snake yml to parse yml
    File config = new File('/home/virtualdogbert/application.yml')
    String configText = config.newDataInputStream().getText()
    List<String> docs = configText.split('---\n')
    
    //Writes out to a file
    //GroovyConfigWriter configWriter = new GroovyConfigWriter('/home/virtualdogbert/application.groovy')
    
    
    //writes to System.out
    GroovyConfigWriter configWriter = new GroovyConfigWriter()
    //GroovyConfigWriter configWriter = new GroovyConfigWriter(indentSpacer:'  ', quoteValues:['default'], asClosure:false )
    Yaml yaml = new Yaml()
    
    docs.findResults { String document ->
        configWriter.writeToGroovy(yaml.load(document) as Map)
    }
    
    
    /*
     *flushes writer and closes it. If run in groovy console this will also cause
     * the output window not to work afterward, because it will be  closed'.
     * To get the console back you will have to restart it.
     */
    configWriter.close()