Skip to content

Commit

Permalink
Include bound path segments in request
Browse files Browse the repository at this point in the history
This makes it possible to access raw path parameters in a decorator.
  • Loading branch information
jodersky committed Nov 2, 2024
1 parent e7fcba1 commit 54395ba
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import de.tobiasroeser.mill.vcs.version.VcsVersion

val scala213 = "2.13.10"
val scala212 = "2.12.17"
val scala3 = "3.2.2"
val scala3 = "3.3.4"
val scalaJS = "1.13.0"
val communityBuildDottyVersion = sys.props.get("dottyVersion").toList

Expand Down
7 changes: 3 additions & 4 deletions cask/src/cask/main/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,17 @@ object Main{
.toList

dispatchTrie.lookup(decodedSegments, Vector()) match {
case None => Main.writeResponse(exchange, handleNotFound(Request(exchange, decodedSegments)))
case None => Main.writeResponse(exchange, handleNotFound(Request(exchange, decodedSegments, Map())))
case Some((methodMap, routeBindings, remaining)) =>
methodMap.get(effectiveMethod) match {
case None => Main.writeResponse(exchange, handleMethodNotAllowed(Request(exchange, remaining)))
case None => Main.writeResponse(exchange, handleMethodNotAllowed(Request(exchange, remaining, routeBindings)))
case Some((routes, metadata)) =>
val req = Request(exchange, remaining)
val req = Request(exchange, remaining, routeBindings)
Decorator.invoke(
req,
metadata.endpoint,
metadata.entryPoint.asInstanceOf[EntryPoint[Routes, _]],
routes,
routeBindings,
(mainDecorators ++ routes.decorators ++ metadata.decorators).toList,
Nil
) match {
Expand Down
2 changes: 1 addition & 1 deletion cask/src/cask/model/Params.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.undertow.server.handlers.CookieImpl
case class QueryParams(value: Map[String, collection.Seq[String]])
case class RemainingPathSegments(value: Seq[String])

case class Request(exchange: HttpServerExchange, remainingPathSegments: Seq[String])
case class Request(exchange: HttpServerExchange, remainingPathSegments: Seq[String], boundPathSegments: Map[String, String])
extends geny.ByteData with geny.Readable {
import collection.JavaConverters._
lazy val cookies: Map[String, Cookie] = {
Expand Down
5 changes: 2 additions & 3 deletions cask/src/cask/router/Decorators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,19 @@ object Decorator{
endpoint: Endpoint[_, _, _],
entryPoint: EntryPoint[T, _],
routes: T,
routeBindings: Map[String, String],
remainingDecorators: List[Decorator[_, _, _]],
bindings: List[Map[String, Any]]): Result[Any] = try {
remainingDecorators match {
case head :: rest =>
head.asInstanceOf[Decorator[Any, Any, Any]].wrapFunction(
ctx,
args => invoke(ctx, endpoint, entryPoint, routes, routeBindings, rest, args :: bindings)
args => invoke(ctx, endpoint, entryPoint, routes, rest, args :: bindings)
.asInstanceOf[Result[Nothing]]
)

case Nil =>
endpoint.wrapFunction(ctx, { (endpointBindings: Map[String, Any]) =>
val mergedEndpointBindings = endpointBindings ++ routeBindings.mapValues(endpoint.wrapPathSegment)
val mergedEndpointBindings = endpointBindings ++ ctx.boundPathSegments.mapValues(endpoint.wrapPathSegment)
val finalBindings = mergedEndpointBindings :: bindings

entryPoint
Expand Down

0 comments on commit 54395ba

Please sign in to comment.