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

Use flat_package for scala codegen by default #106

Merged
merged 3 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//#full-client
package io.grpc.examples.helloworld
package io.grpc.examples

import java.util.concurrent.TimeUnit

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//#full-server
package io.grpc.examples.helloworld
package io.grpc.examples

import java.io.FileInputStream
import java.nio.file.Files
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//#full-service-impl
package io.grpc.examples.helloworld
package io.grpc.examples

import scala.concurrent.Future

Expand Down
18 changes: 10 additions & 8 deletions project/ReflectiveCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ import ProtocPlugin.autoImport.PB
import protocbridge.{Artifact, Generator, JvmGenerator, Target}
import sbt.internal.inc.classpath.ClasspathUtilities

import scalapb.ScalaPbCodeGenerator

/** A plugin that allows to use a code generator compiled in one subproject to be used in a test project */
object ReflectiveCodeGen extends AutoPlugin {
// In the 'real' plugin we enable the 'flat_package' option by default to get package names
// that are more consistent between Scala and Java.
// Because the interop tests generate both Scala and Java code, however, here we disable this
// option to avoid name clashes in the generated classes:
val codeGeneratorSettings = Seq.empty

override def projectSettings: Seq[Def.Setting[_]] =
inConfig(Compile)(Seq(
PB.generate :=
Expand Down Expand Up @@ -36,7 +44,7 @@ object ReflectiveCodeGen extends AutoPlugin {
mutableGenerator in Compile := createMutableGenerator(),
PB.targets in Compile := Seq(
// Scala model classes:
scalapb.gen(grpc = false, flatPackage = false) -> (sourceManaged in Compile).value,
(JvmGenerator("scala", ScalaPbCodeGenerator), codeGeneratorSettings) -> (sourceManaged in Compile).value,
// Java model classes:
PB.gens.java -> (sourceManaged in Compile).value,
// akka-grpc code:
Expand Down Expand Up @@ -72,13 +80,7 @@ object ReflectiveCodeGen extends AutoPlugin {
}

val adapted = new MutableProtocCodeGenerator
val generator = JvmGenerator(s"mutable", adapted)
val settings: Seq[String] = Seq(
"flat_package" -> false,
"java_conversions" -> false,
"single_line_to_string" -> false).collect { case (settingName, v) if v => settingName }

MutableGeneratorAccess(adapted.setUnderlying _, (generator, settings))
MutableGeneratorAccess(adapted.setUnderlying _, (JvmGenerator(s"mutable", adapted), codeGeneratorSettings))
Copy link
Member

Choose a reason for hiding this comment

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

Not used interpolation.

}

def loadAndSetGenerator(classpath: Classpath, access: MutableGeneratorAccess): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object AkkaGrpcPlugin extends AutoPlugin {

def configSettings(config: Configuration): Seq[Setting[_]] = {
inConfig(config)(Seq(
codeGeneratorSettings := Seq.empty,
codeGeneratorSettings := Seq("flat_package"),
akkaGrpcCodeGenerators := GeneratorAndSettings(ScalaServerCodeGenerator, codeGeneratorSettings.value) :: Nil,
akkaGrpcModelGenerators := Seq[Target]((JvmGenerator("scala", ScalaPbCodeGenerator), codeGeneratorSettings.value) -> sourceManaged.value),

Expand Down
6 changes: 6 additions & 0 deletions sbt-plugin/src/sbt-test/gen-scala-server/00-interop/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ javaAgents ++= Seq(
enablePlugins(JavaAgent)
enablePlugins(AkkaGrpcPlugin)

// By default we enable the 'flat_package' option by default to get package names
// that are more consistent between Scala and Java.
// Because the interop tests generate both Scala and Java code, however, here we disable this
// option to avoid name clashes in the generated classes:
(codeGeneratorSettings in Compile) := Seq.empty
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use (codeGeneratorSettings in Compile).value.filterNot(_ == "flat_package") in case we have other settings by default?

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 not super nice to have this stringly-typed but most flexible and not really worth adding infrastructure for I suppose.


(akkaGrpcCodeGenerators in Compile) += GeneratorAndSettings(JavaServerCodeGenerator)
(akkaGrpcModelGenerators in Compile) += PB.gens.java -> sourceManaged.value
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.akka.grpc.helloworld
package io.akka.grpc

import scala.concurrent.Future

import akka.NotUsed
import akka.stream.scaladsl.Source

import io.akka.grpc.helloworld._

class GreeterImpl extends Greeter {
override def sayHello(in: HelloRequest): Future[HelloReply] = ???

Expand All @@ -15,4 +13,5 @@ class GreeterImpl extends Greeter {
override def itKeepsTalking(in: Source[HelloRequest, NotUsed]): Future[HelloReply] = ???

override def itKeepsReplying(in: HelloRequest): Source[HelloReply, NotUsed] = ???

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
> check

$ exists target/scala-2.12/src_managed
$ exists target/scala-2.12/src_managed/main/io/akka/grpc/helloworld/HelloRequest.scala
$ exists target/scala-2.12/src_managed/main/io/akka/grpc/helloworld/HelloworldProto.scala
$ exists target/scala-2.12/src_managed/main/io/akka/grpc/helloworld/Greeter.scala
$ exists target/scala-2.12/src_managed/main/io/akka/grpc/HelloRequest.scala
$ exists target/scala-2.12/src_managed/main/io/akka/grpc/HelloworldProto.scala
$ exists target/scala-2.12/src_managed/main/io/akka/grpc/Greeter.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.akka.grpc.echo
package io.akka.grpc

import scala.concurrent.Future

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.akka.grpc.helloworld
package io.akka.grpc

import scala.concurrent.Future

import akka.NotUsed
import akka.stream.scaladsl.Source

import io.akka.grpc.helloworld._

class GreeterImpl extends Greeter {
override def sayHello(in: HelloRequest): Future[HelloReply] = ???

Expand All @@ -15,5 +13,4 @@ class GreeterImpl extends Greeter {
override def itKeepsTalking(in: Source[HelloRequest, NotUsed]): Future[HelloReply] = ???

override def itKeepsReplying(in: HelloRequest): Source[HelloReply, NotUsed] = ???

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import javax.net.ssl.{KeyManagerFactory, SSLContext}
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.http.scaladsl.{Http2, HttpsConnectionContext}
import io.akka.grpc.echo.{EchoImpl, EchoHandler}
import io.akka.grpc.helloworld.{GreeterImpl, GreeterHandler}

object Main extends App {
implicit val system = ActorSystem()
Expand Down