Skip to content

Commit

Permalink
Add document for support array result (#5311)
Browse files Browse the repository at this point in the history
* Fix test case failed for swift

* Add document based on support array result feature

* Apply review comments
  • Loading branch information
ningyougang committed Aug 18, 2022
1 parent 9005a08 commit 7de8bad
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 8 deletions.
18 changes: 18 additions & 0 deletions docs/actions-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```
Expand Down
40 changes: 40 additions & 0 deletions docs/actions-dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions docs/actions-go.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
Expand Down
30 changes: 30 additions & 0 deletions docs/actions-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions docs/actions-nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
26 changes: 26 additions & 0 deletions docs/actions-php.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions docs/actions-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions docs/actions-ruby.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions docs/actions-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
26 changes: 24 additions & 2 deletions docs/actions-swift.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!" ]
Expand All @@ -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:

Expand Down
5 changes: 3 additions & 2 deletions tests/dat/actions/unicode.tests/swift-4.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
5 changes: 3 additions & 2 deletions tests/dat/actions/unicode.tests/swift-5.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
5 changes: 3 additions & 2 deletions tests/dat/actions/unicode.tests/swift-5.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down

0 comments on commit 7de8bad

Please sign in to comment.