Skip to content

Support array result include sequence action #72

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

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
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,38 @@
[![Twitter](https://img.shields.io/twitter/follow/openwhisk.svg?style=social&logo=twitter)](https://twitter.com/intent/follow?screen_name=openwhisk)

### Give it a try today
A very simple `hello world` function would be:

```ruby
def main(args)
name = args["name"] || "stranger"
greeting = "Hello #{name}!"
puts greeting
{ "greeting" => greeting }
end
```

For the return result, not only support `dictionary` but also support `array`

So a very simple `hello array` function would be:

```ruby
def main(args)
nums = Array["a","b"]
nums
end
```

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

```ruby
def main(args)
args
end
```

To use as a docker action
```
wsk action update myAction my_action.rb --docker openwhisk/action-ruby-v2.5
Expand Down
6 changes: 3 additions & 3 deletions core/ruby2.5Action/rackapp/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def call(env)
if system(env, "bundle exec ruby -r #{ENTRYPOINT} #{RACKAPP_DIR}runner.rb | tee #{OUT}") then
if File.exist? RESULT then
result = File.read(RESULT)
if valid_json?(result) then
if valid_json_or_array(result) then
SuccessResponse.new(JSON.parse(result))
else
warn "Result must be an array but has type '#{result.class.to_s}': #{result}"
Expand All @@ -59,8 +59,8 @@ def call(env)
end

private
def valid_json?(json)
JSON.parse(json).class == Hash
def valid_json_or_array(json)
JSON.parse(json).class == Hash or JSON.parse(json).class == Array
rescue
false
end
Expand Down
4 changes: 2 additions & 2 deletions core/ruby2.6ActionLoop/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \

# or build it from a release
FROM golang:1.18 AS builder_release
ARG GO_PROXY_RELEASE_VERSION=1.18@1.19.0
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 ruby:2.6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,40 @@ class Ruby25ActionContainerTests extends BasicActionRunnerTests with WskActorSys
}
}

it should "support return array result" in {
val (out, err) = withRuby25Container { c =>
val code = """
| def main(args)
| nums = Array["a","b"]
| nums
| end
""".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) = withRuby25Container { c =>
val code = """
| def main(args)
| args
| end
""".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")))
}
}
}
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 Ruby26ActionLoopContainerTests extends BasicActionRunnerTests with WskActorSystem {
Expand Down Expand Up @@ -89,4 +90,41 @@ class Ruby26ActionLoopContainerTests extends BasicActionRunnerTests with WskActo
| args
|end
|""".stripMargin)

it should "support return array result" in {
val (out, err) = withActionLoopContainer { c =>
val code = """
| def main(args)
| nums = Array["a","b"]
| nums
| end
""".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 = """
| def main(args)
| args
| end
""".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")))
}
}
}