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

[openwhisk] branch master updated: Add document for support array result (#5311)

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

ningyougang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 7de8bad25 Add document for support array result (#5311)
7de8bad25 is described below

commit 7de8bad25fbad49b2613c70b12a3abe9b24c857e
Author: ningyougang <ni...@navercorp.com>
AuthorDate: Thu Aug 18 16:09:21 2022 +0800

    Add document for support array result (#5311)
    
    * Fix test case failed for swift
    
    * Add document based on support array result feature
    
    * Apply review comments
---
 docs/actions-docker.md                        | 18 ++++++++++++
 docs/actions-dotnet.md                        | 40 +++++++++++++++++++++++++++
 docs/actions-go.md                            | 25 +++++++++++++++++
 docs/actions-java.md                          | 30 ++++++++++++++++++++
 docs/actions-nodejs.md                        | 26 +++++++++++++++++
 docs/actions-php.md                           | 26 +++++++++++++++++
 docs/actions-python.md                        | 18 ++++++++++++
 docs/actions-ruby.md                          | 21 ++++++++++++++
 docs/actions-rust.md                          | 31 +++++++++++++++++++++
 docs/actions-swift.md                         | 26 +++++++++++++++--
 tests/dat/actions/unicode.tests/swift-4.2.txt |  5 ++--
 tests/dat/actions/unicode.tests/swift-5.1.txt |  5 ++--
 tests/dat/actions/unicode.tests/swift-5.3.txt |  5 ++--
 13 files changed, 268 insertions(+), 8 deletions(-)

diff --git a/docs/actions-docker.md b/docs/actions-docker.md
index 788f72324..2112746ce 100644
--- a/docs/actions-docker.md
+++ b/docs/actions-docker.md
@@ -236,6 +236,24 @@ DATE=`date`
 echo "{ \"message\": \"Hello $NAME! It is $DATE.\" }"
 ```
 
+An action supports not only a JSON object but also a JSON array as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```
+#!/bin/bash
+echo '["a", "b"]''
+```
+
+You can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```
+#!/bin/bash
+echo $1
+```
+
 - Create an action from this shell script.
 
 ```
diff --git a/docs/actions-dotnet.md b/docs/actions-dotnet.md
index 09c293bf5..3ac75beac 100644
--- a/docs/actions-dotnet.md
+++ b/docs/actions-dotnet.md
@@ -79,6 +79,46 @@ cd out
 zip -r -0 helloDotNet.zip *
 ```
 
+An action supports not only a JSON object but also a JSON array as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```csharp
+using System;
+using Newtonsoft.Json.Linq;
+namespace Apache.OpenWhisk.Tests.Dotnet
+{
+    public class HelloArray
+    {
+        public JArray Main(JObject args)
+        {
+            JArray jarray = new JArray();
+            jarray.Add("a");
+            jarray.Add("b");
+            return (jarray);
+        }
+    }
+}
+```
+You can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```csharp
+using System;
+using Newtonsoft.Json.Linq;
+namespace Apache.OpenWhisk.Tests.Dotnet
+{
+    public class HelloPassArrayParam
+    {
+        public JArray Main(JArray args)
+        {
+            return (args);
+        }
+    }
+}
+```
+
 ### Create the .NET Core Action
 
 You need to specify the name of the function handler using `--main` argument.
diff --git a/docs/actions-go.md b/docs/actions-go.md
index 94c11989d..5088a337e 100644
--- a/docs/actions-go.md
+++ b/docs/actions-go.md
@@ -58,6 +58,31 @@ func Main(obj map[string]interface{}) map[string]interface{} {
 }
 ```
 
+An action supports not only a JSON object but also a JSON array as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```go
+package main
+// Main is the function implementing the action
+func Main(event map[string]interface{}) []interface{} {
+        result := []interface{}{"a", "b"}
+        return result
+}
+```
+
+you can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```go
+package main
+// Main is the function implementing the action
+func Main(obj []interface{}) []interface{} {
+        return obj
+}
+```
+
 You can deploy it with just:
 
 ```
diff --git a/docs/actions-java.md b/docs/actions-java.md
index e62834480..9264e335d 100644
--- a/docs/actions-java.md
+++ b/docs/actions-java.md
@@ -48,6 +48,36 @@ public class Hello {
 }
 ```
 
+An action supports not only a JSON object but also a JSON array as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```java
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+public class HelloArray {
+    public static JsonArray main(JsonObject args) {
+        JsonArray jsonArray = new JsonArray();
+        jsonArray.add("a");
+        jsonArray.add("b");
+        return jsonArray;
+    }
+}
+```
+
+You can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```java
+import com.google.gson.JsonArray;
+public class Sort {
+    public static JsonArray main(JsonArray args) {
+        return args;
+    }
+}
+```
+
 Then, compile `Hello.java` into a JAR file `hello.jar` as follows:
 ```
 javac Hello.java
diff --git a/docs/actions-nodejs.md b/docs/actions-nodejs.md
index 5346c9340..7390f7605 100644
--- a/docs/actions-nodejs.md
+++ b/docs/actions-nodejs.md
@@ -31,6 +31,32 @@ and demonstrate how to bundle multiple JavaScript files and third party dependen
   }
   ```
 
+  An action supports not only a JSON object but also a JSON array as a return value.
+
+  It would be a simple example that uses an array as a return value:
+
+  ```javascript
+  function main(params) {
+    return ["a", "b"];
+  }
+  ```
+
+  You can also create a sequence action with actions accepting an array param and returning an array result.
+
+  You can easily figure out the parameters with the following example:
+
+  ```javascript
+  /**
+   * Sort a set of lines.
+   * @param lines An array of strings to sort.
+   */
+  function main(msg) {
+      var lines = msg || [];
+      lines.sort();
+      return lines;
+  }
+  ```
+
   The JavaScript file might contain additional functions.
   However, by convention, a function called `main` must exist to provide the entry point for the action.
 
diff --git a/docs/actions-php.md b/docs/actions-php.md
index d70a5442e..32babe6c1 100644
--- a/docs/actions-php.md
+++ b/docs/actions-php.md
@@ -47,6 +47,32 @@ function main(array $args) : array
 }
 ```
 
+An action supports not only a JSON object but also a JSON arary as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```php
+<?php
+function main(array $args) : array
+{
+    $arr=array("a","b","c");
+    return $arr;
+}
+```
+
+You can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```php
+<?php
+function main(array $args) : array
+{
+    $result = array_reverse($args);
+    return $result;
+}
+```
+
 PHP actions always consume an associative array and return an associative array.
 The entry method for the action is `main` by default but may be specified explicitly when creating
 the action with the `wsk` CLI using `--main`, as with any other action type.
diff --git a/docs/actions-python.md b/docs/actions-python.md
index 22c002d81..f3f000ca7 100644
--- a/docs/actions-python.md
+++ b/docs/actions-python.md
@@ -34,6 +34,24 @@ def main(args):
     return {"greeting": greeting}
 ```
 
+An action supports not only a JSON object but also a JSON array as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```python
+def main(args):
+    return ["a", "b"]
+```
+
+You can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```python
+def main(args):
+    return args
+```
+
 Python actions always consume a dictionary and produce a dictionary.
 The entry method for the action is `main` by default but may be specified explicitly when creating
 the action with the `wsk` CLI using `--main`, as with any other action type.
diff --git a/docs/actions-ruby.md b/docs/actions-ruby.md
index 0eb45b1a5..183c5179e 100644
--- a/docs/actions-ruby.md
+++ b/docs/actions-ruby.md
@@ -39,6 +39,27 @@ def main(args)
 end
 ```
 
+An action supports not only a JSON object but also a JSON array as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```ruby
+def main(args)
+  nums = Array["a","b"]
+  nums
+end
+```
+
+You can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```ruby
+def main(args)
+  args
+end
+```
+
 Ruby actions always consume a Hash and return a Hash.
 The entry method for the action is `main` by default but may be specified explicitly
 when creating the action with the `wsk` CLI using `--main`, as with any other action type.
diff --git a/docs/actions-rust.md b/docs/actions-rust.md
index 795c30b2e..82a692c5e 100644
--- a/docs/actions-rust.md
+++ b/docs/actions-rust.md
@@ -58,6 +58,37 @@ pub fn main(args: Value) -> Result<Value, Error> {
 
 Rust actions are mainly composed by a `main` function that accepts a JSON `serdes Value` as input and returns a `Result` including a JSON `serde Value`.
 
+An action supports not only a JSON object but also a JSON array as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```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)
+}
+```
+You can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```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),
+    }
+}
+```
+
 The entry method for the action is `main` by default but may be specified explicitly when creating
 the action with the `wsk` CLI using `--main`, as with any other action type.
 
diff --git a/docs/actions-swift.md b/docs/actions-swift.md
index c54a043be..ad1f240c2 100644
--- a/docs/actions-swift.md
+++ b/docs/actions-swift.md
@@ -35,8 +35,9 @@ An action is simply a top-level Swift function. For example, create a file calle
 `hello.swift` with the following content:
 
 ```swift
-func main(args: [String:Any]) -> [String:Any] {
-    if let name = args["name"] as? String {
+func main(args: Any) -> Any {
+    let dict = args as! [String:Any]
+    if let name = dict["name"] as? String {
         return [ "greeting" : "Hello \(name)!" ]
     } else {
         return [ "greeting" : "Hello stranger!" ]
@@ -45,6 +46,27 @@ func main(args: [String:Any]) -> [String:Any] {
 ```
 In this example the Swift action consumes a dictionary and produces a dictionary.
 
+An action supports not only a JSON object but also a JSON array as a return value.
+
+It would be a simple example that uses an array as a return value:
+
+```swift
+func main(args: Any) -> Any {
+    var arr = ["a", "b"]
+    return arr
+}
+```
+
+You can also create a sequence action with actions accepting an array param and returning an array result.
+
+You can easily figure out the parameters with the following example:
+
+```swift
+ func main(args: Any) -> Any {
+     return args
+ }
+```
+
 You can create an OpenWhisk action called `helloSwift` from this function as
 follows:
 
diff --git a/tests/dat/actions/unicode.tests/swift-4.2.txt b/tests/dat/actions/unicode.tests/swift-4.2.txt
index aff9c50a2..44f790222 100644
--- a/tests/dat/actions/unicode.tests/swift-4.2.txt
+++ b/tests/dat/actions/unicode.tests/swift-4.2.txt
@@ -15,8 +15,9 @@
  * limitations under the License.
  */
 
-func main(args: [String:Any]) -> [String:Any] {
-    if let str = args["delimiter"] as? String {
+func main(args: Any) -> Any {
+    let dict = args as! [String:Any]
+    if let str = dict["delimiter"] as? String {
         let msg = "\(str) ☃ \(str)"
         print(msg)
         return [ "winter" : msg ]
diff --git a/tests/dat/actions/unicode.tests/swift-5.1.txt b/tests/dat/actions/unicode.tests/swift-5.1.txt
index aff9c50a2..44f790222 100644
--- a/tests/dat/actions/unicode.tests/swift-5.1.txt
+++ b/tests/dat/actions/unicode.tests/swift-5.1.txt
@@ -15,8 +15,9 @@
  * limitations under the License.
  */
 
-func main(args: [String:Any]) -> [String:Any] {
-    if let str = args["delimiter"] as? String {
+func main(args: Any) -> Any {
+    let dict = args as! [String:Any]
+    if let str = dict["delimiter"] as? String {
         let msg = "\(str) ☃ \(str)"
         print(msg)
         return [ "winter" : msg ]
diff --git a/tests/dat/actions/unicode.tests/swift-5.3.txt b/tests/dat/actions/unicode.tests/swift-5.3.txt
index aff9c50a2..44f790222 100644
--- a/tests/dat/actions/unicode.tests/swift-5.3.txt
+++ b/tests/dat/actions/unicode.tests/swift-5.3.txt
@@ -15,8 +15,9 @@
  * limitations under the License.
  */
 
-func main(args: [String:Any]) -> [String:Any] {
-    if let str = args["delimiter"] as? String {
+func main(args: Any) -> Any {
+    let dict = args as! [String:Any]
+    if let str = dict["delimiter"] as? String {
         let msg = "\(str) ☃ \(str)"
         print(msg)
         return [ "winter" : msg ]