You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/02/14 07:30:58 UTC
[GitHub] csantanapr opened a new pull request #23: Add Codable Support for Swift 4.x
csantanapr opened a new pull request #23: Add Codable Support for Swift 4.x
URL: https://github.com/apache/incubator-openwhisk-runtime-swift/pull/23
## Codable Suppor with Swift 4.x
Some examples of using Codable In and Out
### Codable style function signature
Create file `helloCodableAsync.swift`
```swift
// 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:4.1
```
ok: updated action helloCodableAsync
```
wsk action invoke helloCodableAsync -r -p id 42 -p name Carlos
```
```json
{
"id": 42,
"name": "Carlos"
}
```
### Codable Error Handling
Create file `helloCodableAsync.swift`
```swift
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:4.1
```
ok: updated action helloCodableError
```
wsk action invoke helloCodableError -b -p id 42 -p name Carlos
```
```json
{
"name": "helloCodableError",
"response": {
"result": {
"error": "insufficientFunds(5)"
},
"status": "application error",
"success": false
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services