From ea2ed05ca460d6121393038fc90a3a69ae7fdd59 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 25 Jul 2022 18:29:28 +0800 Subject: [PATCH 1/4] Support array result --- core/actionProxy/actionproxy.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/actionProxy/actionproxy.py b/core/actionProxy/actionproxy.py index 1812918..d174ce0 100644 --- a/core/actionProxy/actionproxy.py +++ b/core/actionProxy/actionproxy.py @@ -135,7 +135,7 @@ def run(self, args, env): def error(msg): # fall through (exception and else case are handled the same way) sys.stdout.write('%s\n' % msg) - return (502, {'error': 'The action did not return a dictionary.'}) + return (502, {'error': 'The action did not return a dictionary or array.'}) try: input = json.dumps(args) @@ -186,7 +186,7 @@ def error(msg): try: json_output = json.loads(lastLine) - if isinstance(json_output, dict): + if isinstance(json_output, dict) or isinstance(json_output, list): return (200, json_output) else: return error(lastLine) @@ -258,7 +258,7 @@ def init(message=None): def run(message=None): def error(): - response = flask.jsonify({'error': 'The action did not receive a dictionary as an argument.'}) + response = flask.jsonify({'error': 'The action did not receive a dictionary or array as an argument.'}) response.status_code = 404 return complete(response) @@ -269,7 +269,7 @@ def error(): return error() else: args = message.get('value', {}) if message else {} - if not isinstance(args, dict): + if not (isinstance(args, dict) or isinstance(args, list)): return error() if runner.verify(): From 83ce2a858d8f8da7ee32b969c93d1a515b248965 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 25 Jul 2022 18:31:26 +0800 Subject: [PATCH 2/4] Add test case --- .../ActionProxyContainerTests.scala | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala b/tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala index acf83b9..d6c7ec3 100644 --- a/tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala +++ b/tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala @@ -227,7 +227,7 @@ class ActionProxyContainerTests extends BasicActionRunnerTests with WskActorSyst initCode should be(200) val (runCode, out) = c.run(JsNull) runCode should be(502) - out should be(Some(JsObject("error" -> JsString("The action did not return a dictionary.")))) + out should be(Some(JsObject("error" -> JsString("The action did not return a dictionary or array.")))) } checkStreams(out, err, { @@ -270,4 +270,20 @@ class ActionProxyContainerTests extends BasicActionRunnerTests with WskActorSyst runRes.get.fields.get("pwd_cmd") shouldBe Some(JsString("/action")) } } + + it should "support array result" in { + withActionContainer() { c => + val code = """ + |#!/bin/bash + |echo '["a", "b"]' + """.stripMargin.trim + + val (initCode, initRes) = c.init(initPayload(code)) + initCode should be(200) + + val (runCode, runRes) = c.runForJsArray(JsObject()) + runCode should be(200) + runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) + } + } } From ef401fc86f5b8ab4002d1a150e2bcb84e4b3b4d4 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 8 Aug 2022 13:57:47 +0800 Subject: [PATCH 3/4] Add another test case --- .../ActionProxyContainerTests.scala | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala b/tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala index d6c7ec3..97e6227 100644 --- a/tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala +++ b/tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala @@ -271,7 +271,7 @@ class ActionProxyContainerTests extends BasicActionRunnerTests with WskActorSyst } } - it should "support array result" in { + it should "support return array result" in { withActionContainer() { c => val code = """ |#!/bin/bash @@ -281,7 +281,24 @@ class ActionProxyContainerTests extends BasicActionRunnerTests with WskActorSyst val (initCode, initRes) = c.init(initPayload(code)) initCode should be(200) - val (runCode, runRes) = c.runForJsArray(JsObject()) + val (runCode, runRes) = c.runForJsArray(runPayload(JsObject())) + runCode should be(200) + runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) + } + } + + it should "support array as input param" in { + withActionContainer() { c => + val code = """ + |#!/bin/bash + |arr=$1 + |echo $arr + """.stripMargin.trim + + val (initCode, initRes) = c.init(initPayload(code)) + initCode should be(200) + + val (runCode, runRes) = c.runForJsArray(runPayload(JsArray(JsString("a"), JsString("b")))) runCode should be(200) runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) } From 5366addbd1c8566133763e92b2353e396aa33a7d Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 8 Aug 2022 17:09:56 +0800 Subject: [PATCH 4/4] Add document for support array result --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 20e97b2..c0406b2 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,21 @@ echo \ '#!/bin/bash echo "{\"message\":\"Hello World\"}"' > exec ``` + +For the return result, not only support `dictionary` but also support `array` +``` +echo \ +'#!/bin/bash +echo '["a", "b"]'' > exec +``` + +And support array result for sequence action as well, the first action's array result can be used as next action's input parameter +``` +echo \ +'#!/bin/bash +echo $1' > exec +``` + ``` chmod +x exec zip myAction.zip exec