Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support array result include sequence action #39

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,43 @@ pub fn main(args: Value) -> Result<Value, Error> {

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`.

For the return result, not only support `A JSON serde Value` but also support `Array serde Value`

So a simple `hello array` funtion would be:

```rust
extern crate serde_json;

use serde_derive::{Deserialize, Serialize};
use serde_json::{Error, Value};


pub fn main(args: Value) -> Result<Value, Error> {
let output = ["a", "b"];
serde_json::to_value(output)
}
```

And support array result for sequence action as well, the first action's array result can be used as next action's input parameter.

So the function can be:

```rust
extern crate serde_json;

use serde_derive::{Deserialize, Serialize};
use serde_json::{Error, Value};


pub fn main(args: Value) -> Result<Value, Error> {
let inputParam = args.as_array();
let defaultOutput = ["c", "d"];
match inputParam {
None => serde_json::to_value(defaultOutput),
Some(x) => serde_json::to_value(x),
}
}
```
### Managing dependencies

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:
Expand Down
8 changes: 4 additions & 4 deletions core/rust1.34/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

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

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

FROM rust:1.34

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import actionContainers.{ActionContainer, BasicActionRunnerTests}
import common.WskActorSystem
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import spray.json.{JsArray, JsObject, JsString}

@RunWith(classOf[JUnitRunner])
class ActionLoopRustBasicTests extends BasicActionRunnerTests with WskActorSystem {
Expand Down Expand Up @@ -136,4 +137,58 @@ class ActionLoopRustBasicTests extends BasicActionRunnerTests with WskActorSyste
| Ok(args)
|}
""".stripMargin)

it should "support return array result" in {
val (out, err) = withActionLoopContainer { c =>
val code = """
|extern crate serde_json;
|
|use serde_derive::{Deserialize, Serialize};
|use serde_json::{Error, Value};
|
|
|pub fn main(args: Value) -> Result<Value, Error> {
| let output = ["a", "b"];
| serde_json::to_value(output)
|}
""".stripMargin

val (initCode, _) = c.init(initPayload(code))

initCode should be(200)

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 {
val (out, err) = withActionLoopContainer { c =>
val code = """
|extern crate serde_json;
|
|use serde_derive::{Deserialize, Serialize};
|use serde_json::{Error, Value};
|
|
|pub fn main(args: Value) -> Result<Value, Error> {
| let inputParam = args.as_array();
| let defaultOutput = ["c", "d"];
| match inputParam {
| None => serde_json::to_value(defaultOutput),
| Some(x) => serde_json::to_value(x),
| }
|}
""".stripMargin

val (initCode, _) = 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")))
}
}
}