AabinGunz
AabinGunz

Reputation: 12347

Groovy way of doing things

I have the below code, I want to transform it fully to groovy, so far what I came up with is shown below

def homeDir="INFA_HOME"
HashMap<String,String> params = IntermediateResults.get("userparams")
Map env=AppContext.get(AppCtxProperties.environmentVariables)

//checking if INFA_HOME is present in the map or not, I will be using INFA_HOME a lot, so can't I use homeDir istead??
boolean homeVarPresent=env.get("INFA_HOME")!=null

csm.pmserver(){
    pmserver_homevar(name:"$homeDir",set:"${homeVarPresent?'Y':'N'}",value:"${homeVarPresent?env.get('INFA_HOME'):na}")

//Instead of $env.INFA_HOME can't i use homeDir?
    pmserver_home(value:"$env.INFA_HOME/server/bin",exists:"${homeVarPresent?'Y':'N'}")
}

Upvotes: 0

Views: 134

Answers (2)

OverZealous
OverZealous

Reputation: 39570

I think @tim_yates example is OK, but it over-complicates the map access and ternary operators. I'd probably do this:

def homeDir = 'INFA_HOME'
HashMap<String,String> params = IntermediateResults.get("userparams")
Map env = AppContext.get( AppCtxProperties.environmentVariables )

csm.pmserver {
    pmserver_homevar( name  : homeDir,
                      set   : env[homeDir] ? 'Y' : 'N',
                      value : env[homeDir] ?: 'na' )

    pmserver_home( value  : "${env[homeDir]}/server/bin",
                   exists : env[homeDir] ? 'Y' :'N' )
}

It's a lot more readable to use the array access modifier with variables that to create several unnecessary GStrings. I might also be tempted to use a manual string concatenation for the path (env[homeDir] + '/server/bin'), but that's personal preference.

Upvotes: 1

tim_yates
tim_yates

Reputation: 171084

I think this is equivalent:

def homeDir = 'INFA_HOME'
HashMap<String,String> params = IntermediateResults.get("userparams")
Map env = AppContext.get( AppCtxProperties.environmentVariables )

csm.pmserver {
    pmserver_homevar( name  : "$homeDir",
                      set   : "${env."$homeDir" ? 'Y' : 'N'}",
                      value : "${env."$homeDir" ?: 'na'}" )

    pmserver_home( value  : "${env."$homeDir"}/server/bin",
                   exists : "${env."$homeDir" ? 'Y' :'N'}" )
}

Upvotes: 1

Related Questions