[英]DataWeave best practices when null variable assignment
This is a JSON-JSON transformation, transforming a boolean input ("true"|"false")
into a char output ('Y'|'N')
. 这是JSON-JSON转换,将布尔输入("true"|"false")
转换为char输出('Y'|'N')
。 So we go for something like: 所以我们去做类似的事情:
varOutput: ('Y' when payload.varInput otherwise 'N')
But what if varInput is null? 但是,如果varInput为null怎么办? we got exception. 我们有例外。 I could control it with another when-otherwise: 否则,我可以用另一个控制它:
varOutput: ('Y' when payload.varInput != null otherwise 'N')
when payload.varInput != null otherwise null,
This last one is null safe, but again, I feel there should be a more elegant way. 最后一个是空安全的,但再次,我觉得应该有一个更优雅的方法。
Use default
使用default
{varOutput: ( payload.varInput default 'N')
}
Or unless/otherwise is null safe and a bit more elegant: 或除非/否则为null安全且更为优雅:
{
varOutput: ('Y' unless payload.varInput !=null otherwise 'N')
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.