Skip to content
Permalink
Browse files

Merge pull request #259 from avast/issue225

Issue225
  • Loading branch information...
blast-hardcheese committed May 2, 2019
2 parents c31bad9 + 24bda3a commit 87b68e7e4655a7ab1fba06a4d279f1f706e470fc
@@ -77,10 +77,13 @@ object Http4sServerGenerator {
case GetExtraRouteParams(tracing) =>
for {
_ <- Target.log.debug("Http4sServerGenerator", "server")(s"getExtraRouteParams(${tracing})")
res <- if (tracing) {
Target.pure(List(param"""trace: String => Request[F] => TraceBuilder[F]"""))
} else Target.pure(List.empty)
} yield res
mapRoute = param"""mapRoute: (String, Request[F], F[Response[F]]) => F[Response[F]] = (_: String, _: Request[F], r: F[Response[F]]) => r"""
tracing <- if (tracing) {
Target.pure(Option(param"""trace: String => Request[F] => TraceBuilder[F]"""))
} else Target.pure(Option.empty)
} yield {
tracing.toList ::: List(mapRoute)
}

case GenerateSupportDefinitions(tracing) =>
Target.pure(List.empty)
@@ -501,8 +504,12 @@ object Http4sServerGenerator {
case Nil => responseInMatch
case generators => q"for {..${generators :+ enumerator"response <- $responseInMatch"}} yield response"
}
val routeBody = entityProcessor.fold[Term](responseInMatchInFor)(_.apply(responseInMatchInFor))
val fullRoute: Case = p"case req @ $fullRouteWithTracingMatcher => $routeBody"
val routeBody = entityProcessor.fold[Term](responseInMatchInFor)(_.apply(responseInMatchInFor))

val fullRoute: Case =
p"""case req @ $fullRouteWithTracingMatcher =>
mapRoute($operationId, req, {$routeBody})
"""

val respond: List[List[Term.Param]] = List(List(param"respond: $responseCompanionTerm.type"))

@@ -670,5 +677,6 @@ object Http4sServerGenerator {
def unapply(r: Request[F]): Option[(Request[F], TraceBuilder[F])] = Some(r -> $tracingField(r))
}
"""

}
}
@@ -48,24 +48,30 @@ class Issue165 extends FunSuite with Matchers with SwaggerSpecRunner {
}
"""
val resource = q"""
class StoreResource[F[_]]()(implicit F: Async[F]) extends Http4sDsl[F] {
class StoreResource[F[_]](mapRoute: (String, Request[F], F[Response[F]]) => F[Response[F]] = (_: String, _: Request[F], r: F[Response[F]]) => r)(implicit F: Async[F]) extends Http4sDsl[F] {
def routes(handler: StoreHandler[F]): HttpRoutes[F] = HttpRoutes.of {
{
case req @ GET -> Root =>
handler.getRoot(GetRootResponse)() flatMap {
case GetRootResponse.Ok =>
Ok()
}
case req @ GET -> Root =>
mapRoute("getRoot", req, {
handler.getRoot(GetRootResponse)() flatMap {
case GetRootResponse.Ok =>
Ok()
}
})
case req @ GET -> Root / "foo" =>
handler.getFoo(GetFooResponse)() flatMap {
case GetFooResponse.Ok =>
Ok()
}
mapRoute("getFoo", req, {
handler.getFoo(GetFooResponse)() flatMap {
case GetFooResponse.Ok =>
Ok()
}
})
case req @ GET -> Root / "foo" / "" =>
handler.getFooDir(GetFooDirResponse)() flatMap {
case GetFooDirResponse.Ok =>
Ok()
mapRoute("getFooDir", req, {
handler.getFooDir(GetFooDirResponse)() flatMap {
case GetFooDirResponse.Ok =>
Ok()
}
})
}
}
}
@@ -0,0 +1,62 @@
package core.issues

import com.twilio.guardrail.generators.Http4s
import com.twilio.guardrail.{ Context, Server, Servers }
import org.scalatest.{ FunSuite, Matchers }
import support.SwaggerSpecRunner

class Issue225 extends FunSuite with Matchers with SwaggerSpecRunner {

import scala.meta._

val swagger: String =
s"""
|swagger: '2.0'
|host: petstore.swagger.io
|paths:
| /{response}:
| parameters:
| - in: path
| name: response
| type: string
| get:
| operationId: getRoot
| responses:
| 200:
| description: description
|""".stripMargin

test("Ensure mapRoute is generated") {
val (_, _, Servers(Server(_, _, genHandler, genResource :: _) :: Nil, Nil)) = runSwaggerSpec(swagger)(Context.empty, Http4s)

val handler = q"""
trait Handler[F[_]] {
def getRoot(respond: GetRootResponse.type)(response: String): F[GetRootResponse]
}
"""
val resource = q"""
class Resource[F[_]](mapRoute: (String, Request[F], F[Response[F]]) => F[Response[F]] = (_: String, _: Request[F], r: F[Response[F]]) => r)(implicit F: Async[F]) extends Http4sDsl[F] {
def routes(handler: Handler[F]): HttpRoutes[F] = HttpRoutes.of {
{
case req @ GET -> Root / response =>
mapRoute("getRoot", req, {
handler.getRoot(GetRootResponse)(response) flatMap {
case GetRootResponse.Ok =>
Ok()
}
})
}
}
}
"""

compare(genHandler, handler)

// Cause structure is slightly different but source code is the same the value converted to string and then parsed
compare(genResource.toString().parse[Stat].get, resource)
}

private def compare(actual: Tree, expected: Tree): Unit = {
actual.structure shouldEqual expected.structure
}
}

0 comments on commit 87b68e7

Please sign in to comment.
You can’t perform that action at this time.