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

Ability to generate gRPC descriptor set #36930

Merged
merged 1 commit into from
Nov 12, 2023
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
15 changes: 15 additions & 0 deletions docs/src/main/asciidoc/grpc-generation-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,22 @@
}
----

== Generating Descriptor Set

Check warning on line 102 in docs/src/main/asciidoc/grpc-generation-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Headings] Use sentence-style capitalization in 'Generating Descriptor Set'. Raw Output: {"message": "[Quarkus.Headings] Use sentence-style capitalization in 'Generating Descriptor Set'.", "location": {"path": "docs/src/main/asciidoc/grpc-generation-reference.adoc", "range": {"start": {"line": 102, "column": 4}}}, "severity": "INFO"}
Protocol Buffers do not contain descriptions of their own types. Thus, given only a raw message without the corresponding .proto file defining its type, it is difficult to extract any useful data. However, the contents of a .proto file can itself be https://protobuf.dev/programming-guides/techniques/#self-description[represented using protocol buffers].

Check warning on line 103 in docs/src/main/asciidoc/grpc-generation-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'therefore' rather than 'Thus' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'therefore' rather than 'Thus' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/grpc-generation-reference.adoc", "range": {"start": {"line": 103, "column": 66}}}, "severity": "WARNING"}

By default, Quarkus does not generate these descriptors. Quarkus does provide several configuration options for generating them. These would be added to your `application.properties` or `application.yml` file:

* `quarkus.generate-code.grpc.descriptor-set.generate`
** Set to `true` to enable generation
* `quarkus.generate-code.grpc.descriptor-set.output-dir`
** Set this to a value relative to the project's build directory (i.e. `target` for Maven, `build` for Gradle)

Check failure on line 110 in docs/src/main/asciidoc/grpc-generation-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsErrors] Use 'you' rather than 'i'. Raw Output: {"message": "[Quarkus.TermsErrors] Use 'you' rather than 'i'.", "location": {"path": "docs/src/main/asciidoc/grpc-generation-reference.adoc", "range": {"start": {"line": 110, "column": 67}}}, "severity": "ERROR"}
** Maven default value: `target/generated-sources/grpc`
** Gradle default value: `$buildDir/classes/java/quarkus-generated-sources/grpc`
* `quarkus.generate-code.grpc.descriptor-set.name`
** Name of the descriptor set file to generate
** Default value: `descriptor_set.dsc`

== Configuring gRPC code generation for dependencies

Check warning on line 117 in docs/src/main/asciidoc/grpc-generation-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'might (for possiblity)' or 'can (for ability)' rather than 'may' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'might (for possiblity)' or 'can (for ability)' rather than 'may' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/grpc-generation-reference.adoc", "range": {"start": {"line": 117, "column": 33}}}, "severity": "WARNING"}

You may have dependencies that contain `\*.proto` files you want to compile to Java sources.
This section explains how to configure code generation to include these `\*.proto` files during code generation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.grpc.deployment;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static java.util.Arrays.asList;

Expand Down Expand Up @@ -56,6 +57,9 @@ public class GrpcCodeGen implements CodeGenProvider {
private static final String SCAN_FOR_IMPORTS = "quarkus.generate-code.grpc.scan-for-imports";

private static final String POST_PROCESS_SKIP = "quarkus.generate.code.grpc-post-processing.skip";
private static final String GENERATE_DESCRIPTOR_SET = "quarkus.generate-code.grpc.descriptor-set.generate";
private static final String DESCRIPTOR_SET_OUTPUT_DIR = "quarkus.generate-code.grpc.descriptor-set.output-dir";
private static final String DESCRIPTOR_SET_FILENAME = "quarkus.generate-code.grpc.descriptor-set.name";

private Executables executables;
private String input;
Expand Down Expand Up @@ -149,6 +153,11 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
"--q-grpc_out=" + outDir,
"--grpc_out=" + outDir,
"--java_out=" + outDir));

if (shouldGenerateDescriptorSet(context.config())) {
command.add(String.format("--descriptor_set_out=%s", getDescriptorSetOutputFile(context)));
}

command.addAll(protoFiles);

ProcessBuilder processBuilder = new ProcessBuilder(command);
Expand Down Expand Up @@ -262,6 +271,25 @@ private boolean isGeneratingFromAppDependenciesEnabled(Config config) {
.filter(value -> !"none".equals(value)).isPresent();
}

private boolean shouldGenerateDescriptorSet(Config config) {
return config.getOptionalValue(GENERATE_DESCRIPTOR_SET, Boolean.class).orElse(FALSE);
}

private Path getDescriptorSetOutputFile(CodeGenContext context) throws IOException {
var dscOutputDir = context.config().getOptionalValue(DESCRIPTOR_SET_OUTPUT_DIR, String.class)
.map(context.workDir()::resolve)
.orElseGet(context::outDir);

if (Files.notExists(dscOutputDir)) {
Files.createDirectories(dscOutputDir);
}

var dscFilename = context.config().getOptionalValue(DESCRIPTOR_SET_FILENAME, String.class)
.orElse("descriptor_set.dsc");

return dscOutputDir.resolve(dscFilename).normalize();
}

private Collection<String> gatherDirectoriesWithImports(Path workDir, CodeGenContext context) throws CodeGenException {
Config properties = context.config();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id 'java'
id 'io.quarkus'
}

repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
includeGroup 'org.hibernate.orm'
}
}
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'
implementation 'io.quarkus:quarkus-grpc'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

test {
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
includeGroup 'org.hibernate.orm'
}
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}
rootProject.name = 'grpc-descriptor-set-alternate-output-dir'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.acme.quarkus.sample;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import io.quarkus.example.HelloMsg;

@Path("/hello")
public class HelloResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
Integer number = HelloMsg.Status.TEST_ONE.getNumber();
// return a thing from proto file (for devmode test)
return "hello " + number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";
package io.quarkus.example;

option java_multiple_files = true;
option java_package = "io.quarkus.example";

import "google/protobuf/timestamp.proto";


message HelloMsg {
enum Status {
UNKNOWN = 0;
NOT_SERVING = 1;
TEST_ONE = 2;
}
string message = 1;
google.protobuf.Timestamp date_time = 2;
Status status = 3;
}

service DevModeService {
rpc Check(HelloMsg) returns (HelloMsg);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkus.generate-code.grpc.descriptor-set.generate=true
quarkus.generate-code.grpc.descriptor-set.name=hello.dsc
quarkus.generate-code.grpc.descriptor-set.output-dir=proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id 'java'
id 'io.quarkus'
}

repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
includeGroup 'org.hibernate.orm'
}
}
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'
implementation 'io.quarkus:quarkus-grpc'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

test {
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
includeGroup 'org.hibernate.orm'
}
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}
rootProject.name = 'grpc-descriptor-set-alternate-output'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.acme.quarkus.sample;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import io.quarkus.example.HelloMsg;

@Path("/hello")
public class HelloResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
Integer number = HelloMsg.Status.TEST_ONE.getNumber();
// return a thing from proto file (for devmode test)
return "hello " + number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";
package io.quarkus.example;

option java_multiple_files = true;
option java_package = "io.quarkus.example";

import "google/protobuf/timestamp.proto";


message HelloMsg {
enum Status {
UNKNOWN = 0;
NOT_SERVING = 1;
TEST_ONE = 2;
}
string message = 1;
google.protobuf.Timestamp date_time = 2;
Status status = 3;
}

service DevModeService {
rpc Check(HelloMsg) returns (HelloMsg);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.generate-code.grpc.descriptor-set.generate=true
quarkus.generate-code.grpc.descriptor-set.name=hello.dsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id 'java'
id 'io.quarkus'
}

repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
includeGroup 'org.hibernate.orm'
}
}
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'
implementation 'io.quarkus:quarkus-grpc'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

test {
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
includeGroup 'org.hibernate.orm'
}
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}
rootProject.name = 'grpc-descriptor-set'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.acme.quarkus.sample;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import io.quarkus.example.HelloMsg;

@Path("/hello")
public class HelloResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
Integer number = HelloMsg.Status.TEST_ONE.getNumber();
// return a thing from proto file (for devmode test)
return "hello " + number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";
package io.quarkus.example;

option java_multiple_files = true;
option java_package = "io.quarkus.example";

import "google/protobuf/timestamp.proto";


message HelloMsg {
enum Status {
UNKNOWN = 0;
NOT_SERVING = 1;
TEST_ONE = 2;
}
string message = 1;
google.protobuf.Timestamp date_time = 2;
Status status = 3;
}

service DevModeService {
rpc Check(HelloMsg) returns (HelloMsg);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.generate-code.grpc.descriptor-set.generate=true
Loading
Loading