Skip to content

Commit ebddcbd

Browse files
authored
Support array result include sequence action (#39)
1 parent 69c033c commit ebddcbd

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,43 @@ pub fn main(args: Value) -> Result<Value, Error> {
6262

6363
The action is mainly composed by a `main` function that accepts a JSON `serdes Value` as input and returns a `Result` including a JSON `serde Value`.
6464

65+
For the return result, not only support `A JSON serde Value` but also support `Array serde Value`
66+
67+
So a simple `hello array` funtion would be:
68+
69+
```rust
70+
extern crate serde_json;
71+
72+
use serde_derive::{Deserialize, Serialize};
73+
use serde_json::{Error, Value};
74+
75+
76+
pub fn main(args: Value) -> Result<Value, Error> {
77+
let output = ["a", "b"];
78+
serde_json::to_value(output)
79+
}
80+
```
81+
82+
And support array result for sequence action as well, the first action's array result can be used as next action's input parameter.
83+
84+
So the function can be:
85+
86+
```rust
87+
extern crate serde_json;
88+
89+
use serde_derive::{Deserialize, Serialize};
90+
use serde_json::{Error, Value};
91+
92+
93+
pub fn main(args: Value) -> Result<Value, Error> {
94+
let inputParam = args.as_array();
95+
let defaultOutput = ["c", "d"];
96+
match inputParam {
97+
None => serde_json::to_value(defaultOutput),
98+
Some(x) => serde_json::to_value(x),
99+
}
100+
}
101+
```
65102
### Managing dependencies
66103

67104
If your action needs external dependencies, you need to provide a zip file including your source, and your cargo file with all your dependencies. The folder structure is the following:

core/rust1.34/Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717

1818
# build go proxy from source
19-
FROM golang:1.16 AS builder_source
19+
FROM golang:1.18 AS builder_source
2020
ARG GO_PROXY_GITHUB_USER=apache
2121
ARG GO_PROXY_GITHUB_BRANCH=master
2222
RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \
@@ -25,13 +25,13 @@ RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \
2525
mv proxy /bin/proxy
2626

2727
# or build it from a release
28-
FROM golang:1.16 AS builder_release
29-
ARG GO_PROXY_RELEASE_VERSION=1.16@1.19.0
28+
FROM golang:1.18 AS builder_release
29+
ARG GO_PROXY_RELEASE_VERSION=1.18@1.20.0
3030
RUN curl -sL \
3131
https://github.com/apache/openwhisk-runtime-go/archive/{$GO_PROXY_RELEASE_VERSION}.tar.gz\
3232
| tar xzf -\
3333
&& cd openwhisk-runtime-go-*/main\
34-
&& GO111MODULE=on go build -o /bin/proxy
34+
&& GO111MODULE=on CGO_ENABLED=0 go build -o /bin/proxy
3535

3636
FROM rust:1.34
3737

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import actionContainers.{ActionContainer, BasicActionRunnerTests}
2121
import common.WskActorSystem
2222
import org.junit.runner.RunWith
2323
import org.scalatest.junit.JUnitRunner
24+
import spray.json.{JsArray, JsObject, JsString}
2425

2526
@RunWith(classOf[JUnitRunner])
2627
class ActionLoopRustBasicTests extends BasicActionRunnerTests with WskActorSystem {
@@ -136,4 +137,58 @@ class ActionLoopRustBasicTests extends BasicActionRunnerTests with WskActorSyste
136137
| Ok(args)
137138
|}
138139
""".stripMargin)
140+
141+
it should "support return array result" in {
142+
val (out, err) = withActionLoopContainer { c =>
143+
val code = """
144+
|extern crate serde_json;
145+
|
146+
|use serde_derive::{Deserialize, Serialize};
147+
|use serde_json::{Error, Value};
148+
|
149+
|
150+
|pub fn main(args: Value) -> Result<Value, Error> {
151+
| let output = ["a", "b"];
152+
| serde_json::to_value(output)
153+
|}
154+
""".stripMargin
155+
156+
val (initCode, _) = c.init(initPayload(code))
157+
158+
initCode should be(200)
159+
160+
val (runCode, runRes) = c.runForJsArray(runPayload(JsObject()))
161+
runCode should be(200)
162+
runRes shouldBe Some(JsArray(JsString("a"), JsString("b")))
163+
}
164+
}
165+
166+
it should "support array as input param" in {
167+
val (out, err) = withActionLoopContainer { c =>
168+
val code = """
169+
|extern crate serde_json;
170+
|
171+
|use serde_derive::{Deserialize, Serialize};
172+
|use serde_json::{Error, Value};
173+
|
174+
|
175+
|pub fn main(args: Value) -> Result<Value, Error> {
176+
| let inputParam = args.as_array();
177+
| let defaultOutput = ["c", "d"];
178+
| match inputParam {
179+
| None => serde_json::to_value(defaultOutput),
180+
| Some(x) => serde_json::to_value(x),
181+
| }
182+
|}
183+
""".stripMargin
184+
185+
val (initCode, _) = c.init(initPayload(code))
186+
187+
initCode should be(200)
188+
189+
val (runCode, runRes) = c.runForJsArray(runPayload(JsArray(JsString("a"), JsString("b"))))
190+
runCode should be(200)
191+
runRes shouldBe Some(JsArray(JsString("a"), JsString("b")))
192+
}
193+
}
139194
}

0 commit comments

Comments
 (0)