Skip to content

airframe-grpc: Fix syntax for Scala 3 #2193

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

Merged
merged 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airframe-di/src/main/scala-3/wvlet/airframe/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ package object airframe {
def newSilentDesign: Design = newDesign.noLifeCycleLogging

import scala.jdk.CollectionConverters._

// This will not be used in Scala 3, but left for the compatibility with Scala 2
val traitFactoryCache = new ConcurrentHashMap[Surface, Session => Any].asScala
def getOrElseUpdateTraitFactoryCache(s: Surface, factory: Session => Any): Session => Any = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ case class GrpcServerConfig(
executorProvider: GrpcServerConfig => ExecutorService = GrpcServer.newAsyncExecutorFactory,
maxThreads: Int = (Runtime.getRuntime.availableProcessors() * 2).max(2),
codecFactory: MessageCodecFactory = MessageCodecFactory.defaultFactoryForMapOutput,
requestLoggerProvider: GrpcServerConfig => GrpcRequestLogger = { config: GrpcServerConfig =>
requestLoggerProvider: GrpcServerConfig => GrpcRequestLogger = { (config: GrpcServerConfig) =>
GrpcRequestLogger
.newLogger(config.name)
}
Expand Down Expand Up @@ -85,7 +85,9 @@ case class GrpcServerConfig(
def withRequestLoggerProvider(provider: GrpcServerConfig => GrpcRequestLogger) = this
.copy(requestLoggerProvider = provider)
// Disable RPC logging
def noRequestLogging = this.copy(requestLoggerProvider = { config: GrpcServerConfig => GrpcRequestLogger.nullLogger })
def noRequestLogging = this.copy(requestLoggerProvider = { (config: GrpcServerConfig) =>
GrpcRequestLogger.nullLogger
})

/**
* Create and start a new server based on this config.
Expand Down Expand Up @@ -123,7 +125,7 @@ case class GrpcServerConfig(
*/
def designWithChannel: Design = {
design
.bind[Channel].toProvider { server: GrpcServer =>
.bind[Channel].toProvider { (server: GrpcServer) =>
ManagedChannelBuilder.forTarget(server.localAddress).usePlaintext().build()
}
.onShutdown {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import wvlet.airframe.rx.Rx
import wvlet.airspec.AirSpec
import wvlet.log.{LogSupport, Logger}

import scala.concurrent.Promise
import scala.concurrent.{ExecutionContext, Promise}

class GrpcClientTest extends AirSpec {

// TODO Use AirSpec's defaultExecutionContext
private implicit val sc = scala.concurrent.ExecutionContext.global
private implicit val sc: ExecutionContext = defaultExecutionContext

override def design: Design = DemoApiV2.design

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object GrpcContextTest extends AirSpec {

override protected def design: Design = DemoApi.design

test("get context") { client: DemoApiClient =>
test("get context") { (client: DemoApiClient) =>
val ret = client.getContext
info(ret)
client.getContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object GrpcErrorLogTest extends AirSpec {
gRPC.server
.withName("demo-api-debug")
.withRouter(Router.add[DemoApiDebug])
.withRequestLoggerProvider { config: GrpcServerConfig =>
.withRequestLoggerProvider { (config: GrpcServerConfig) =>
GrpcRequestLogger
.newLogger(config.name, inMemoryLogWriter)
}
Expand All @@ -64,7 +64,7 @@ object GrpcErrorLogTest extends AirSpec {
inMemoryLogWriter.getLogs
}

test("request logger test") { client: DemoApiClient =>
test("request logger test") { (client: DemoApiClient) =>
test("unary method error log") {
val logs = captureAll {
client.hello("gRPC")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GrpcJsonTest extends AirSpec {

protected override def design = gRPC.server.withRouter(router).designWithChannel

test("json encoding") { c: DemoApiClient =>
test("json encoding") { (c: DemoApiClient) =>
val client = c.withEncoding(RPCEncoding.JSON)

test("unary") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GrpcRequestLoggerTest extends AirSpec {
gRPC.server
.withName("demo-api")
.withRouter(DemoApi.router)
.withRequestLoggerProvider { config: GrpcServerConfig =>
.withRequestLoggerProvider { (config: GrpcServerConfig) =>
GrpcRequestLogger.newLogger(config.name, inMemoryLogWriter)
}
.designWithChannel
Expand All @@ -45,7 +45,7 @@ class GrpcRequestLoggerTest extends AirSpec {
inMemoryLogWriter.getLogs
}

test("request logger test") { client: DemoApiClient =>
test("request logger test") { (client: DemoApiClient) =>
test("unary method log") {
val logs = captureAll {
client.hello("gRPC")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object GrpcServerFactoryTest extends AirSpec {

private val r = Router.add[Greeter]

test("Build multiple gRPC servers") { f: GrpcServerFactory =>
test("Build multiple gRPC servers") { (f: GrpcServerFactory) =>
val s1 = f.newServer(gRPC.server.withName("grpc1").withRouter(r))
val s2 = f.newServer(gRPC.server.withName("grpc2").withRouter(r))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import wvlet.airspec.AirSpec
*/
object GrpcStreamingTest extends AirSpec {

private def router = Router.add[DemoApi]
debug(router)

protected override def design = gRPC.server.withRouter(router).designWithChannel
private lazy val router: Router = Router.add[DemoApi]
protected override def design = {
gRPC.server.withRouter(router).designWithChannel
}

// launching a gRPC server first
test("Launch a standalone gRPC server") { server: GrpcServer =>
test("Test gRPC client methods") { stub: DemoApiClient =>
test("Launch a standalone gRPC server") { (server: GrpcServer) =>
test("Test gRPC client methods") { (stub: DemoApiClient) =>
val N = 10

test("unary") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object GrpcTest extends AirSpec {
info(s"Shutting down gRPC server localhost:${port}")
server.shutdownNow()
}
.bind[ManagedChannel].toProvider { server: Server =>
.bind[ManagedChannel].toProvider { (server: Server) =>
ManagedChannelBuilder.forTarget(s"localhost:${server.getPort}").usePlaintext().build()
}
.onShutdown { channel =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ object DemoApi {
.withRouter(router)
.withName("DemoApi")
.designWithChannel
.bind[DemoApiClient].toProvider { channel: Channel => new DemoApiClient(channel) }
.bind[DemoApiClient].toProvider { (channel: Channel) => new DemoApiClient(channel) }

val router = Router.add[DemoApi]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package wvlet.airframe.http.router
import wvlet.airframe.http.Router
import wvlet.airframe.http.HttpFilterType
import wvlet.airframe.surface.Surface
import wvlet.airframe.Session

trait RouterBase { self: Router =>
inline def add[Controller]: Router = {
// TODO registerTraitFactory
self.addInternal(Surface.of[Controller], Surface.methodsOf[Controller])
}

Expand All @@ -41,9 +43,22 @@ private[router] object RouterObjectMacros {
import quotes.reflect._

if (TypeRepr.of[Controller] <:< TypeRepr.of[HttpFilterType]) {
// TODO registerTraitFactory
'{ Router(filterSurface = Some(Surface.of[Controller])) }
} else {
// TODO registerTraitFactory
'{ Router.empty.add[Controller] }
}
}

// def registerTraitFactory[T: Type](using quotes: Quotes): Expr[Unit] = {
// // TODO implement simlar thing method like AirframeMacros.shouldGenerateTrait
// // TODO instantiate an arbitrary trait https://github.com/lampepfl/dotty/issues/11685
// val t = Type.of[T].info
// '{ wvlet.airframe.getOrElseUpdateTraitFactoryCache(
// Surface.of[T],
// { (ss: Session) => (new ${t} {}).asInstanceOf[Any] }
// )
// }
// }
}