groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jesper Steen Møller <jes...@selskabet.org>
Subject Re: Java object conversion using ConfigSlurper
Date Wed, 16 May 2018 11:25:31 GMT
Hi Adithyank

> On 16 May 2018, at 12.52, adithyank <adi.k.dev@gmail.com> wrote:
> 
> Hi Mario Garcia,
> 
> Groovy already has Map coercion feature with which the methods of any class
> can be delegated to the closures in Map. This uses `as` keyword.
> 

> If we implement `asType(Class)` in `ConfigObject` (ConfigObject already
> implements Map), some of the functionalities of existing coercion may be
> affected. I am not sure !

Using "as <classname>" already works, so there's already a pretty simple way to do what
you're suggesting:

ConfigObject co = new ConfigSlurper().parse("""
myownconfig {
    name = 'Threadpool'
    displayLabel = "Thread Pool"
    threadCount = 12
}""")

public class MyConfig
{
     String name
     String displayLabel
     int threadCount
}

def myConfig = co.myownconfig as MyConfig

println "${myConfig.displayLabel} - ${myConfig.threadCount}"

(try it in Groovy Console)

-Jesper
Mime
View raw message