空手道-带字符串和数组的条件逻辑 [英] Karate - Conditional logic with string and array
问题描述
此问题源自这个问题之前问过.我正在发出一个Soap请求,并且接收到的响应是数组或字符串.
This question derives from this question asked earlier. I am making a Soap request, and I am receiving the response as either an Array, or String.
[print] [ "M4205N", "M4206U" ]
[print] M5967H
当我以数组形式获得响应时,我想出了如何遍历它,并将值传递给另一个请求.但是,有时响应将以单个Code形式返回,并且将以String形式返回.在这种情况下,我无法执行与数组相同的逻辑.我已经读过空手道条件逻辑,但我不知道该怎么做做我想做的.
When I get the response as an array, I figured out how to loop through it, and pass the values to another request. However, sometimes the response will come back as a single Code, and it will be returned as a String. In that case, I cannot perform the same logic as I did with the array. I've read about Karate conditional logic, but I cannot figure out how to make it do what I want.
我想做这样的事情: 如果响应以字符串形式返回,则使用一个值调用此方法. 如果响应以数组形式返回,则调用此方法并传递数组中的每个值.
I want to do something like this: If the response is returned as a String, then call this method with the one value. If the response is returned as an array, then call this method and pass each value from the array.
这是我想到的方法之一,但由于类型差异而无法使用:
This is one of the ways I had in mind, but it does not work because of the Type difference:
* def memberCodes = memberCodes.size() > 1 ? karate.mapWithKey(memberCodes, 'memberCode') : {}
* def result = call read('OtherRequest.feature') memberCodes
如果响应以数组形式返回,则此方法有效,但是显然,如果将其作为String返回,它将中断.
This works if the response is returned as an array, but obviously if it's returned as a String it will break.
执行此条件逻辑的正确方法是什么?另外,请参阅我的上一个问题以获取更多背景信息(如果需要).谢谢!
What is the right way to perform this conditional logic? Also, please refer to my previous question for more context if needed. Thanks!
推荐答案
有趣.此检查应该可以测试是否(不是)字符串:
Interesting. This check should work for testing if (not) a string:
* def memberCodes = typeof memberCodes != 'string' ? karate.mapWithKey(memberCodes, 'memberCode') : {}
也请参考以下相关问题: https://stackoverflow.com/a/58543843/143475
Also refer this somewhat related question: https://stackoverflow.com/a/58543843/143475
这篇关于空手道-带字符串和数组的条件逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!