You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
If you want to return an error message, return an object with an `error` property with the message.
395
395
396
-
## Writing Functions - Pre-Compiled Swift Binaries
396
+
### Codable Support
397
+
398
+
Swift 4 runtimes support [Codable types](https://developer.apple.com/documentation/swift/codable) to handle the converting between JSON input parameters and response types to native Swift types.
// For simplicity, just passing same Employee instance forward
408
+
respondWith(input, nil)
409
+
}
410
+
```
411
+
412
+
### Pre-Compiled Swift Binaries
397
413
398
414
OpenWhisk supports creating Swift actions from a pre-compiled binary. This reduces startup time for Swift actions by removing the need for a dynamic compilation step.
399
415
400
-
In the `serverless.yaml` file, the `handler` property can refer to the compiled binary file produced by the build.
416
+
In the `serverless.yaml` file, the `handler` property can refer to the zip file containing a binary file produced by the build.
401
417
402
418
```yaml
403
419
functions:
404
420
hello:
405
-
handler: .build/release/Hello
421
+
handler: action.zip
406
422
```
407
423
408
-
This configuration will generate the deployment package for that function containing only this binary file. It will not include other local files, e.g. Swift source files.
409
-
410
-
Pre-compiled Swift actions must be compatible with the platform runtime and architecture. There is an [open-source Swift package](https://packagecatalog.com/package/jthomas/OpenWhiskAction) (`OpenWhiskAction`) that handles wrapping functions within a shim to support runtime execution.
424
+
Compiling a single Swift file to a binary can be handled using this Docker command with the OpenWhisk Swift runtime image. `main.swift` is the file containing the swift code and `action.zip` is the zip archive produced.
411
425
412
426
```
413
-
import OpenWhiskAction
414
-
415
-
func hello(args: [String:Any]) -> [String:Any] {
416
-
if let name = args["name"] as? String {
417
-
return [ "greeting" : "Hello \(name)!" ]
418
-
} else {
419
-
return [ "greeting" : "Hello stranger!" ]
420
-
}
421
-
}
422
-
423
-
OpenWhiskAction(main: hello)
427
+
docker run -i openwhisk/action-swift-v4.2 -compile main < main.swift > action.zip
424
428
```
425
429
426
-
Binaries produced by the Swift build process must be generated for the correct platform architecture. This Docker command will compile Swift sources files using the relevant Swift environment.
430
+
Swift packages containing multiple source files with a package descriptor (`Package.swift` ) can be built using the following command.
427
431
428
432
```
429
-
docker run --rm -it -v $(pwd):/swift-package openwhisk/action-swift-v3.1.1 bash -e -c "cd /swift-package && swift build -v -c release"
433
+
zip - -r * | docker run -i openwhisk/action-swift-v4.2 -compile main > action.zip
0 commit comments