绝妙的做事方式

发布于 2024-12-08 18:59:39 字数 679 浏览 0 评论 0原文

我有下面的代码,我想将其完全转换为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'}")
}

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'}")
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2024-12-15 18:59:39

我认为 @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' )
}

使用数组访问修饰符和变量来创建几个不必要的 GString 更具可读性。我可能还想对路径使用手动字符串连接 (env[homeDir] + '/server/bin'),但这是个人偏好。

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.

寒江雪… 2024-12-15 18:59:39

我认为这是等效的:

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'}" )
}

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'}" )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文