Skip to content

Commit

Permalink
feat: Add codable identifier arrays (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Lees11 authored and ianpartridge committed Mar 3, 2018
1 parent 6419b27 commit a493848
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Sources/KituraContracts/ClosureAliases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ The `CodableArrayResultClosure` is used by other `Codable` aliases when respondi
*/
public typealias CodableArrayResultClosure<O: Codable> = ([O]?, RequestError?) -> Void

/**
The `IdentifierCodableArrayResultClosure` is used by other `Codable` aliases when responding with an array of tuples containing an identifier and a Codable object, or an `RequestError`.
*/
public typealias IdentifierCodableArrayResultClosure<Id: Identifier, O: Codable> = ([(Id, O)]?, RequestError?) -> Void

/**
The `IdentifierCodableResultClosure` is used by other `Codable` aliases when responding with an object which conforms to Codable and/or an object that conforms to `Identifier` or `RequestError` is needed.
*/
Expand Down Expand Up @@ -202,6 +207,31 @@ router.get("/users") { (respondWith: ([User]?, RequestError?) -> Void) in
*/
public typealias CodableArrayClosure<O: Codable> = (@escaping CodableArrayResultClosure<O>) -> Void

/**
The `IdentifierCodableArrayClosure` is for use in cases where you'd want to perform a series of actions then respond with an array of tuples containing an identifier and a Codable object, or an `RequestError`, in the form of a `IdentifierCodableArrayResultClosure`.

### Usage Example: ###
````
router.get("/users") { (respondWith: ([(Int, User)]?, RequestError?) -> Void) in
if databaseConnectionIsOk {

...

//If no errors occured and you have an array of Users you can just respond with the users by passing nil as the 'RequestError?' value.
respondWith([(Int, User)], nil)

} else {

...

//If there has been an error you can use the respondWith call to respond with an appropiate error and passing nil for the [User]?.
respondWith(nil, .internalServerError)
}
}
````
*/
public typealias IdentifierCodableArrayClosure<Id: Identifier, O: Codable> = (@escaping IdentifierCodableArrayResultClosure<Id, O>) -> Void

/**
The `SimpleCodableClosure` is for use in cases where you'd want to perform a series of actions then respond with an object conforming to `Codable` or a `RequestError` in the form of a `CodableResultClosure`.

Expand Down

0 comments on commit a493848

Please sign in to comment.