Skip to content

Parsing String parameter in path. #2

Answered by stephencelis
sean-howard asked this question in Q&A
Discussion options

You must be logged in to vote

@sean-howard You can use Parse(.string) to peel off an entire path component:

Route(.case(AppRoute.buyBook(id:))) {
  Path { "books"; Parse(.string); "buy" }
}

And if you use a RawRepresentable type-safe wrapper for the ID, you can use .string.representing(BookId.self):

struct BookId: RawRepresentable {
  var rawValue: String
}

enum AppRoute {
  case buyBook(id: BookId)
}

let appRouter = OneOf {

  // GET /books/:id/buy
  Route(.case(AppRoute.buyBook(id:))) {
    Path { "books"; Parse(.string.representing(BookId.self); "buy" }
  }
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@davdroman
Comment options

@stephencelis
Comment options

@sean-howard
Comment options

Answer selected by sean-howard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants