Skip to content

Commit

Permalink
Fix index calculation of default parameters
Browse files Browse the repository at this point in the history
In case of multiple parameter lists the list index must also be taken into account
  • Loading branch information
jodersky committed Dec 12, 2019
1 parent ba1ea6d commit 0488b09
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cask/src/cask/router/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class Macros[C <: blackbox.Context](val c: C) {
val argReader = argReaders.lift(argListIndex).getOrElse(q"cask.router.NoOpParser.instanceAny")
val flattenedArgLists = method.paramss(argListIndex)
def hasDefault(i: Int) = {
val defaultName = s"${method.name}$$default$$${i + 1}"
// defaults are numbered globally on a class-level, this means that we
// must take into account the parameter index *as well as the parameter list index*
val defaultIdx = i + argListIndex + 1
val defaultName = s"${method.name}$$default$$$defaultIdx"
if (curCls.members.exists(_.name.toString == defaultName)) Some(defaultName)
else None
}
Expand Down
5 changes: 5 additions & 0 deletions example/decorated/app/src/Decorated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ object Decorated extends cask.MainRoutes{
world + user
}

@loggedIn()
@cask.get("/hello-default")
def defaults(world: String = "world")(user: User) = {
world + user
}
initialize()
}
4 changes: 3 additions & 1 deletion example/decorated/app/test/src/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ object ExampleTests extends TestSuite{
requests.get(s"$host/hello/woo").text() ==> "woo31337"
requests.get(s"$host/internal/boo").text() ==> "boo[haoyi]"
requests.get(s"$host/internal-extra/goo").text() ==> "goo[haoyi]31337"

requests.get(s"$host/internal-extra/goo").text() ==> "goo[haoyi]31337"
requests.get(s"$host/hello-default?world=worldz").text() ==> "worldz[haoyi]"
requests.get(s"$host/hello-default").text() ==> "world[haoyi]"
}
}
}

0 comments on commit 0488b09

Please sign in to comment.