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

Server-side power API #179 #429

Merged
merged 45 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
6d90c85
Draft implementation
ctoomey Oct 2, 2018
b983c53
Fix missing content-type header in Play response
ctoomey Oct 8, 2018
ff4f0d1
Fixed missing Content-Type header
ctoomey Oct 8, 2018
4a9515c
Ripped out Play changes
ctoomey Oct 13, 2018
2d8872f
Cleanup
ctoomey Oct 13, 2018
42a9431
Remove PowerAPI from generated names
ctoomey Oct 13, 2018
502072b
Remove PowerAPI from generated names
ctoomey Oct 13, 2018
fe75c41
Merge power API handling into handler template
ctoomey Oct 15, 2018
73e9397
Merge power API handling into handler template
ctoomey Oct 15, 2018
fb86f38
Merge remote-tracking branch 'upstream/master' into 179-server-power-…
ctoomey Oct 27, 2018
747744e
Integrate extended code generator options into akkaGrpcCodeGeneratorS…
ctoomey Oct 28, 2018
54c863c
Integrate extended code generator options into akkaGrpcCodeGeneratorS…
ctoomey Oct 28, 2018
f7ac227
Fix enum
ctoomey Oct 28, 2018
8a346af
Make GeneratorOption accessible
ctoomey Oct 28, 2018
d06be06
Latest changes requested on PR
ctoomey Nov 12, 2018
2522ccc
Latest changes requested on PR
ctoomey Nov 12, 2018
3a1a1b4
Latest PR-requested updates
ctoomey Nov 12, 2018
cf28c1f
Fix handler name in comments
ctoomey Nov 25, 2018
bd37a4d
Changed handling of server power APIs generator option
ctoomey Nov 25, 2018
c37e267
Revert "Changed handling of server power APIs generator option"
ctoomey Nov 25, 2018
d8eae54
Revert unrelated package name fix
ctoomey Nov 25, 2018
bff5466
Fix handling of both client/server generators
ctoomey Dec 3, 2018
84b0335
Updated ReflectiveCodeGen
ctoomey Dec 10, 2018
f92a635
Update Java code generators
ctoomey Dec 17, 2018
58ca34d
Java power API
ctoomey Jan 11, 2019
f39c234
Backed out unfinished Java power API Metadata.asMap
ctoomey Jan 11, 2019
7fe4b78
Java power API
ctoomey Jan 14, 2019
edc28d2
Merge branch 'master' into 179-server-power-apis2.update-from-master
ctoomey Jan 16, 2019
7544cae
Java power API
ctoomey Jan 22, 2019
cebba99
Maven for power APIs
ctoomey Jan 22, 2019
190964f
Fix missing headers warning
ctoomey Jan 22, 2019
fc1d9be
Fix missing headers warning
ctoomey Jan 22, 2019
0af4c9b
Gradle
ctoomey Jan 22, 2019
6a7b9a2
Build tool fixes
ctoomey Jan 23, 2019
b6684eb
Merge branch 'master' into 179-server-power-apis2
ctoomey Jan 24, 2019
ac44100
Power API test for scala
ctoomey Jan 25, 2019
18fd974
Power API test for java
ctoomey Jan 25, 2019
6444b55
Power API test for java
ctoomey Jan 25, 2019
707a5ae
Power API test fixes for maven, gradle
ctoomey Jan 25, 2019
e7481d1
Power API documentation
ctoomey Feb 5, 2019
e2d5b68
Merge branch 'master' into 179-server-power-apis2
ctoomey Feb 5, 2019
e87129e
Merge branch 'master' into 179-server-power-apis2
ctoomey Feb 6, 2019
686aa5d
Power API tweaks
ctoomey Feb 6, 2019
cbfd17b
Updated Play router templates
ctoomey Feb 12, 2019
c8dba56
sbt Play generators moved back to akkaGrpcExtraGenerators
ctoomey Feb 12, 2019
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
23 changes: 12 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ scalaVersion := scala212

val commonSettings = Seq(
organization := "com.lightbend.akka.grpc",
version := "0.4.1.livongo",

scalacOptions ++= List(
"-unchecked",
Expand Down Expand Up @@ -307,19 +308,19 @@ lazy val root = Project(
.aggregate(
runtime,
codegen,
mavenPlugin,
// mavenPlugin,
sbtPlugin,
scalapbProtocPlugin,
interopTests,
playInteropTestJava,
playInteropTestScala,
playTestkit,
playSpecs2,
playScalaTest,
playTestdata,
pluginTesterScala,
pluginTesterJava,
docs,
// interopTests,
// playInteropTestJava,
// playInteropTestScala,
// playTestkit,
// playSpecs2,
// playScalaTest,
// playTestdata,
// pluginTesterScala,
// pluginTesterJava,
// docs,
)
.enablePlugins(akka.grpc.NoPublish)
.settings(
Expand Down
122 changes: 61 additions & 61 deletions codegen/src/main/scala/akka/grpc/gen/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,65 +48,65 @@ object Main extends App {
case _ => SilencedLogger
}

val out = {
val codeGenerator =
if (!generatePlay) {
if (languageScala) {
// Scala
if (generateClient && generateServer) ScalaBothCodeGenerator
else if (generateClient) ScalaClientCodeGenerator
else if (generateServer) ScalaServerCodeGenerator
else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
} else {
// Java
if (generateClient && generateServer) JavaBothCodeGenerator
else if (generateClient) JavaClientCodeGenerator
else if (generateServer) JavaServerCodeGenerator
else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
}
} else {
if (languageScala) {
// Scala
if (generateClient && generateServer) CombinedPlayScalaBothCodeGenerator
else if (generateClient) CombinedPlayScalaClientCodeGenerator
else if (generateServer) CombinedPlayScalaServerCodeGenerator
else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
} else {
// Java
if (generateClient && generateServer) CombinedPlayJavaBothCodeGenerator
else if (generateClient) CombinedPlayJavaClientCodeGenerator
else if (generateServer) CombinedPlayJavaServerCodeGenerator
else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
}
}

codeGenerator.run(req, logger)
}

System.out.write(out.toByteArray)
System.out.flush()

/**
* Generators to generate both the 'plain' akka-grpc code and the play-specific code that depends on it.
* In other build tools (Maven and sbt) these are passed to protoc separately, but since Gradle does the
* akka-grpc code generation in a single protoc invocation we need to combine those here:
*/
object CombinedPlayJavaClientCodeGenerator extends PlayJavaClientCodeGenerator with JavaClientCodeGenerator {
override def name = "combined-play-java-client"
}
object CombinedPlayJavaServerCodeGenerator extends PlayJavaServerCodeGenerator with JavaServerCodeGenerator {
override def name = "combined-play-java-server"
}
object CombinedPlayJavaBothCodeGenerator extends PlayJavaClientCodeGenerator with JavaClientCodeGenerator with PlayJavaServerCodeGenerator with JavaServerCodeGenerator {
override def name = "combined-play-java-both"
}
object CombinedPlayScalaClientCodeGenerator extends PlayScalaClientCodeGenerator with ScalaClientCodeGenerator {
override def name = "combined-play-scala-client"
}
object CombinedPlayScalaServerCodeGenerator extends PlayScalaServerCodeGenerator with ScalaServerCodeGenerator {
override def name = "combined-play-scala-server"
}
object CombinedPlayScalaBothCodeGenerator extends PlayScalaClientCodeGenerator with ScalaClientCodeGenerator with PlayScalaServerCodeGenerator with ScalaServerCodeGenerator {
override def name = "combined-play-scala-both"
}
// val out = {
// val codeGenerator =
// if (!generatePlay) {
// if (languageScala) {
// // Scala
// if (generateClient && generateServer) ScalaBothCodeGenerator
// else if (generateClient) ScalaClientCodeGenerator
// else if (generateServer) ScalaServerCodeGenerator()
// else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
// } else {
// // Java
// if (generateClient && generateServer) JavaBothCodeGenerator
// else if (generateClient) JavaClientCodeGenerator
// else if (generateServer) JavaServerCodeGenerator
// else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
// }
// } else {
// if (languageScala) {
// // Scala
// if (generateClient && generateServer) CombinedPlayScalaBothCodeGenerator
// else if (generateClient) CombinedPlayScalaClientCodeGenerator
// else if (generateServer) CombinedPlayScalaServerCodeGenerator
// else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
// } else {
// // Java
// if (generateClient && generateServer) CombinedPlayJavaBothCodeGenerator
// else if (generateClient) CombinedPlayJavaClientCodeGenerator
// else if (generateServer) CombinedPlayJavaServerCodeGenerator
// else throw new IllegalArgumentException("At least one of generateClient or generateServer must be enabled")
// }
// }
//
// codeGenerator.run(req, logger)
// }
//
// System.out.write(out.toByteArray)
// System.out.flush()
//
// /**
// * Generators to generate both the 'plain' akka-grpc code and the play-specific code that depends on it.
// * In other build tools (Maven and sbt) these are passed to protoc separately, but since Gradle does the
// * akka-grpc code generation in a single protoc invocation we need to combine those here:
// */
// object CombinedPlayJavaClientCodeGenerator extends PlayJavaClientCodeGenerator with JavaClientCodeGenerator {
// override def name = "combined-play-java-client"
// }
// object CombinedPlayJavaServerCodeGenerator extends PlayJavaServerCodeGenerator with JavaServerCodeGenerator {
// override def name = "combined-play-java-server"
// }
// object CombinedPlayJavaBothCodeGenerator extends PlayJavaClientCodeGenerator with JavaClientCodeGenerator with PlayJavaServerCodeGenerator with JavaServerCodeGenerator {
// override def name = "combined-play-java-both"
// }
// object CombinedPlayScalaClientCodeGenerator extends PlayScalaClientCodeGenerator with ScalaClientCodeGenerator {
// override def name = "combined-play-scala-client"
// }
// object CombinedPlayScalaServerCodeGenerator extends PlayScalaServerCodeGenerator with ScalaServerCodeGenerator {
// override def name = "combined-play-scala-server"
// }
// object CombinedPlayScalaBothCodeGenerator extends PlayScalaClientCodeGenerator with ScalaClientCodeGenerator with PlayScalaServerCodeGenerator with ScalaServerCodeGenerator {
// override def name = "combined-play-scala-both"
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

package akka.grpc.gen.scaladsl

object ScalaBothCodeGenerator extends ScalaServerCodeGenerator with ScalaClientCodeGenerator {
object ScalaBothCodeGenerator extends ScalaServerCodeGenerator(powerApis = false) with ScalaClientCodeGenerator {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this parameter anymore, since this information now comes in via the parameters field on CodeGeneratorRequest, so we could add the powerApis field to https://github.com/akka/akka-grpc/blob/master/codegen/src/main/scala/akka/grpc/gen/scaladsl/Service.scala#L13. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually implemented this change and then was reminded when I tried to use the updated code on a project that we can't pass through to scalaPB our additional code generator params without also modifying it to handle them -- otherwise it throws an "unrecognized parameter" exception. That's why I didn't do it that way originally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to first filter them out before passing them to scalaPB, that is true.

override def name = "akka-grpc-scaladsl-both"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@ import akka.grpc.gen.Logger
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse
import templates.ScalaServer.txt._

trait ScalaServerCodeGenerator extends ScalaCodeGenerator {
case class ScalaServerCodeGenerator(powerApis: Boolean = false) extends ScalaCodeGenerator {
import ScalaServerCodeGenerator._
override def name = "akka-grpc-scaladsl-server"

override def perServiceContent = super.perServiceContent + ScalaCodeGenerator.generateServiceFile + generateHandler
override def perServiceContent =
super.perServiceContent + ScalaCodeGenerator.generateServiceFile ++ {
if (powerApis) Set(generatePowerService) else Set.empty
} + generateHandler(powerApis)
}

def generateHandler(logger: Logger, service: Service): CodeGeneratorResponse.File = {
object ScalaServerCodeGenerator {
val generatePowerService: (Logger, Service) => CodeGeneratorResponse.File = (logger, service) => {
val b = CodeGeneratorResponse.File.newBuilder()
b.setContent(Handler(service).body)
b.setName(s"${service.packageDir}/${service.name}Handler.scala")
b.setContent(PowerApiTrait(service).body)
b.setName(s"${service.packageDir}/${service.name}PowerApi.scala")
raboof marked this conversation as resolved.
Show resolved Hide resolved
logger.info(s"Generating Akka gRPC file ${b.getName}")
// logger.info(s"Generating Akka gRPC extended service interface ${service.packageName}.${service.name}Extended")
b.build
}

def generateHandler(powerApis: Boolean): (Logger, Service) => CodeGeneratorResponse.File = (logger, service) => {
val b = CodeGeneratorResponse.File.newBuilder()
b.setContent(Handler(service, powerApis).body)
b.setName(s"${service.packageDir}/${service.name}Handler.scala")
b.build
}
}

object ScalaServerCodeGenerator extends ScalaServerCodeGenerator
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@
package akka.grpc.gen.scaladsl.play

import akka.grpc.gen.Logger
import akka.grpc.gen.scaladsl.{ ScalaCodeGenerator, Service }
import akka.grpc.gen.scaladsl.{ ScalaCodeGenerator, ScalaServerCodeGenerator, Service }
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse
import templates.PlayScala.txt.Router
import templates.PlayScala.txt._

object PlayScalaServerCodeGenerator extends PlayScalaServerCodeGenerator
case class PlayScalaServerCodeGenerator(powerApis: Boolean = false) extends ScalaCodeGenerator {

import ScalaServerCodeGenerator._

trait PlayScalaServerCodeGenerator extends ScalaCodeGenerator {
override def name: String = "akka-grpc-play-server-scala"

override def perServiceContent = super.perServiceContent + generateRouter
override def perServiceContent = super.perServiceContent ++ Set(ScalaCodeGenerator.generateServiceFile) ++ {
if (powerApis) Set(generatePowerService) else Set.empty
} ++ Set(generateHandler(powerApis), generateRouter)

private val generateRouter: (Logger, Service) => CodeGeneratorResponse.File = (logger, service) => {
val b = CodeGeneratorResponse.File.newBuilder()
b.setContent(Router(service).body)
b.setContent(Router(service, powerApis).body)
b.setName(s"${service.packageDir}/Abstract${service.name}Router.scala")
logger.info(s"Generating Akka gRPC file ${b.getName}")
b.build
}
}
5 changes: 2 additions & 3 deletions codegen/src/main/twirl/templates/PlayScala/Router.scala.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com>
*@

@(service: akka.grpc.gen.scaladsl.Service)
@(service: akka.grpc.gen.scaladsl.Service, powerApis: Boolean)

@akka.grpc.gen.Constants.DoNotEditComment
package @service.packageName
Expand All @@ -17,9 +17,8 @@ import akka.grpc.internal.PlayRouter
/**
* Abstract base class for implementing @{service.name} and using as a play Router
*/
abstract class Abstract@{service.name}Router(mat: Materializer) extends PlayRouter(mat, @{service.name}.name) with @{service.name} {
abstract class Abstract@{service.name}Router(mat: Materializer) extends PlayRouter(mat, @{service.name}.name) with @{service.name}@{if(powerApis) { "PowerApi"} else { "" }} {

final override def createHandler(serviceName: String, mat: Materializer): HttpRequest => Future[HttpResponse] =
@{service.name}Handler(this, serviceName)(mat)

}
Loading