paulcastro commented on a change in pull request #23: Add Codable Support for 
Swift 4.x
URL: 
https://github.com/apache/incubator-openwhisk-runtime-swift/pull/23#discussion_r172728354
 
 

 ##########
 File path: README.md
 ##########
 @@ -39,6 +39,76 @@ func main(args: [String:Any]) -> [String:Any] {
 ```
 
 ## Swift 4.x support
+
+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
+}
+```
+
 ### Packaging an action as a Swift executable using Swift 4
 
 Review comment:
   Hmmmm, can't edit each line individually.  Here are some typos:
   
   Line 209: `All your source code needs to be copied ... `
   Line 231: `Take into account that starting ...`

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to