From 84000c335f5cdd2bb3994e7062f30836f2275b4d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 14 Feb 2013 17:37:27 -0600 Subject: 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 --- src/main/scala/router.scala | 21 +++++++++++++++++++-- 1 file 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 -- cgit v1.2.3-54-g00ecf