Skip to main content

Convert Dict Keys to Snake Case in Ansible

I’m currently writing an Ansible role, where I needed to convert a dictionary with camelCase keys to snake_case keys. I can’t change the source dict directly, as it is some form of external config for which the camelCase actually makes sense. As ansible works with snake_case variables normally, I needed to convert the input to a sane format for later consumption.

After a lot of trail-and-error and searching, I found the following two Stack Overflow questions, which massively inspired my final solution:

Combining these two methods, I came up with the following solution, which I’d like to share:

converted_dict: |-
    | map('regex_replace', '((?!^)|\b[a-zA-Z][a-z]*)([A-Z][a-z]*|\d+)', '\1_\2')
    | map('lower')
    | zip(source_dict.values())
    | items2dict(key_name=0, value_name=1)

Assigning ID for domain objects in Grails via constructor

Update for Grails 2.2+

As of Grails 2.2-RC1 it is possible to simply add a bindable:true to the constraints section of the domain class to allow assignment in the constructor / findOrCreateWhere:

class MyDomain {
    static constraints = {
        // allow binding of "id" attribute (e.g. in constructor or url parameters)
        id bindable: true

Building a Groovy project with Maven

Intro - Why I chose Maven

Recently I wrote a small program in Groovy. As many of my Groovy projects it all started with a single script. After some coding, I ended up with a script containing 3-4 classes with more to come. So I decided to go for a “real project”: Add a build script and separate every class in its own file.

But… what technology should I use to build this? I knew Maven and Ant and just heard of Gant and Gradle. As I have written some Ant scripts before and have seen different ones from other developers, I quickly decided to skip Ant as an option: The build scripts are way to cluttered and long for “modern times” in my opinion. Furthermore I do not want to spent as much time for writing my build script as I need to write the actual software.

Unfortunately I had not time to research for Gradle or Gant. I know both tools are basically Groovy driven build tools, which makes them a quite natural choice for building a Groovy project. However, my company uses Maven and Ant for nearly all of its builds. So using another build tool would add a technology, developers in my company (including me) need to learn first. So here we are… Maven it should be. (I hope I will be able to use Gradle some time in the future - at least in my hobby projects.)