查看文档,很容易就知道如何在字段上使用##运算符
* def data = { a: 'hello', b: null, c: null }
* def json = { foo: '#(data.a)', bar: '#(data.b)', baz: '##(data.c)' }
* match json == { foo: 'hello', bar: null }但是,如果我想在没有属性的情况下在json对象上使用它呢?例如,如果我正在做像这样的事情
* def data = { a: 'hello', b: null, c: null }
* def json = { foo: '#(data.a)', bar: '#(data.b)', jsonObject: {baz: '##(data.c)'} }
* match json == { foo: 'hello', bar: null }它抱怨有一个空的对象
actual: {foo=hello, bar=null, jsonObject={}}, expected: {foo=hello, bar=null}并且将##{baz:' ## (data.c)'}或##({baz:'##(data.c)'})作为jsonObject值不起作用,因为无法正确识别##。
正确的语法是什么?或者有没有其他方法来做我所描述的事情?
发布于 2021-07-22 18:41:07
这实际上是一个有点复杂的条件替换。下面是你怎么做的:
* def data = { a: 'hello', b: null, c: null }
* def temp = data.c ? { baz: data.c } : null
* def json = { foo: '#(data.a)', bar: '#(data.b)', jsonObject: '##(temp)' }
* match json == { foo: 'hello', bar: null }也许你把事情搞得太复杂了,你所需要的就是:
* def data = { a: 'hello', b: null, c: null }
* def json = { foo: '#(data.a)', bar: '#(data.b)' }
* if (data.c) json.jsonObject = ({ baz: data.c })
* match json == { foo: 'hello', bar: null }https://stackoverflow.com/questions/68482628
复制相似问题