summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-02-14 11:02:44 -0600
committerJesse Luehrs <doy@tozt.net>2013-02-14 11:02:44 -0600
commitb1b55f620a72a13ffd02b6e76f92d97a1a82748c (patch)
tree749781336706ff3ed41a1b5675e91e44139b9073
parentd00a11fd101e0894f1279dad3c19c6dcdaf71f6b (diff)
downloadscala-path-router-b1b55f620a72a13ffd02b6e76f92d97a1a82748c.tar.gz
scala-path-router-b1b55f620a72a13ffd02b6e76f92d97a1a82748c.zip
implement optionals
-rw-r--r--src/main/scala/router.scala9
-rw-r--r--src/test/scala/basic.scala15
2 files changed, 14 insertions, 10 deletions
diff --git a/src/main/scala/router.scala b/src/main/scala/router.scala
index f1fbfa7..fbbd052 100644
--- a/src/main/scala/router.scala
+++ b/src/main/scala/router.scala
@@ -58,7 +58,7 @@ class Route[T] (
components: Seq[String] = components,
mapping: Map[String, String] = defaults
): Option[Match[T]] = {
- if (components.length == 0 && parts.length == 0) {
+ if (components.filter(!isOptional(_)).length == 0 && parts.length == 0) {
Some(new Match[T](path, mapping, target))
}
else if (components.length == 0 || parts.length == 0) {
@@ -67,7 +67,12 @@ class Route[T] (
else {
components.head match {
case Optional(name) => {
- throw new Error("unsupported")
+ if (validate(name, parts.head)) {
+ route(parts.tail, components.tail, mapping + (name -> parts.head))
+ }
+ else {
+ route(parts, components.tail, mapping)
+ }
}
case Variable(name) => {
if (validate(name, parts.head)) {
diff --git a/src/test/scala/basic.scala b/src/test/scala/basic.scala
index 15280e5..070ace8 100644
--- a/src/test/scala/basic.scala
+++ b/src/test/scala/basic.scala
@@ -131,13 +131,12 @@ class Basic extends FunSuite {
)
)
- // TODO support optionals
- // testRoute(
- // router, "test/x1", Map(
- // "controller" -> "test",
- // "x" -> "x1",
- // "y" -> "y"
- // )
- // )
+ testRoute(
+ router, "test/x1", Map(
+ "controller" -> "test",
+ "x" -> "x1",
+ "y" -> "y"
+ )
+ )
}
}