我有以下代码,我想将它完全转换为groovy,到目前为止,我想出的内容如下所示
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'}")
}
答案 0 :(得分:1)
我认为这是等效的:
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'}" )
}
答案 1 :(得分:1)
我认为@tim_yates示例没问题,但它使地图访问和三元运算符过于复杂。我可能会这样做:
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' )
}
将数组访问修饰符与变量一起使用以创建多个不必要的GS字符串更具可读性。我也可能想对路径(env[homeDir] + '/server/bin'
)使用手动字符串连接,但这是个人偏好。