diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-02-14 17:37:27 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-02-14 17:44:28 -0600 |
commit | 84000c335f5cdd2bb3994e7062f30836f2275b4d (patch) | |
tree | c2a11ec53cceece5c317c2ba69eb87627457e93f | |
parent | edb531391b56eb964e4cfe513df9f4ac5e6fbcf4 (diff) | |
download | scala-path-router-84000c335f5cdd2bb3994e7062f30836f2275b4d.tar.gz scala-path-router-84000c335f5cdd2bb3994e7062f30836f2275b4d.zip |
a couple more edge cases for filling in a path template
if a mapping value matches the default value for an optional variable
component, then the optional component should just be left out
if the mapping contains a value that doesn't match the corresponding
value in the route's list of defaults, then it shouldn't return anything
-rw-r--r-- | src/main/scala/router.scala | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/main/scala/router.scala b/src/main/scala/router.scala index 2bf8bfd..a9b2ec1 100644 --- a/src/main/scala/router.scala +++ b/src/main/scala/router.scala @@ -93,9 +93,24 @@ class Route[T] ( override def toString = path def pathWithMapping (mapping: Map[String, String]): Option[String] = { - if (requiredVariableComponents.forall(mapping.isDefinedAt)) { + val requiredDefaults = defaults.keys.filter { k => + mapping.isDefinedAt(k) && !hasVariable(k) + } + if (requiredDefaults.forall(k => defaults(k) == mapping(k)) && + requiredVariableComponents.forall(mapping.isDefinedAt)) { val boundComponents = components.flatMap { - case Optional(v) => (mapping get v).map(validComponentValue(v, _)) + case Optional(v) => { + val component = (mapping get v).flatMap(validComponentValue(v, _)) + defaults get v match { + case Some(default) => { + component.flatMap { + case `default` => None + case c => Some(c) + } + } + case None => component + } + } case Variable(v) => validComponentValue(v, (mapping(v))) case literal => Some(literal) } @@ -115,6 +130,8 @@ class Route[T] ( lazy val optionalVariableComponents = components.filter(isOptional).flatMap(getComponentName) + lazy val hasVariable = components.flatMap(getComponentName).toSet + private val Optional = """^\?:(.*)$""".r private val Variable = """^\??:(.*)$""".r |