diff --git a/docs/actions-docker.md b/docs/actions-docker.md index 788f7232488..2112746ce1b 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 09c293bf5b6..3ac75beac5f 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 94c11989d15..5088a337edb 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 e6283448010..9264e335d86 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 5346c934017..7390f7605dc 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 d70a5442e2a..32babe6c16f 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 + Result { 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 { + 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 { + 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 c54a043beb0..ad1f240c253 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 aff9c50a2dd..44f79022256 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 aff9c50a2dd..44f79022256 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 aff9c50a2dd..44f79022256 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 ]