空手道 - 带有字符串和数组的条件逻辑 [英] Karate - Conditional logic with string and array

查看:26
本文介绍了空手道 - 带有字符串和数组的条件逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自 这个问题之前问过.我正在发出 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

当我得到一个数组的响应时,我想出了如何循环遍历它,并将值传递给另一个请求.但是,有时响应会以单个代码的形式返回,并且会以字符串的形式返回.在这种情况下,我无法执行与数组相同的逻辑.我读过 空手道条件逻辑,但我不知道如何制作做我想做的.

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

如果响应以数组形式返回,则此方法有效,但显然如果以字符串形式返回,它将中断.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆