Skip to content

Commit 2c8df9b

Browse files
authored
Support array result include sequence action (#92)
1 parent 28fc01d commit 2c8df9b

3 files changed

Lines changed: 53 additions & 5 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ echo \
2929
'#!/bin/bash
3030
echo "{\"message\":\"Hello World\"}"' > exec
3131
```
32+
33+
For the return result, not only support `dictionary` but also support `array`
34+
```
35+
echo \
36+
'#!/bin/bash
37+
echo '["a", "b"]'' > exec
38+
```
39+
40+
And support array result for sequence action as well, the first action's array result can be used as next action's input parameter
41+
```
42+
echo \
43+
'#!/bin/bash
44+
echo $1' > exec
45+
```
46+
3247
```
3348
chmod +x exec
3449
zip myAction.zip exec

core/actionProxy/actionproxy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def run(self, args, env):
135135
def error(msg):
136136
# fall through (exception and else case are handled the same way)
137137
sys.stdout.write('%s\n' % msg)
138-
return (502, {'error': 'The action did not return a dictionary.'})
138+
return (502, {'error': 'The action did not return a dictionary or array.'})
139139

140140
try:
141141
input = json.dumps(args)
@@ -186,7 +186,7 @@ def error(msg):
186186

187187
try:
188188
json_output = json.loads(lastLine)
189-
if isinstance(json_output, dict):
189+
if isinstance(json_output, dict) or isinstance(json_output, list):
190190
return (200, json_output)
191191
else:
192192
return error(lastLine)
@@ -258,7 +258,7 @@ def init(message=None):
258258

259259
def run(message=None):
260260
def error():
261-
response = flask.jsonify({'error': 'The action did not receive a dictionary as an argument.'})
261+
response = flask.jsonify({'error': 'The action did not receive a dictionary or array as an argument.'})
262262
response.status_code = 404
263263
return complete(response)
264264

@@ -269,7 +269,7 @@ def error():
269269
return error()
270270
else:
271271
args = message.get('value', {}) if message else {}
272-
if not isinstance(args, dict):
272+
if not (isinstance(args, dict) or isinstance(args, list)):
273273
return error()
274274

275275
if runner.verify():

tests/src/test/scala/runtime/actionContainers/ActionProxyContainerTests.scala

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class ActionProxyContainerTests extends BasicActionRunnerTests with WskActorSyst
227227
initCode should be(200)
228228
val (runCode, out) = c.run(JsNull)
229229
runCode should be(502)
230-
out should be(Some(JsObject("error" -> JsString("The action did not return a dictionary."))))
230+
out should be(Some(JsObject("error" -> JsString("The action did not return a dictionary or array."))))
231231
}
232232

233233
checkStreams(out, err, {
@@ -270,4 +270,37 @@ class ActionProxyContainerTests extends BasicActionRunnerTests with WskActorSyst
270270
runRes.get.fields.get("pwd_cmd") shouldBe Some(JsString("/action"))
271271
}
272272
}
273+
274+
it should "support return array result" in {
275+
withActionContainer() { c =>
276+
val code = """
277+
|#!/bin/bash
278+
|echo '["a", "b"]'
279+
""".stripMargin.trim
280+
281+
val (initCode, initRes) = c.init(initPayload(code))
282+
initCode should be(200)
283+
284+
val (runCode, runRes) = c.runForJsArray(runPayload(JsObject()))
285+
runCode should be(200)
286+
runRes shouldBe Some(JsArray(JsString("a"), JsString("b")))
287+
}
288+
}
289+
290+
it should "support array as input param" in {
291+
withActionContainer() { c =>
292+
val code = """
293+
|#!/bin/bash
294+
|arr=$1
295+
|echo $arr
296+
""".stripMargin.trim
297+
298+
val (initCode, initRes) = c.init(initPayload(code))
299+
initCode should be(200)
300+
301+
val (runCode, runRes) = c.runForJsArray(runPayload(JsArray(JsString("a"), JsString("b"))))
302+
runCode should be(200)
303+
runRes shouldBe Some(JsArray(JsString("a"), JsString("b")))
304+
}
305+
}
273306
}

0 commit comments

Comments
 (0)