绝妙的做事方式
我有下面的代码,我想将其完全转换为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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 @tim_yates 示例还可以,但它使地图访问和三元运算符过于复杂。我可能会这样做:
使用数组访问修饰符和变量来创建几个不必要的 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:
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.我认为这是等效的:
I think this is equivalent: