Skip to content

Instantly share code, notes, and snippets.

@tanner0101
Created January 11, 2019 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanner0101/d17c02bfff784ab33f73f121a5279890 to your computer and use it in GitHub Desktop.
Save tanner0101/d17c02bfff784ab33f73f121a5279890 to your computer and use it in GitHub Desktop.
r.get("posts", ":postID") { req in
let postID = try req.parameters.get(":postID")
print(postID) // String
...
}
enum PathComponent {
case part(String)
case dynamic(name: String)
case anything
case catchall
}
extension PathComponent: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
if value.hasPrefix(":") {
self = .dynamic(name: value[1...])
} else if value == ":" {
self = .anything
} else if value == "*" {
self = .catchall
} else {
self = .part(value)
}
}
}
extension PathComponent {
static var postID: PathComponent {
return ":postID"
}
}
extension PathComponent {
static var postID: PathComponent {
return .dynamic(name: "postID")
}
}
r.get("posts", .postID) { req in
let postID = try req.parameters.get(.postID)
print(postID) // String
...
}
try req.parameters.get(.postID, as: Int.self)
return try Post.find(req.parameters.get(.postID), on: self.db).first()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment