diff --git a/README.md b/README.md index f30a236..1ae16b1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/core/ruby2.5Action/rackapp/run.rb b/core/ruby2.5Action/rackapp/run.rb index 2e4eab2..d2200b5 100644 --- a/core/ruby2.5Action/rackapp/run.rb +++ b/core/ruby2.5Action/rackapp/run.rb @@ -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}" @@ -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 diff --git a/core/ruby2.6ActionLoop/Dockerfile b/core/ruby2.6ActionLoop/Dockerfile index 8daffed..834650e 100644 --- a/core/ruby2.6ActionLoop/Dockerfile +++ b/core/ruby2.6ActionLoop/Dockerfile @@ -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 diff --git a/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala b/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala index 4e5839b..4ca1288 100644 --- a/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala +++ b/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala @@ -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"))) + } + } } diff --git a/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala b/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala index a5ed68d..d249d51 100644 --- a/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala +++ b/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala @@ -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 { @@ -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"))) + } + } }