-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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` | ||||||||
``` | ||||||||
#!/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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated accordingly |
||||||||
``` | ||||||||
#!/bin/bash | ||||||||
echo $1 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
``` | ||||||||
|
||||||||
- Create an action from this shell script. | ||||||||
|
||||||||
``` | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -79,6 +79,47 @@ cd out | |||||||||||
zip -r -0 helloDotNet.zip * | ||||||||||||
``` | ||||||||||||
|
||||||||||||
For the return result, not only support `dictionary` but also support `array` | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated accordingly |
||||||||||||
|
||||||||||||
So a very simple `hello array` function would be: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
```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: | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||||||||||||||
|
||||||||||||||
```go | ||||||||||||||
package main | ||||||||||||||
// Main is the function implementing the action | ||||||||||||||
func Main(obj []interface{}) []interface{} { | ||||||||||||||
return obj | ||||||||||||||
} | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
You can deploy it with just: | ||||||||||||||
|
||||||||||||||
``` | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -48,6 +48,34 @@ public class Hello { | |||||||||||
} | ||||||||||||
``` | ||||||||||||
|
||||||||||||
Not only support return JsonObject but also support return JsonArray, the main function would be: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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 | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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. | ||||||||||||
|
||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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. | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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. | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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. | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```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. | ||||||||||||
|
||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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. | ||||||||||||
|
||||||||||||
For the return result, not only support `dictionary`, but also support `array` | ||||||||||||
|
||||||||||||
So a very simple `hello array` function woule be: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
```swift | ||||||||||||
func main(args: Any) -> Any { | ||||||||||||
return args | ||||||||||||
} | ||||||||||||
``` | ||||||||||||
|
||||||||||||
You can create an OpenWhisk action called `helloSwift` from this function as | ||||||||||||
follows: | ||||||||||||
|
||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated accordingly