Skip to content

Commit

Permalink
removes the macro params that can be inferred
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaparadela committed Feb 22, 2019
1 parent 20aae64 commit 15abe94
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ sealed abstract class CompressionType extends Product with Serializable
case object Identity extends CompressionType
case object Gzip extends CompressionType

class message extends StaticAnnotation
class option(name: String, value: Any) extends StaticAnnotation
class outputPackage(value: String) extends StaticAnnotation
class outputName(value: String) extends StaticAnnotation
class http(method: HttpMethod, uri: String) extends StaticAnnotation
class message extends StaticAnnotation
class option(name: String, value: Any) extends StaticAnnotation
class outputPackage(value: String) extends StaticAnnotation
class outputName(value: String) extends StaticAnnotation
class http extends StaticAnnotation

sealed trait HttpMethod extends Product with Serializable
case object OPTIONS extends HttpMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,46 +268,158 @@ class GreeterRestTests

"Auto-derived REST Client" should {

val unaryClient: UnaryGreeter.HttpClient[IO] =
UnaryGreeter.httpClient[IO](serviceUri / UnaryServicePrefix)

"serve a GET request" in {
val client: UnaryGreeter.HttpClient[IO] =
UnaryGreeter.httpClient[IO](serviceUri / UnaryServicePrefix)
val response: IO[HelloResponse] = BlazeClientBuilder[IO](ec).resource.use(client.getHello(_))
val response: IO[HelloResponse] =
BlazeClientBuilder[IO](ec).resource.use(unaryClient.getHello(_))
response.unsafeRunSync() shouldBe HelloResponse("hey")
}

"serve a unary POST request" in {
val client: UnaryGreeter.HttpClient[IO] =
UnaryGreeter.httpClient[IO](serviceUri / UnaryServicePrefix)
val response: IO[HelloResponse] =
BlazeClientBuilder[IO](ec).resource.use(client.sayHello(HelloRequest("hey"))(_))
BlazeClientBuilder[IO](ec).resource.use(unaryClient.sayHello(HelloRequest("hey"))(_))
response.unsafeRunSync() shouldBe HelloResponse("hey")
}

"handle a raised gRPC exception in a unary POST request" in {

val client: UnaryGreeter.HttpClient[IO] =
UnaryGreeter.httpClient[IO](serviceUri / UnaryServicePrefix)

val response: IO[HelloResponse] =
BlazeClientBuilder[IO](ec).resource.use(client.sayHello(HelloRequest("SRE"))(_))
BlazeClientBuilder[IO](ec).resource.use(unaryClient.sayHello(HelloRequest("SRE"))(_))

the[UnexpectedStatus] thrownBy response.unsafeRunSync() shouldBe UnexpectedStatus(
Status.BadRequest)
}

"handle a raised non-gRPC exception in a unary POST request" in {
val response: IO[HelloResponse] =
BlazeClientBuilder[IO](ec).resource.use(unaryClient.sayHello(HelloRequest("RTE"))(_))

val client: UnaryGreeter.HttpClient[IO] =
UnaryGreeter.httpClient[IO](serviceUri / UnaryServicePrefix)
the[UnexpectedStatus] thrownBy response.unsafeRunSync() shouldBe UnexpectedStatus(
Status.InternalServerError)
}

"handle a thrown exception in a unary POST request" in {
val response: IO[HelloResponse] =
BlazeClientBuilder[IO](ec).resource.use(client.sayHello(HelloRequest("RTE"))(_))
BlazeClientBuilder[IO](ec).resource.use(unaryClient.sayHello(HelloRequest("TR"))(_))

the[ResponseError] thrownBy response.unsafeRunSync() shouldBe ResponseError(
Status.InternalServerError,
Some("RTE"))
the[UnexpectedStatus] thrownBy response.unsafeRunSync() shouldBe UnexpectedStatus(
Status.InternalServerError)
}

// "serve a POST request with fs2 streaming request" in {
//
// val fs2Client = Fs2Greeter.httpClient[IO](serviceUri / UnaryServicePrefix)
//
// val requests = Stream(HelloRequest("hey"), HelloRequest("there"))
//// val response =
//// BlazeClientBuilder[IO](ec).resource.use(fs2ServiceClient.sayHellos(requests)(_))
//// response.unsafeRunSync() shouldBe HelloResponse("hey, there")
//
// val response: IO[HelloResponse] =
// BlazeClientBuilder[IO](ec).resource.use(fs2Client.sayHellos(requests)(_))
// response.unsafeRunSync() shouldBe HelloResponse("hey, there")
// }
//
// "serve a POST request with empty fs2 streaming request" in {
// val requests = Stream.empty
// val response =
// BlazeClientBuilder[IO](ec).resource.use(fs2ServiceClient.sayHellos(requests)(_))
// response.unsafeRunSync() shouldBe HelloResponse("")
// }
//
// "serve a POST request with Observable streaming request" in {
// val requests = Observable(HelloRequest("hey"), HelloRequest("there"))
// val response =
// BlazeClientBuilder[IO](ec).resource.use(monixServiceClient.sayHellos(requests)(_))
// response.unsafeRunSync() shouldBe HelloResponse("hey, there")
// }
//
// "serve a POST request with empty Observable streaming request" in {
// val requests = Observable.empty
// val response =
// BlazeClientBuilder[IO](ec).resource.use(monixServiceClient.sayHellos(requests)(_))
// response.unsafeRunSync() shouldBe HelloResponse("")
// }
//
// "serve a POST request with fs2 streaming response" in {
// val request = HelloRequest("hey")
// val responses =
// BlazeClientBuilder[IO](ec).stream.flatMap(fs2ServiceClient.sayHelloAll(request)(_))
// responses.compile.toList
// .unsafeRunSync() shouldBe List(HelloResponse("hey"), HelloResponse("hey"))
// }
//
// "serve a POST request with Observable streaming response" in {
// val request = HelloRequest("hey")
// val responses = BlazeClientBuilder[IO](ec).stream
// .flatMap(monixServiceClient.sayHelloAll(request)(_).toFs2Stream[IO])
// responses.compile.toList
// .unsafeRunTimed(10.seconds)
// .getOrElse(sys.error("Stuck!")) shouldBe List(HelloResponse("hey"), HelloResponse("hey"))
// }
//
// "handle errors with fs2 streaming response" in {
// val request = HelloRequest("")
// val responses =
// BlazeClientBuilder[IO](ec).stream.flatMap(fs2ServiceClient.sayHelloAll(request)(_))
// the[IllegalArgumentException] thrownBy responses.compile.toList
// .unsafeRunSync() should have message "empty greeting"
// }
//
// "handle errors with Observable streaming response" in {
// val request = HelloRequest("")
// val responses = BlazeClientBuilder[IO](ec).stream
// .flatMap(monixServiceClient.sayHelloAll(request)(_).toFs2Stream[IO])
// the[IllegalArgumentException] thrownBy responses.compile.toList
// .unsafeRunTimed(10.seconds)
// .getOrElse(sys.error("Stuck!")) should have message "empty greeting"
// }
//
// "serve a POST request with bidirectional fs2 streaming" in {
// val requests = Stream(HelloRequest("hey"), HelloRequest("there"))
// val responses =
// BlazeClientBuilder[IO](ec).stream.flatMap(fs2ServiceClient.sayHellosAll(requests)(_))
// responses.compile.toList
// .unsafeRunSync() shouldBe List(HelloResponse("hey"), HelloResponse("there"))
// }
//
// "serve an empty POST request with bidirectional fs2 streaming" in {
// val requests = Stream.empty
// val responses =
// BlazeClientBuilder[IO](ec).stream.flatMap(fs2ServiceClient.sayHellosAll(requests)(_))
// responses.compile.toList.unsafeRunSync() shouldBe Nil
// }
//
// "serve a POST request with bidirectional Observable streaming" in {
// val requests = Observable(HelloRequest("hey"), HelloRequest("there"))
// val responses = BlazeClientBuilder[IO](ec).stream
// .flatMap(monixServiceClient.sayHellosAll(requests)(_).toFs2Stream[IO])
// responses.compile.toList
// .unsafeRunTimed(10.seconds)
// .getOrElse(sys.error("Stuck!")) shouldBe List(HelloResponse("hey"), HelloResponse("there"))
// }
//
// "serve an empty POST request with bidirectional Observable streaming" in {
// val requests = Observable.empty
// val responses = BlazeClientBuilder[IO](ec).stream
// .flatMap(monixServiceClient.sayHellosAll(requests)(_).toFs2Stream[IO])
// responses.compile.toList
// .unsafeRunTimed(10.seconds)
// .getOrElse(sys.error("Stuck!")) shouldBe Nil
// }
//
// "serve ScalaCheck-generated POST requests with bidirectional Observable streaming" in {
// forAll { strings: List[String] =>
// val requests = Observable.fromIterable(strings.map(HelloRequest))
// val responses = BlazeClientBuilder[IO](ec).stream
// .flatMap(monixServiceClient.sayHellosAll(requests)(_).toFs2Stream[IO])
// responses.compile.toList
// .unsafeRunTimed(10.seconds)
// .getOrElse(sys.error("Stuck!")) shouldBe strings.map(HelloResponse)
// }
// }

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ import higherkindness.mu.rpc.protocol._

@service(Avro) trait UnaryGreeter[F[_]] {

@http(GET, "getHello") def getHello(request: Empty.type): F[HelloResponse]
@http def getHello(request: Empty.type): F[HelloResponse]

@http(POST, "sayHello") def sayHello(request: HelloRequest): F[HelloResponse]
@http def sayHello(request: HelloRequest): F[HelloResponse]
}

import fs2.Stream
@service(Avro) trait Fs2Greeter[F[_]] {

def sayHellos(requests: Stream[F, HelloRequest]): F[HelloResponse]
@http def sayHellos(requests: Stream[F, HelloRequest]): F[HelloResponse]

def sayHelloAll(request: HelloRequest): Stream[F, HelloResponse]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package higherkindness.mu.rpc
package internal

import higherkindness.mu.rpc.protocol._

import scala.reflect.api.Trees
import scala.reflect.macros.blackbox

// $COVERAGE-OFF$
Expand Down Expand Up @@ -430,8 +432,15 @@ object serviceImpl {
_ = require(params.length == 1, s"RPC call ${d.name} has more than one request parameter")
p <- params.headOption.toList
} yield {
val method = TermName(args(0).toString) // TODO: fix direct index access
val uri = args(1).toString // TODO: fix direct index access

val method: c.universe.TermName = p.tpt match {

This comment has been minimized.

Copy link
@L-Lavigne

L-Lavigne Feb 26, 2019

Contributor

👏

case tq"Empty.type" => TermName("GET")
case _ => TermName("POST")
}
val uri = d.name.toString

// val method: c.universe.TermName = TermName(args(0).toString) // TODO: fix direct index access
// val uri = args(1).toString // TODO: fix direct index access

val responseType: Tree = d.tpt match {
case tq"Observable[..$tpts]" => tpts.head
Expand Down

0 comments on commit 15abe94

Please sign in to comment.