You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by dg...@apache.org on 2022/08/15 15:18:14 UTC

[openwhisk-runtime-ruby] branch master updated: Support array result include sequence action (#72)

This is an automated email from the ASF dual-hosted git repository.

dgrove pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk-runtime-ruby.git


The following commit(s) were added to refs/heads/master by this push:
     new afce9ad  Support array result include sequence action (#72)
afce9ad is described below

commit afce9ad0306e0d1021051ddf9c10079e8c31ec6f
Author: ningyougang <41...@qq.com>
AuthorDate: Mon Aug 15 23:18:10 2022 +0800

    Support array result include sequence action (#72)
---
 README.md                                          | 32 ++++++++++++++++++
 core/ruby2.5Action/rackapp/run.rb                  |  6 ++--
 core/ruby2.6ActionLoop/Dockerfile                  |  4 +--
 .../Ruby25ActionContainerTests.scala               | 36 ++++++++++++++++++++
 .../Ruby26ActionLoopContainerTests.scala           | 38 ++++++++++++++++++++++
 5 files changed, 111 insertions(+), 5 deletions(-)

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 @@ class RunApp
     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 @@ class RunApp
   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")))
+    }
+  }
 }