Simple bean code creation

Code generator (codegen.groovy)

import java.util.Map  
import groovy.text.TemplateEngine  
import groovy.text.SimpleTemplateEngine  
import groovy.text.Template

def bean = {  
    dto = "CartItem"
    members {
        id { type "String" }
        product { type "String" }
        price { 
            type "int" 
            mutable = true
        }
    }

    mappers {
        CartItem {
            to {
                id = "id"
                name = "product"
                priceMinorUnits = "price"
            }
            from {
                id = "id"
                product = "name"
                price = "priceMinorUnits"
            }
        }
    }
}

class NestedMapBuilder {  
    Map valueMap = [:]
    NestedMapBuilder(Closure c) {
        def callable = c
        callable.delegate = this
        callable.resolveStrategy = Closure.DELEGATE_FIRST
        callable()
    }
    def propertyMissing(String name) {
        return valueMap[name]
    }
    def propertyMissing(String name, value) {
        valueMap[name] = value
    }
    def methodMissing(String name, args) {
        if (args.size() == 1) {
            if (args[0] instanceof Closure) {
                valueMap[name] = new NestedMapBuilder(args[0]).valueMap
            } else {
                valueMap[name] = args[0]
            }
        } else {
            valueMap[name] = args
        }
    }
}

//println new NestedMapBuilder(bean).valueMap

SimpleTemplateEngine template = new SimpleTemplateEngine()  
Map model = new NestedMapBuilder(bean).valueMap  
println template.createTemplate(new File("dto.groovy").text).make(model)  

Template (dto.template)

class ${dto}DTO {  
    private members = [:]

    <% members.each { k, v -> %>
        ${v.type} get${k.capitalize()}() {
            return members.${k}
        }

        <% if (v.mutable) { %>
        void set${k.capitalize()}(${v.type} val) {
            members.${k} = val
        }
        <% } %>
    <% } %>

    <% mappers.each { mapper -> %>
        <% mapper.value.each { mode, members -> %>
            <%  def type = mapper.key %>
            <% if (mode == "to") { %>
                ${type} to${type}() {
                    ${type} thing = new ${type}()
                    <% members.each { k1, v1 -> %>
                        thing.${k1} = ${v1}
                    <% } %>

                    return thing
                }
            <% } %>
            <% if (mode == "from") { %>
                void from${type}(${type} thing) {
                    <% members.each { k1, v1 -> %>
                        ${v1} = thing.${k1}
                    <% } %>
                }
            <% } %>
        <% } %>
    <% } %>
}

Generated code

class CartItemDTO {  
    private members = [:]

    String getId() {
        return members.id
    }

    String getProduct() {
        return members.product
    }

    int getPrice() {
        return members.price
    }

    void setPrice(int val) {
        members.price = val
    }

    CartItem toCartItem() {
        CartItem thing = new CartItem()

        thing.id = id
        thing.name = product
        thing.priceMinorUnits = price

        return thing
    }

    void fromCartItem(CartItem thing) {
        id = thing.id
        name = thing.product
        priceMinorUnits = thing.price
    }
}