Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue225 #259

Merged
merged 18 commits into from May 2, 2019
Merged

Issue225 #259

Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.