Skip to content

Commit

Permalink
Fixes. #917. Added include_std_type configuration option. (#1136)
Browse files Browse the repository at this point in the history
* Fixes. #917. Added include_std_type configuration option.

+ Maven
+ Gradle
+ Sbt

* Restored overwritten files.

* Plusing modifications:

cleanup + version alignment

* sbt akka-grpc-plugin-tester-scala/test:scalafmt

* Correctly pass in option in scripted test

* Restore '#option' marker to make 'sbt paradox' work

* Fixed maven plugin configuration.

* Code review change

Co-authored-by: Arnout Engelen <github@bzzt.net>

Co-authored-by: Arnout Engelen <arnout@bzzt.net>
Co-authored-by: Arnout Engelen <github@bzzt.net>
  • Loading branch information
3 people committed Oct 8, 2020
1 parent f266e5e commit 7b4d0cd
Show file tree
Hide file tree
Showing 22 changed files with 85 additions and 28 deletions.
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ lazy val pluginTesterScala = Project(id = "akka-grpc-plugin-tester-scala", base
skip in publish := true,
crossScalaVersions := Dependencies.Versions.CrossScalaForLib,
scalaVersion := scala212,
ReflectiveCodeGen.codeGeneratorSettings ++= Seq("flat_package", "server_power_apis"))
ReflectiveCodeGen.codeGeneratorSettings ++= Seq("flat_package", "server_power_apis"),
Compile / ReflectiveCodeGen.protocOptions += "--include_std_types")
.pluginTestingSettings

lazy val pluginTesterJava = Project(id = "akka-grpc-plugin-tester-java", base = file("plugin-tester-java"))
Expand All @@ -223,7 +224,8 @@ lazy val pluginTesterJava = Project(id = "akka-grpc-plugin-tester-java", base =
ReflectiveCodeGen.generatedLanguages := Seq("Java"),
crossScalaVersions := Dependencies.Versions.CrossScalaForLib,
scalaVersion := scala212,
ReflectiveCodeGen.codeGeneratorSettings ++= Seq("server_power_apis"))
ReflectiveCodeGen.codeGeneratorSettings ++= Seq("server_power_apis"),
Compile / ReflectiveCodeGen.protocOptions += "--include_std_types")
.pluginTestingSettings

lazy val root = Project(id = "akka-grpc", base = file("."))
Expand Down
2 changes: 1 addition & 1 deletion gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ repositories {
}

dependencies {
implementation 'com.google.protobuf:protobuf-gradle-plugin:0.8.12'
implementation 'com.google.protobuf:protobuf-gradle-plugin:0.8.13'
testImplementation('org.spockframework:spock-core:1.3-groovy-2.5')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ class AkkaGrpcPlugin implements Plugin<Project> {
}
plugins {
akkaGrpc {
artifact = "com.lightbend.akka.grpc:akka-grpc-codegen_$PROTOC_PLUGIN_SCALA_VERSION:${baselineVersion}:${assemblyClassifier}@${assemblySuffix}"
artifact = "com.lightbend.akka.grpc:akka-grpc-codegen_${PROTOC_PLUGIN_SCALA_VERSION}:${baselineVersion}:${assemblyClassifier}@${assemblySuffix}"
}
if (akkaGrpcExt.scala) {
scalapb {
artifact = "com.lightbend.akka.grpc:akka-grpc-scalapb-protoc-plugin_$PROTOC_PLUGIN_SCALA_VERSION:${baselineVersion}:${assemblyClassifier}@${assemblySuffix}"
artifact = "com.lightbend.akka.grpc:akka-grpc-scalapb-protoc-plugin_${PROTOC_PLUGIN_SCALA_VERSION}:${baselineVersion}:${assemblyClassifier}@${assemblySuffix}"
}
}
}
Expand All @@ -117,6 +117,9 @@ class AkkaGrpcPlugin implements Plugin<Project> {
option "use_play_actions=${akkaGrpcExt.usePlayActions}"
option "extra_generators=${akkaGrpcExt.extraGenerators.join(';')}"
option "logfile=${project.projectDir.toPath().relativize(logFile).toString()}"
if (akkaGrpcExt.includeStdTypes) {
option "include_std_types=true"
}
if (akkaGrpcExt.generatePlay) {
option "generate_play=true"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import org.gradle.api.Project

class AkkaGrpcPluginExtension {

static final String PLUGIN_CODE = 'com.lightbend.akka.grpc.gradle'

static final String PROTOC_VERSION = "3.4.0"
static final String PROTOC_VERSION = "3.11.0"

static final String PROTOC_PLUGIN_SCALA_VERSION = "2.12"

static final String GRPC_VERSION = "1.30.0"
static final String GRPC_VERSION = "1.32.1"

static final String PLUGIN_CODE = 'com.lightbend.akka.grpc.gradle'

// workaround for tests, where there's no jar and MANIFEST.MF can't be read
final String pluginVersion = System.getProperty("akkaGrpcTest.pluginVersion", AkkaGrpcPlugin.class.package.implementationVersion)
Expand All @@ -21,6 +21,8 @@ class AkkaGrpcPluginExtension {
boolean generatePlay = false
boolean serverPowerApis = false
boolean usePlayActions = false
boolean includeStdTypes = false

List<String> extraGenerators = []

private final Project project
Expand Down
4 changes: 2 additions & 2 deletions gradle-plugin/src/test/groovy/unit/DependenciesSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DependenciesSpec extends BaseSpec {

def checkCodegen(Dependency d, AkkaGrpcPluginExtension ext) {
assert d.group == "com.lightbend.akka.grpc"
assert d.name == "akka-grpc-codegen_$PROTOC_PLUGIN_SCALA_VERSION"
assert d.name == "akka-grpc-codegen_${PROTOC_PLUGIN_SCALA_VERSION}"
assert d.version == ext.pluginVersion
true
}
Expand All @@ -25,7 +25,7 @@ class DependenciesSpec extends BaseSpec {

def checkScalapb(Dependency d, AkkaGrpcPluginExtension ext) {
assert d.group == "com.lightbend.akka.grpc"
assert d.name == "akka-grpc-scalapb-protoc-plugin_$PROTOC_PLUGIN_SCALA_VERSION"
assert d.name == "akka-grpc-scalapb-protoc-plugin_${PROTOC_PLUGIN_SCALA_VERSION}"
assert d.version == ext.pluginVersion
true
}
Expand Down
16 changes: 16 additions & 0 deletions maven-plugin/src/main/maven/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
<editable>true</editable>
<description>The proto compiler version</description>
</parameter>
<parameter>
<name>includeStdTypes</name>
<type>boolean</type>
<required>false</required>
<editable>true</editable>
<description>If "true", extract the included google.protobuf standard types and add them to protoc import path.</description>
</parameter>
</parameters>
<configuration>
<project implementation="" default-value="${project}"/>
Expand All @@ -88,6 +95,7 @@
<protoPaths default-value="src/main/proto,src/main/protobuf">${akka-grpc.protoPaths}</protoPaths>
<outputDirectory default-value="target/generated-sources">${akka-grpc.outputDirectory}</outputDirectory>
<protocVersion implementation="java.lang.String" default-value="-v3.11.4">${akka-grpc.protoc-version}</protocVersion>
<includeStdTypes implementation="boolean" default-value="false" />
</configuration>
</mojo>
<mojo>
Expand Down Expand Up @@ -160,6 +168,13 @@
<editable>true</editable>
<description>The proto compiler version</description>
</parameter>
<parameter>
<name>includeStdTypes</name>
<type>boolean</type>
<required>false</required>
<editable>true</editable>
<description>If "true", extract the included google.protobuf standard types and add them to protoc import path.</description>
</parameter>
</parameters>
<configuration>
<project implementation="" default-value="${project}"/>
Expand All @@ -171,6 +186,7 @@
<protoPaths default-value="src/test/proto,src/test/protobuf">${akka-grpc.protoPaths}</protoPaths>
<outputDirectory default-value="target/generated-test-sources">${akka-grpc.outputDirectory}</outputDirectory>
<protocVersion implementation="java.lang.String" default-value="-v3.11.4">${akka-grpc.protoc-version}</protocVersion>
<includeStdTypes implementation="boolean" default-value="false" />
</configuration>
</mojo>
</mojos>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ abstract class AbstractGenerateMojo @Inject() (buildContext: BuildContext) exten
@BeanProperty
var extraGenerators: java.util.ArrayList[String] = _

@BeanProperty
var includeStdTypes: Boolean = _

@BeanProperty
var protocVersion: String = _

Expand Down Expand Up @@ -186,7 +189,7 @@ abstract class AbstractGenerateMojo @Inject() (buildContext: BuildContext) exten

val runProtoc: Seq[String] => Int = args =>
com.github.os72.protocjar.Protoc.runProtoc(protocVersion +: args.toArray)
val protocOptions = Seq.empty
val protocOptions = if (includeStdTypes) Seq("--include_std_types") else Seq.empty

compile(runProtoc, schemas, protoDir, protocOptions, targets)
}
Expand Down
4 changes: 2 additions & 2 deletions plugin-tester-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ akkaGrpc {
generateClient = true
generateServer = true
serverPowerApis = true // This isn't default
includeStdTypes = true // This isn't default
extraGenerators = [ ]
}

Expand All @@ -18,9 +19,8 @@ def scalaVersion = org.gradle.util.VersionNumber.parse(System.getenv("TRAVIS_SCA
def scalaBinaryVersion = "${scalaVersion.major}.${scalaVersion.minor}"

dependencies {
implementation group: 'ch.megard', name: "akka-http-cors_${scalaBinaryVersion}", version: '0.4.2'
implementation group: 'ch.megard', name: "akka-http-cors_${scalaBinaryVersion}", version: '1.1.0'
testImplementation "com.typesafe.akka:akka-stream-testkit_${scalaBinaryVersion}:2.5.31"
testImplementation "org.scalatest:scalatest_${scalaBinaryVersion}:3.1.2"
testImplementation "org.scalatestplus:junit-4-12_${scalaBinaryVersion}:3.1.2.0"
}

9 changes: 6 additions & 3 deletions plugin-tester-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<akka.http.cors.version>0.4.2</akka.http.cors.version>
<maven-dependency-plugin.version>3.1.2</maven-dependency-plugin.version>
<maven-exec-plugin.version>3.0.0</maven-exec-plugin.version>
<akka.http.cors.version>1.1.0</akka.http.cors.version>
<grpc.version>1.32.1</grpc.version> <!-- checked synced by GrpcVersionSyncCheckPlugin -->
<project.encoding>UTF-8</project.encoding>
</properties>
Expand Down Expand Up @@ -54,12 +56,13 @@
<generatorSettings>
<serverPowerApis>true</serverPowerApis>
</generatorSettings>
<includeStdTypes>true</includeStdTypes>
</configuration>
</plugin>

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<id>getClasspathFilenames</id>
Expand All @@ -74,7 +77,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<version>${maven-exec-plugin.version}</version>
<configuration>
<executable>java</executable>
<arguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
package example.myapp.helloworld;

import akka.actor.ActorSystem;
import akka.http.javadsl.*;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.Http;
import akka.http.javadsl.ServerBinding;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import example.myapp.helloworld.grpc.*;
import example.myapp.helloworld.grpc.GreeterService;
import example.myapp.helloworld.grpc.GreeterServiceHandlerFactory;

import java.util.concurrent.CompletionStage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;

import com.google.protobuf.Timestamp;
import example.myapp.helloworld.grpc.*;

public class GreeterServiceImpl implements GreeterService {
Expand All @@ -27,7 +28,10 @@ public GreeterServiceImpl(Materializer mat) {
@Override
public CompletionStage<HelloReply> sayHello(HelloRequest in) {
System.out.println("sayHello to " + in.getName());
HelloReply reply = HelloReply.newBuilder().setMessage("Hello, " + in.getName()).build();
HelloReply reply = HelloReply.newBuilder()
.setMessage("Hello, " + in.getName())
.setTimestamp(Timestamp.newBuilder().setSeconds(1234567890).setNanos(12345).build())
.build();
return CompletableFuture.completedFuture(reply);
}

Expand Down
3 changes: 3 additions & 0 deletions plugin-tester-java/src/main/protobuf/helloworld.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
syntax = "proto3";

import "google/protobuf/timestamp.proto";

option java_multiple_files = true;
option java_package = "example.myapp.helloworld.grpc";
option java_outer_classname = "HelloWorldProto";
Expand Down Expand Up @@ -38,4 +40,5 @@ message HelloRequest {
// The response message containing the greetings
message HelloReply {
string message = 1;
google.protobuf.Timestamp timestamp = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.grpc.GrpcClientSettings
import akka.stream.ActorMaterializer
import com.google.protobuf.Timestamp
import com.typesafe.config.ConfigFactory
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.Span
Expand All @@ -21,7 +22,8 @@ import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class JGreeterServiceSpec extends Matchers with AnyWordSpecLike with BeforeAndAfterAll with ScalaFutures {
implicit val patience = PatienceConfig(5.seconds, Span(100, org.scalatest.time.Millis))
implicit val patience: PatienceConfig =
PatienceConfig(5.seconds, Span(100, org.scalatest.time.Millis))

implicit val serverSystem: ActorSystem = {
// important to enable HTTP/2 in server ActorSystem's config
Expand Down Expand Up @@ -52,7 +54,10 @@ class JGreeterServiceSpec extends Matchers with AnyWordSpecLike with BeforeAndAf
"GreeterService" should {
"reply to single request" in {
val reply = clients.head.sayHello(HelloRequest.newBuilder.setName("Alice").build())
reply.toCompletableFuture.get should ===(HelloReply.newBuilder.setMessage("Hello, Alice").build())
val timestamp = Timestamp.newBuilder.setSeconds(1234567890).setNanos(12345).build()
val expectedResponse =
HelloReply.newBuilder.setMessage("Hello, Alice").setTimestamp(timestamp).build()
reply.toCompletableFuture.get should ===(expectedResponse)
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugin-tester-scala/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def scalaVersion = org.gradle.util.VersionNumber.parse(System.getenv("TRAVIS_SCA
def scalaBinaryVersion = "${scalaVersion.major}.${scalaVersion.minor}"

dependencies {
implementation group: 'ch.megard', name: "akka-http-cors_${scalaBinaryVersion}", version: '0.4.2'
implementation group: 'ch.megard', name: "akka-http-cors_${scalaBinaryVersion}", version: '1.1.0'
testImplementation "com.typesafe.akka:akka-stream-testkit_${scalaBinaryVersion}:2.5.31"
testImplementation "org.scalatest:scalatest_${scalaBinaryVersion}:3.1.2"
testImplementation "org.scalatestplus:junit-4-12_${scalaBinaryVersion}:3.1.2.0"
Expand Down
1 change: 1 addition & 0 deletions plugin-tester-scala/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<generatorSettings>
<serverPowerApis>true</serverPowerApis>
</generatorSettings>
<includeStdTypes>true</includeStdTypes>
</configuration>
<executions>
<execution>
Expand Down
1 change: 1 addition & 0 deletions plugin-tester-scala/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pluginManagement {
if (!System.getProperty('akka.grpc.project.version')) {
throw new GradleException("System property `akka.grpc.project.version` is not provided. Use -Dakka.grpc.project.version=\$VERSION.")
}

3 changes: 3 additions & 0 deletions plugin-tester-scala/src/main/protobuf/helloworld.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
syntax = "proto3";

import "google/protobuf/timestamp.proto";

//#options
option java_multiple_files = true;
option java_package = "example.myapp.helloworld.grpc";
Expand Down Expand Up @@ -43,5 +45,6 @@ message HelloRequest {
// The response message containing the greetings
message HelloReply {
string message = 1;
google.protobuf.Timestamp timestamp = 2;
}
//#messages
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
package example.myapp.helloworld

import scala.concurrent.Future

import akka.NotUsed
import akka.stream.Materializer
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Source

import com.google.protobuf.timestamp.Timestamp
import example.myapp.helloworld.grpc._

class GreeterServiceImpl(implicit mat: Materializer) extends GreeterService {
import mat.executionContext

override def sayHello(in: HelloRequest): Future[HelloReply] = {
println(s"sayHello to ${in.name}")
Future.successful(HelloReply(s"Hello, ${in.name}"))
Future.successful(HelloReply(s"Hello, ${in.name}", Some(Timestamp.apply(123456, 123))))
}

override def itKeepsTalking(in: Source[HelloRequest, NotUsed]): Future[HelloReply] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package example.myapp.helloworld
import akka.actor.{ ActorSystem, ClassicActorSystemProvider }
import akka.grpc.GrpcClientSettings
import akka.stream.ActorMaterializer
import com.google.protobuf.timestamp.Timestamp
import com.typesafe.config.ConfigFactory
import example.myapp.helloworld.grpc._
import org.junit.runner.RunWith
Expand Down Expand Up @@ -57,7 +58,7 @@ class GreeterSpec extends Matchers with AnyWordSpecLike with BeforeAndAfterAll w
"GreeterService" should {
"reply to single request" in {
val reply = clients.head.sayHello(HelloRequest("Alice"))
reply.futureValue should ===(HelloReply("Hello, Alice"))
reply.futureValue should ===(HelloReply("Hello, Alice", Some(Timestamp.apply(123456, 123))))
}
}

Expand Down
3 changes: 3 additions & 0 deletions project/ReflectiveCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ object ReflectiveCodeGen extends AutoPlugin {
val generatedSources = SettingKey[Seq[String]]("reflectiveGrpcGeneratedSources")
val extraGenerators = SettingKey[Seq[String]]("reflectiveGrpcExtraGenerators")
val codeGeneratorSettings = settingKey[Seq[String]]("Code generator settings")
val protocOptions = settingKey[Seq[String]]("Protoc Options.")

// needed to be able to override the PB.generate task reliably
override def requires = ProtocPlugin

override def projectSettings: Seq[Def.Setting[_]] =
inConfig(Compile)(
Seq(
PB.protocOptions := protocOptions.value,
PB.generate :=
// almost the same as `Def.sequential` but will return the "middle" value, ie. the result of the generation
// Defines three steps:
Expand Down Expand Up @@ -82,6 +84,7 @@ object ReflectiveCodeGen extends AutoPlugin {
generatedLanguages in Global := Seq("Scala"),
generatedSources in Global := Seq("Client", "Server"),
extraGenerators in Global := Seq.empty,
protocOptions in Global := Seq.empty,
watchSources ++= (watchSources in ProjectRef(file("."), "akka-grpc-codegen")).value,
watchSources ++= (watchSources in ProjectRef(file("."), "sbt-akka-grpc")).value)

Expand Down
Loading

0 comments on commit 7b4d0cd

Please sign in to comment.