Skip to content

Commit

Permalink
Options for generating gRPC descriptor set
Browse files Browse the repository at this point in the history
  • Loading branch information
edeandrea committed Nov 9, 2023
1 parent 0741b56 commit 43b7e08
Show file tree
Hide file tree
Showing 43 changed files with 955 additions and 0 deletions.
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,6 +99,21 @@ quarkus {
}
----

== Generating Descriptor Set
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].

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)
** 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

You may have dependencies that contain `\*.proto` files you want to compile to Java sources.
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

0 comments on commit 43b7e08

Please sign in to comment.