Skip to content

Commit e8a2b7c

Browse files
ningyougangmichele-sciabarra
authored andcommitted
Add document for support array result (apache#5311)
* Fix test case failed for swift * Add document based on support array result feature * Apply review comments
1 parent 9344d63 commit e8a2b7c

13 files changed

+268
-8
lines changed

docs/actions-docker.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ DATE=`date`
236236
echo "{ \"message\": \"Hello $NAME! It is $DATE.\" }"
237237
```
238238

239+
An action supports not only a JSON object but also a JSON array as a return value.
240+
241+
It would be a simple example that uses an array as a return value:
242+
243+
```
244+
#!/bin/bash
245+
echo '["a", "b"]''
246+
```
247+
248+
You can also create a sequence action with actions accepting an array param and returning an array result.
249+
250+
You can easily figure out the parameters with the following example:
251+
252+
```
253+
#!/bin/bash
254+
echo $1
255+
```
256+
239257
- Create an action from this shell script.
240258

241259
```

docs/actions-dotnet.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,46 @@ cd out
7979
zip -r -0 helloDotNet.zip *
8080
```
8181

82+
An action supports not only a JSON object but also a JSON array as a return value.
83+
84+
It would be a simple example that uses an array as a return value:
85+
86+
```csharp
87+
using System;
88+
using Newtonsoft.Json.Linq;
89+
namespace Apache.OpenWhisk.Tests.Dotnet
90+
{
91+
public class HelloArray
92+
{
93+
public JArray Main(JObject args)
94+
{
95+
JArray jarray = new JArray();
96+
jarray.Add("a");
97+
jarray.Add("b");
98+
return (jarray);
99+
}
100+
}
101+
}
102+
```
103+
You can also create a sequence action with actions accepting an array param and returning an array result.
104+
105+
You can easily figure out the parameters with the following example:
106+
107+
```csharp
108+
using System;
109+
using Newtonsoft.Json.Linq;
110+
namespace Apache.OpenWhisk.Tests.Dotnet
111+
{
112+
public class HelloPassArrayParam
113+
{
114+
public JArray Main(JArray args)
115+
{
116+
return (args);
117+
}
118+
}
119+
}
120+
```
121+
82122
### Create the .NET Core Action
83123

84124
You need to specify the name of the function handler using `--main` argument.

docs/actions-go.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ func Main(obj map[string]interface{}) map[string]interface{} {
5858
}
5959
```
6060

61+
An action supports not only a JSON object but also a JSON array as a return value.
62+
63+
It would be a simple example that uses an array as a return value:
64+
65+
```go
66+
package main
67+
// Main is the function implementing the action
68+
func Main(event map[string]interface{}) []interface{} {
69+
result := []interface{}{"a", "b"}
70+
return result
71+
}
72+
```
73+
74+
you can also create a sequence action with actions accepting an array param and returning an array result.
75+
76+
You can easily figure out the parameters with the following example:
77+
78+
```go
79+
package main
80+
// Main is the function implementing the action
81+
func Main(obj []interface{}) []interface{} {
82+
return obj
83+
}
84+
```
85+
6186
You can deploy it with just:
6287

6388
```

docs/actions-java.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ public class Hello {
4848
}
4949
```
5050

51+
An action supports not only a JSON object but also a JSON array as a return value.
52+
53+
It would be a simple example that uses an array as a return value:
54+
55+
```java
56+
import com.google.gson.JsonArray;
57+
import com.google.gson.JsonObject;
58+
public class HelloArray {
59+
public static JsonArray main(JsonObject args) {
60+
JsonArray jsonArray = new JsonArray();
61+
jsonArray.add("a");
62+
jsonArray.add("b");
63+
return jsonArray;
64+
}
65+
}
66+
```
67+
68+
You can also create a sequence action with actions accepting an array param and returning an array result.
69+
70+
You can easily figure out the parameters with the following example:
71+
72+
```java
73+
import com.google.gson.JsonArray;
74+
public class Sort {
75+
public static JsonArray main(JsonArray args) {
76+
return args;
77+
}
78+
}
79+
```
80+
5181
Then, compile `Hello.java` into a JAR file `hello.jar` as follows:
5282
```
5383
javac Hello.java

docs/actions-nodejs.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ and demonstrate how to bundle multiple JavaScript files and third party dependen
3131
}
3232
```
3333

34+
An action supports not only a JSON object but also a JSON array as a return value.
35+
36+
It would be a simple example that uses an array as a return value:
37+
38+
```javascript
39+
function main(params) {
40+
return ["a", "b"];
41+
}
42+
```
43+
44+
You can also create a sequence action with actions accepting an array param and returning an array result.
45+
46+
You can easily figure out the parameters with the following example:
47+
48+
```javascript
49+
/**
50+
* Sort a set of lines.
51+
* @param lines An array of strings to sort.
52+
*/
53+
function main(msg) {
54+
var lines = msg || [];
55+
lines.sort();
56+
return lines;
57+
}
58+
```
59+
3460
The JavaScript file might contain additional functions.
3561
However, by convention, a function called `main` must exist to provide the entry point for the action.
3662

docs/actions-php.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@ function main(array $args) : array
4747
}
4848
```
4949

50+
An action supports not only a JSON object but also a JSON arary as a return value.
51+
52+
It would be a simple example that uses an array as a return value:
53+
54+
```php
55+
<?php
56+
function main(array $args) : array
57+
{
58+
$arr=array("a","b","c");
59+
return $arr;
60+
}
61+
```
62+
63+
You can also create a sequence action with actions accepting an array param and returning an array result.
64+
65+
You can easily figure out the parameters with the following example:
66+
67+
```php
68+
<?php
69+
function main(array $args) : array
70+
{
71+
$result = array_reverse($args);
72+
return $result;
73+
}
74+
```
75+
5076
PHP actions always consume an associative array and return an associative array.
5177
The entry method for the action is `main` by default but may be specified explicitly when creating
5278
the action with the `wsk` CLI using `--main`, as with any other action type.

docs/actions-python.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ def main(args):
3434
return {"greeting": greeting}
3535
```
3636

37+
An action supports not only a JSON object but also a JSON array as a return value.
38+
39+
It would be a simple example that uses an array as a return value:
40+
41+
```python
42+
def main(args):
43+
return ["a", "b"]
44+
```
45+
46+
You can also create a sequence action with actions accepting an array param and returning an array result.
47+
48+
You can easily figure out the parameters with the following example:
49+
50+
```python
51+
def main(args):
52+
return args
53+
```
54+
3755
Python actions always consume a dictionary and produce a dictionary.
3856
The entry method for the action is `main` by default but may be specified explicitly when creating
3957
the action with the `wsk` CLI using `--main`, as with any other action type.

docs/actions-ruby.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ def main(args)
3939
end
4040
```
4141

42+
An action supports not only a JSON object but also a JSON array as a return value.
43+
44+
It would be a simple example that uses an array as a return value:
45+
46+
```ruby
47+
def main(args)
48+
nums = Array["a","b"]
49+
nums
50+
end
51+
```
52+
53+
You can also create a sequence action with actions accepting an array param and returning an array result.
54+
55+
You can easily figure out the parameters with the following example:
56+
57+
```ruby
58+
def main(args)
59+
args
60+
end
61+
```
62+
4263
Ruby actions always consume a Hash and return a Hash.
4364
The entry method for the action is `main` by default but may be specified explicitly
4465
when creating the action with the `wsk` CLI using `--main`, as with any other action type.

docs/actions-rust.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,37 @@ pub fn main(args: Value) -> Result<Value, Error> {
5858

5959
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`.
6060

61+
An action supports not only a JSON object but also a JSON array as a return value.
62+
63+
It would be a simple example that uses an array as a return value:
64+
65+
```rust
66+
extern crate serde_json;
67+
use serde_derive::{Deserialize, Serialize};
68+
use serde_json::{Error, Value};
69+
pub fn main(args: Value) -> Result<Value, Error> {
70+
let output = ["a", "b"];
71+
serde_json::to_value(output)
72+
}
73+
```
74+
You can also create a sequence action with actions accepting an array param and returning an array result.
75+
76+
You can easily figure out the parameters with the following example:
77+
78+
```rust
79+
extern crate serde_json;
80+
use serde_derive::{Deserialize, Serialize};
81+
use serde_json::{Error, Value};
82+
pub fn main(args: Value) -> Result<Value, Error> {
83+
let inputParam = args.as_array();
84+
let defaultOutput = ["c", "d"];
85+
match inputParam {
86+
None => serde_json::to_value(defaultOutput),
87+
Some(x) => serde_json::to_value(x),
88+
}
89+
}
90+
```
91+
6192
The entry method for the action is `main` by default but may be specified explicitly when creating
6293
the action with the `wsk` CLI using `--main`, as with any other action type.
6394

docs/actions-swift.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ An action is simply a top-level Swift function. For example, create a file calle
3535
`hello.swift` with the following content:
3636

3737
```swift
38-
func main(args: [String:Any]) -> [String:Any] {
39-
if let name = args["name"] as? String {
38+
func main(args: Any) -> Any {
39+
let dict = args as! [String:Any]
40+
if let name = dict["name"] as? String {
4041
return [ "greeting" : "Hello \(name)!" ]
4142
} else {
4243
return [ "greeting" : "Hello stranger!" ]
@@ -45,6 +46,27 @@ func main(args: [String:Any]) -> [String:Any] {
4546
```
4647
In this example the Swift action consumes a dictionary and produces a dictionary.
4748

49+
An action supports not only a JSON object but also a JSON array as a return value.
50+
51+
It would be a simple example that uses an array as a return value:
52+
53+
```swift
54+
func main(args: Any) -> Any {
55+
var arr = ["a", "b"]
56+
return arr
57+
}
58+
```
59+
60+
You can also create a sequence action with actions accepting an array param and returning an array result.
61+
62+
You can easily figure out the parameters with the following example:
63+
64+
```swift
65+
func main(args: Any) -> Any {
66+
return args
67+
}
68+
```
69+
4870
You can create an OpenWhisk action called `helloSwift` from this function as
4971
follows:
5072

0 commit comments

Comments
 (0)