GithubHelp home page GithubHelp logo

isabella232 / openwhisk-runtime-swift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apache/openwhisk-runtime-swift

0.0 0.0 0.0 26.56 MB

Apache OpenWhisk Runtime Swift supports Apache OpenWhisk functions written in Swift

Home Page: https://openwhisk.apache.org/

License: Apache License 2.0

Python 7.96% Swift 48.75% Scala 29.76% Shell 7.98% Dockerfile 4.52% Makefile 1.04%

openwhisk-runtime-swift's Introduction

Apache OpenWhisk runtimes for swift

License Build Status

Changelogs

Quick Swift Action

Simple swift action hello.swift

The traditional support for the dictionary still works:

import Foundation

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

Swift 5.x support

Some examples of using Codable In and Out

Codable style function signature

Create file helloCodableAsync.swift

import Foundation

// Domain model/entity
struct Employee: Codable {
  let id: Int?
  let name: String?
}
// codable main function
func main(input: Employee, respondWith: (Employee?, Error?) -> Void) -> Void {
    // For simplicity, just passing same Employee instance forward
    respondWith(input, nil)
}
wsk action update helloCodableAsync helloCodableAsync.swift swift:5.1
ok: updated action helloCodableAsync
wsk action invoke helloCodableAsync -r -p id 73 -p name Andrea
{
    "id": 73,
    "name": "Andrea"
}

Codable Error Handling

Create file helloCodableAsync.swift

import Foundation

struct Employee: Codable {
    let id: Int?
    let name: String?
}
enum VendingMachineError: Error {
    case invalidSelection
    case insufficientFunds(coinsNeeded: Int)
    case outOfStock
}
func main(input: Employee, respondWith: (Employee?, Error?) -> Void) -> Void {
    // Return real error
    do {
        throw VendingMachineError.insufficientFunds(coinsNeeded: 5)
    } catch {
        respondWith(nil, error)
    }
}
wsk action update helloCodableError helloCodableError.swift swift:5.1
ok: updated action helloCodableError
wsk action invoke helloCodableError -b -p id 51 -p name Carlos
{
  "name": "helloCodableError",
  "response": {
    "result": {
      "error": "insufficientFunds(5)"
    },
    "status": "application error",
    "success": false
  }
}

Packaging an action as a Swift executable using Swift 5.x

When you create an OpenWhisk Swift action with a Swift source file, it has to be compiled into a binary before the action is run. Once done, subsequent calls to the action are much faster until the container holding your action is purged. This delay is known as the cold-start delay.

To avoid the cold-start delay, you can compile your Swift file into a binary and then upload to OpenWhisk in a zip file. As you need the OpenWhisk scaffolding, the easiest way to create the binary is to build it within the same environment as it will be run in.

Compiling Swift 5.x

Compiling Swift 5.x single file

Use the docker container and pass the single source file as stdin. Pass the name of the method to the flag -compile

docker run -i openwhisk/action-swift-v5.1 -compile main <main.swift >../action.zip

Compiling Swift 5.1 multiple files with dependencies

Use the docker container and pass a zip archive containing a Package.swift and source files a main source file in the location Sources/main.swift.

zip - -r * | docker run -i openwhisk/action-swift-v5.1 -compile main >../action.zip

For more build examples see here

openwhisk-runtime-swift's People

Contributors

akrabat avatar andrea-scuderi avatar cbickel avatar chetanmeh avatar csantanapr avatar dgrove-oss avatar dubee avatar eweiter avatar falkzoll avatar fxulusoy avatar iainduncani avatar ioana-blue avatar jasonpet avatar jbampton avatar jeremiaswerner avatar jiangpengcheng avatar joachimvaldez avatar luke-roy-ibm avatar markusthoemmes avatar mdeuser avatar mrutkows avatar nwspeete-ibm avatar paulcastro avatar perryibm avatar psuter avatar rabbah avatar rsulzmann avatar sjfink avatar starpit avatar tysonnorris avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.