Skip to content

Commit

Permalink
Route: Fix parsing of optional path elements
Browse files Browse the repository at this point in the history
  • Loading branch information
tindzk committed Jul 8, 2019
1 parent 89f7b4a commit e64e0fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion shared/src/main/scala/trail/Route.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object Route {
override def url(value: T): String = arg.codec.encode(value).getOrElse("")
override def parseInternal(path: Path): Option[(T, Path)] = {
val untilSlash = path.path.takeWhile(_ != '/')
arg.codec.decode(Some(untilSlash))
arg.codec.decode(if (untilSlash.isEmpty) None else Some(untilSlash))
.map(value => (value, path.copy(path =
path.path.drop(untilSlash.length + 1))))
}
Expand Down
13 changes: 13 additions & 0 deletions shared/src/test/scala/trail/RouteParseTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ class RouteParseTests extends FunSpec with Matchers {
assert(parsed === Some((("bob", true), 42)))
}

it("Route with optional path element can be parsed") {
val disk = Root / Arg[Option[Long]]
assert(disk.parse("/").contains(None))
assert(disk.parse("/42").contains(Some(42L)))
}

it("Route with optional path element can be parsed (2)") {
val disk = Root / "disk" / Arg[Option[Long]]
assert(disk.parse("/disk").contains(None))
assert(disk.parse("/disk/").contains(None))
assert(disk.parse("/disk/42").contains(Some(42L)))
}

it("Route can be parsed from string") {
val userInfo = Root / "user" / Arg[String] / Arg[Boolean]

Expand Down

0 comments on commit e64e0fc

Please sign in to comment.