Skip to content

Support array result include sequence action #150

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
Show file tree
Hide file tree
Changes from all 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
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,38 @@ The traditional support for the dictionary still works:
```swift
import Foundation

func main(args: [String:Any]) -> [String:Any] {
if let name = args["name"] as? String {
func main(args: Any) -> Any {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In order to support array result, from the new main signature. we can see, the the input param and outout param should be changed to Any both.

let dict = args as! [String:Any]
if let name = dict["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello stranger!" ]
}
}
```

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

So a very simple `hello array` function woule be:

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

```swift
func main(args: Any) -> Any {
return args
}
```
When invokes above action, we can pass an array object as the input parameter.

## Swift 5.x support

Some examples of using Codable In and Out
Expand Down
8 changes: 4 additions & 4 deletions core/swift51Action/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

# build go proxy from source
FROM golang:1.16 AS builder_source
FROM golang:1.18 AS builder_source
ARG GO_PROXY_GITHUB_USER=apache
ARG GO_PROXY_GITHUB_BRANCH=master
RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \
Expand All @@ -25,13 +25,13 @@ RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \
mv proxy /bin/proxy

# or build it from a release
FROM golang:1.16 AS builder_release
ARG GO_PROXY_RELEASE_VERSION=1.16@1.19.0
FROM golang:1.18 AS builder_release
ARG GO_PROXY_RELEASE_VERSION=1.18@1.20.0
RUN curl -sL \
https://github.com/apache/openwhisk-runtime-go/archive/{$GO_PROXY_RELEASE_VERSION}.tar.gz\
| tar xzf -\
&& cd openwhisk-runtime-go-*/main\
&& GO111MODULE=on go build -o /bin/proxy
&& GO111MODULE=on CGO_ENABLED=0 go build -o /bin/proxy

FROM swift:5.1.5

Expand Down
5 changes: 3 additions & 2 deletions core/swift51Action/main.swift
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 name = args["name"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let name = newArgs["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello stranger!" ]
Expand Down
4 changes: 2 additions & 2 deletions core/swift51Action/swiftbuild.py.launcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func _whisk_print_buffer(jsonString: String){
}

// snippet of code "injected" (wrapper code for invoking traditional main)
func _run_main(mainFunction: ([String: Any]) -> [String: Any], json: Data) -> Void {
func _run_main(mainFunction: (Any) -> Any, json: Data) -> Void {
do {
let parsed = try JSONSerialization.jsonObject(with: json, options: []) as! [String: Any]
let parsed = try JSONSerialization.jsonObject(with: json, options: [])
let result = mainFunction(parsed)
if JSONSerialization.isValidJSONObject(result) {
do {
Expand Down
8 changes: 4 additions & 4 deletions core/swift53Action/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

# build go proxy from source
FROM golang:1.16 AS builder_source
FROM golang:1.18 AS builder_source
ARG GO_PROXY_GITHUB_USER=apache
ARG GO_PROXY_GITHUB_BRANCH=master
RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \
Expand All @@ -25,13 +25,13 @@ RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \
mv proxy /bin/proxy

# or build it from a release
FROM golang:1.16 AS builder_release
ARG GO_PROXY_RELEASE_VERSION=1.16@1.19.0
FROM golang:1.18 AS builder_release
ARG GO_PROXY_RELEASE_VERSION=1.18@1.20.0
RUN curl -sL \
https://github.com/apache/openwhisk-runtime-go/archive/{$GO_PROXY_RELEASE_VERSION}.tar.gz\
| tar xzf -\
&& cd openwhisk-runtime-go-*/main\
&& GO111MODULE=on go build -o /bin/proxy
&& GO111MODULE=on CGO_ENABLED=0 go build -o /bin/proxy

FROM swift:5.3

Expand Down
5 changes: 3 additions & 2 deletions core/swift53Action/main.swift
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 name = args["name"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let name = newArgs["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello stranger!" ]
Expand Down
4 changes: 2 additions & 2 deletions core/swift53Action/swiftbuild.py.launcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func _whisk_print_buffer(jsonString: String){
}

// snippet of code "injected" (wrapper code for invoking traditional main)
func _run_main(mainFunction: ([String: Any]) -> [String: Any], json: Data) -> Void {
func _run_main(mainFunction: (Any) -> Any, json: Data) -> Void {
do {
let parsed = try JSONSerialization.jsonObject(with: json, options: []) as! [String: Any]
let parsed = try JSONSerialization.jsonObject(with: json, options: [])
let result = mainFunction(parsed)
if JSONSerialization.isValidJSONObject(result) {
do {
Expand Down
8 changes: 4 additions & 4 deletions core/swift54Action/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

# build go proxy from source
FROM golang:1.16 AS builder_source
FROM golang:1.18 AS builder_source
ARG GO_PROXY_GITHUB_USER=apache
ARG GO_PROXY_GITHUB_BRANCH=master
RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \
Expand All @@ -25,13 +25,13 @@ RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \
mv proxy /bin/proxy

# or build it from a release
FROM golang:1.16 AS builder_release
ARG GO_PROXY_RELEASE_VERSION=1.16@1.19.0
FROM golang:1.18 AS builder_release
ARG GO_PROXY_RELEASE_VERSION=1.18@1.20.0
RUN curl -sL \
https://github.com/apache/openwhisk-runtime-go/archive/{$GO_PROXY_RELEASE_VERSION}.tar.gz\
| tar xzf -\
&& cd openwhisk-runtime-go-*/main\
&& GO111MODULE=on go build -o /bin/proxy
&& GO111MODULE=on CGO_ENABLED=0 go build -o /bin/proxy

FROM swift:5.4

Expand Down
5 changes: 3 additions & 2 deletions core/swift54Action/main.swift
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 name = args["name"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let name = newArgs["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello stranger!" ]
Expand Down
4 changes: 2 additions & 2 deletions core/swift54Action/swiftbuild.py.launcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func _whisk_print_buffer(jsonString: String){
}

// snippet of code "injected" (wrapper code for invoking traditional main)
func _run_main(mainFunction: ([String: Any]) -> [String: Any], json: Data) -> Void {
func _run_main(mainFunction: (Any) -> Any, json: Data) -> Void {
do {
let parsed = try JSONSerialization.jsonObject(with: json, options: []) as! [String: Any]
let parsed = try JSONSerialization.jsonObject(with: json, options: [])
let result = mainFunction(parsed)
if JSONSerialization.isValidJSONObject(result) {
do {
Expand Down
7 changes: 4 additions & 3 deletions examples/swift-main-single/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
* limitations under the License.
*/

func main(args: [String:Any]) -> [String:Any] {
if let name = args["name"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let name = newArgs["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello swif4.2!" ]
}
}

func mainenv(args: [String: Any]) -> [String: Any] {
func mainenv(args: Any) -> Any {
let env = ProcessInfo.processInfo.environment
var a = "???"
var b = "???"
Expand Down
4 changes: 2 additions & 2 deletions examples/swift-main-zip/HelloSwift4/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# limitations under the License.
#

func main(args: [String:Any]) -> [String:Any] {
if let name = args["name"] as? String {
func main(args: Any) -> Any {
if let name = newArgs["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello stranger!" ]
Expand Down
5 changes: 3 additions & 2 deletions tests/dat/actions/HelloSwift5/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

import Foundation

func main(args: [String:Any]) -> [String:Any] {
if let name = args["name"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let name = newArgs["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello stranger!" ]
Expand Down
4 changes: 2 additions & 2 deletions tests/dat/actions/SwiftyRequest/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import SwiftyRequest
import Dispatch
import Foundation

func main(args: [String:Any]) -> [String:Any] {
func main(args: Any) -> Any {
var resp :[String:Any] = ["error":"Action failed"]
let echoURL = "http://httpbin.org/post"

// setting body data to {"Data":"string"}
let origJson: [String: Any] = args
let origJson = args as! [String:Any]
guard let data = try? JSONSerialization.data(withJSONObject: origJson, options: []) else {
return ["error": "Could not encode json"]
}
Expand Down
4 changes: 2 additions & 2 deletions tests/dat/actions/SwiftyRequest5/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import SwiftyRequest
import Dispatch
import Foundation

func main(args: [String:Any]) -> [String:Any] {
func main(args: Any) -> Any {
var resp :[String:Any] = ["error":"Action failed"]
let echoURL = "http://httpbin.org/post"

// setting body data to {"Data":"string"}
let origJson: [String: Any] = args
let origJson = args as! [String:Any]
guard let data = try? JSONSerialization.data(withJSONObject: origJson, options: []) else {
return ["error": "Could not encode json"]
}
Expand Down
11 changes: 6 additions & 5 deletions tests/dat/actions/sdk/swift4/createRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
* limitations under the License.
*/

func main(args: [String:Any]) -> [String:Any] {
if let baseUrl = args["baseUrl"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let baseUrl = newArgs["baseUrl"] as? String {
//Overriding WHISK API HOST using baseUrl, only applicable in testing with self sign ssl certs"
Whisk.baseUrl = baseUrl
}
guard let triggerName = args["triggerName"] as? String else {
guard let triggerName = newArgs["triggerName"] as? String else {
return ["error": "You must specify a triggerName parameter!"]
}
guard let actionName = args["actionName"] as? String else {
guard let actionName = newArgs["actionName"] as? String else {
return ["error": "You must specify a actionName parameter!"]
}
guard let ruleName = args["ruleName"] as? String else {
guard let ruleName = newArgs["ruleName"] as? String else {
return ["error": "You must specify a ruleName parameter!"]
}
print("Rule Name: \(ruleName), Trigger Name: \(triggerName), actionName: \(actionName)")
Expand Down
7 changes: 4 additions & 3 deletions tests/dat/actions/sdk/swift4/createTrigger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
* limitations under the License.
*/

func main(args: [String:Any]) -> [String:Any] {
if let baseUrl = args["baseUrl"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let baseUrl = newArgs["baseUrl"] as? String {
//Overriding WHISK API HOST using baseUrl, only applicable in testing with self sign ssl certs"
Whisk.baseUrl = baseUrl
}
guard let triggerName = args["triggerName"] as? String else {
guard let triggerName = newArgs["triggerName"] as? String else {
return ["error": "You must specify a triggerName parameter!"]
}
print("Trigger Name: \(triggerName)")
Expand Down
5 changes: 3 additions & 2 deletions tests/dat/actions/sdk/swift4/hello.swift
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 name = args["name"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let name = newArgs["name"] as? String {
return [ "greeting" : "Hello \(name)!" ]
} else {
return [ "greeting" : "Hello stranger!" ]
Expand Down
5 changes: 3 additions & 2 deletions tests/dat/actions/sdk/swift4/invoke.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ struct Activation: Decodable {
let activationId: String
}

func main(args: [String:Any]) -> [String:Any] {
if let baseUrl = args["baseUrl"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let baseUrl = newArgs["baseUrl"] as? String {
//Overriding WHISK API HOST using baseUrl, only applicable in testing with self sign ssl certs"
Whisk.baseUrl = baseUrl
}
Expand Down
5 changes: 3 additions & 2 deletions tests/dat/actions/sdk/swift4/invokeNonBlocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ struct Activation: Decodable {
let activationId: String
}

func main(args: [String:Any]) -> [String:Any] {
if let baseUrl = args["baseUrl"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let baseUrl = newArgs["baseUrl"] as? String {
//Overriding WHISK API HOST using baseUrl, only applicable in testing with self sign ssl certs"
Whisk.baseUrl = baseUrl
}
Expand Down
7 changes: 4 additions & 3 deletions tests/dat/actions/sdk/swift4/trigger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
* limitations under the License.
*/

func main(args: [String:Any]) -> [String:Any] {
if let baseUrl = args["baseUrl"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let baseUrl = newArgs["baseUrl"] as? String {
//Overriding WHISK API HOST using baseUrl, only applicable in testing with self sign ssl certs"
Whisk.baseUrl = baseUrl
}
if let triggerName = args["triggerName"] as? String {
if let triggerName = newArgs ["triggerName"] as? String {
print("Trigger Name: \(triggerName)")
return Whisk.trigger(eventNamed: triggerName, withParameters: [:])
} else {
Expand Down
11 changes: 6 additions & 5 deletions tests/dat/actions/sdk/swift5/createRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@

import Foundation

func main(args: [String:Any]) -> [String:Any] {
if let baseUrl = args["baseUrl"] as? String {
func main(args: Any) -> Any {
let newArgs = args as! [String:Any]
if let baseUrl = newArgs["baseUrl"] as? String {
//Overriding WHISK API HOST using baseUrl, only applicable in testing with self sign ssl certs"
Whisk.baseUrl = baseUrl
}
guard let triggerName = args["triggerName"] as? String else {
guard let triggerName = newArgs["triggerName"] as? String else {
return ["error": "You must specify a triggerName parameter!"]
}
guard let actionName = args["actionName"] as? String else {
guard let actionName = newArgs["actionName"] as? String else {
return ["error": "You must specify a actionName parameter!"]
}
guard let ruleName = args["ruleName"] as? String else {
guard let ruleName = newArgs["ruleName"] as? String else {
return ["error": "You must specify a ruleName parameter!"]
}
print("Rule Name: \(ruleName), Trigger Name: \(triggerName), actionName: \(actionName)")
Expand Down
Loading