Skip to content

Add document for support array result #5311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/actions-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ DATE=`date`
echo "{ \"message\": \"Hello $NAME! It is $DATE.\" }"
```

For the return result, not only support `dictionary` but also support `array`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `dictionary` but also support `array`
Action support not only `dictionary` but also `array` as a return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly

```
#!/bin/bash
echo '["a", "b"]''
```

And support array result for sequence action as well, the first action's array result can be used as next action's input parameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
And support array result for sequence action as well, the first action's array result can be used as next action's input parameter
You can also create a sequence action with actions returning an array result.
You can easily figure out the parameters with the following example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly

```
#!/bin/bash
echo $1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sequence action's second action. can accept the array as input parameter, and we can do some sort operation here,
But here, i just return it directly(don't add sort operation), just a example.

Copy link
Contributor Author

@ningyougang ningyougang Aug 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there seems to be a bad point here, due to shell, it implies that any type of parameter can be received, but my example's exact meaning is, it here just can accept array type.

for some strong type language, e.g. java, we can know the input/out data type from main signature
image

```

- Create an action from this shell script.

```
Expand Down
41 changes: 41 additions & 0 deletions docs/actions-dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,47 @@ cd out
zip -r -0 helloDotNet.zip *
```

For the return result, not only support `dictionary` but also support `array`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `dictionary` but also support `array`
Action support not only a JSON object but also a JSON array as a return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly


So a very simple `hello array` function would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
So a very simple `hello array` function would be:
It would be a simple example that uses an array as a return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly


```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);
}
}
}
```

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
You can also create a sequence action with actions returning an array result.
You can easily figure out the parameters with the following example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly


```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{} {
}
```

For the return result, not only support `map[string]interface{}` but also support `[]interface{}`

So a very simple `hello array` function would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `map[string]interface{}` but also support `[]interface{}`
So a very simple `hello array` function would be:
Action support not only a JSON object but also a JSON array as a return value and the corresponding return types are `map[string]interface{}` and `[]interface{}` respectively.
This is a simple example.


```go
package main
// Main is the function implementing the action
func Main(event map[string]interface{}) []interface{} {
result := []interface{}{"a", "b"}
return result
}
```

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also create a sequence action with actions 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
28 changes: 28 additions & 0 deletions docs/actions-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ public class Hello {
}
```

Not only support return JsonObject but also support return JsonArray, the main function would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Not only support return JsonObject but also support return JsonArray, the main function would be:
Action support not only a JSON object but also a JSON 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;
}
}
```

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 would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 would be:
You can also create a sequence action with actions 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
}
```

For the return result, not only support `dictionary` but also support `array`

So a very simple `hello array` function would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `dictionary` but also support `array`
So a very simple `hello array` function would be:
Action support not only a JSON object but also a JSON array as a return value.


```javascript
function main(params) {
return ["a", "b"];
}
```

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
You can also create a sequence action with actions returning an array result.
The following is a good example to sort parameters in an array.


```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
}
```

For the return result, not only support `dictionary` but also support `array`

So a very simple `hello array` function would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `dictionary` but also support `array`
So a very simple `hello array` function would be:
Action support not only a JSON object but also a JSON array as a return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly


```php
<?php
function main(array $args) : array
{
$arr=array("a","b","c");
return $arr;
}
```

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
You can also create a sequence action with actions returning an array result.
The following is a good example to reverse parameters in an array.


```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
16 changes: 16 additions & 0 deletions docs/actions-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,23 @@ def main(args):
print(greeting)
return {"greeting": greeting}
```
For the return result, not only support `dictionary` but also support `array`

So a very simple `hello array` function would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `dictionary` but also support `array`
So a very simple `hello array` function would be:
Action support not only a JSON object but also a JSON array as a return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly


```python
def main(args):
return ["a", "b"]
```

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
You can also create a sequence action with actions 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
```

For the return result, not only support `dictionary` but also support `array`

So a very simple `hello array` function would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `dictionary` but also support `array`
So a very simple `hello array` function would be:
Action support not only a JSON object but also a JSON array as a return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly


```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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
You can also create a sequence action with actions 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
32 changes: 32 additions & 0 deletions docs/actions-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ 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`.

For the return result, not only support `A JSON serde Value` but also support `Array serde Value`

So a simple `hello array` funtion would be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `A JSON serde Value` but also support `Array serde Value`
So a simple `hello array` funtion would be:
Action support not only a JSON object but also a JSON array as a return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly


```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)
}
```

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
You can also create a sequence action with actions 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.

For the return result, not only support `dictionary`, but also support `array`

So a very simple `hello array` function woule be:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the return result, not only support `dictionary`, but also support `array`
So a very simple `hello array` function woule be:
Action support not only a JSON object but also a JSON array as a return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly


```swift
func main(args: Any) -> Any {
var arr = ["a", "b"]
return arr
}
```

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
You can also create a sequence action with actions 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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found relative test cases failed in our downstream, so need to fix it in upstream firstly.

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