Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix missing include statements in the proto compilation and also improve the syntax of the proto include/exclude clauses (comma-separated list accepting spaces)
  • Loading branch information
cescoffier authored and holly-cummins committed Feb 8, 2024
1 parent 485d812 commit 686e8af
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 16 deletions.
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/grpc-getting-started.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ For instance, having the following properties in your `application.properties`:

[source,properties]
----
quarkus.generate-code.grpc.scan-for-proto-includes."<groupId>\:<artifactId>"=foo/**,bar/**,banana/a-proto.proto
quarkus.generate-code.grpc.scan-for-proto-excludes."<groupId>\:<artifactId>"=foo/private/**,bar/another-proto.proto
quarkus.generate-code.grpc.scan-for-proto-include."<groupId>\:<artifactId>"=foo/**,bar/**,banana/a-proto.proto
quarkus.generate-code.grpc.scan-for-proto-exclude."<groupId>\:<artifactId>"=foo/private/**,bar/another-proto.proto
----

will include:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -100,11 +101,14 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
Collection<Path> protoFilesFromDependencies = gatherProtosFromDependencies(dirWithProtosFromDependencies, protoDirs,
context);
if (!protoFilesFromDependencies.isEmpty()) {
protoFilesFromDependencies.stream()
.map(Path::normalize)
.map(Path::toAbsolutePath)
.map(Path::toString)
.forEach(protoFiles::add);
for (Path files : protoFilesFromDependencies) {
var pathToProtoFile = files.normalize().toAbsolutePath();
var pathToParentDir = files.getParent();
// Add the proto file to the list of proto to compile, but also add the directory containing the
// proto file to the list of directories to include (it's a set, so no duplicate).
protoFiles.add(pathToProtoFile.toString());
protoDirs.add(pathToParentDir.toString());
}
}

if (!protoFiles.isEmpty()) {
Expand All @@ -115,12 +119,12 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {

List<String> command = new ArrayList<>();
command.add(executables.protoc.toString());
for (String protoImportDir : protosToImport) {
command.add(String.format("-I=%s", escapeWhitespace(protoImportDir)));
}
for (String protoDir : protoDirs) {
command.add(String.format("-I=%s", escapeWhitespace(protoDir)));
}
for (String protoImportDir : protosToImport) {
command.add(String.format("-I=%s", escapeWhitespace(protoImportDir)));
}

command.addAll(asList("--plugin=protoc-gen-grpc=" + executables.grpc,
"--plugin=protoc-gen-q-grpc=" + executables.quarkusGrpc,
Expand Down Expand Up @@ -203,17 +207,21 @@ private Collection<Path> gatherProtosFromDependencies(Path workDir, Set<String>
}
boolean scanAll = "all".equalsIgnoreCase(scanDependencies);

List<String> dependenciesToScan = asList(scanDependencies.split(","));
List<String> dependenciesToScan = Arrays.stream(scanDependencies.split(",")).map(String::trim)
.collect(Collectors.toList());

ApplicationModel appModel = context.applicationModel();
List<Path> protoFilesFromDependencies = new ArrayList<>();
for (ResolvedDependency artifact : appModel.getRuntimeDependencies()) {
String packageId = String.format("%s:%s", artifact.getGroupId(), artifact.getArtifactId());
Collection<String> includes = properties
.getOptionalValues(String.format(SCAN_DEPENDENCIES_FOR_PROTO_INCLUDE_PATTERN, packageId), String.class)
.getOptionalValue(String.format(SCAN_DEPENDENCIES_FOR_PROTO_INCLUDE_PATTERN, packageId), String.class)
.map(s -> Arrays.stream(s.split(",")).map(String::trim).collect(Collectors.toList()))
.orElse(List.of());

Collection<String> excludes = properties
.getOptionalValues(String.format(SCAN_DEPENDENCIES_FOR_PROTO_EXCLUDE_PATTERN, packageId), String.class)
.getOptionalValue(String.format(SCAN_DEPENDENCIES_FOR_PROTO_EXCLUDE_PATTERN, packageId), String.class)
.map(s -> Arrays.stream(s.split(",")).map(String::trim).collect(Collectors.toList()))
.orElse(List.of());

if (scanAll
Expand Down Expand Up @@ -247,7 +255,8 @@ private Collection<String> gatherDirectoriesWithImports(Path workDir, CodeGenCon
}

boolean scanAll = "all".equals(scanForImports.toLowerCase(Locale.getDefault()));
List<String> dependenciesToScan = Arrays.asList(scanForImports.split(","));
List<String> dependenciesToScan = Arrays.stream(scanForImports.split(",")).map(String::trim)
.collect(Collectors.toList());

Set<String> importDirectories = new HashSet<>();
ApplicationModel appModel = context.applicationModel();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";

option java_package = "org.acme.protos.extended";
option java_outer_classname = "ExtendedProtos";

option optimize_for = CODE_SIZE;

package org.acme.proto.extended;

// Import the base proto file
import "base.proto";

// A message representing detailed user information
message DetailedUser {
org.acme.protos.base.User user = 1; // Use the User message from base.proto
org.acme.protos.base.Address address = 2; // Use the Address message from base.proto
string phone_number = 3;
}

// Another message using the base Address message
message Company {
string name = 1;
org.acme.protos.base.Address company_address = 2;
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
quarkus.generate-code.grpc.scan-for-proto=io.quarkus:quarkus-integration-test-external-proto
quarkus.generate-code.grpc.scan-for-proto-include."io.quarkus\:quarkus-integration-test-external-proto"=dir/**,invalids/invalid2.proto
quarkus.generate-code.grpc.scan-for-proto-exclude."io.quarkus\:quarkus-integration-test-external-proto"=dir/invalid.proto,invalids/invalid2.proto
quarkus.generate-code.grpc.scan-for-proto-include."io.quarkus\:quarkus-integration-test-external-proto"=dir/**, invalids/invalid2.proto, protobuf/**
quarkus.generate-code.grpc.scan-for-proto-exclude."io.quarkus\:quarkus-integration-test-external-proto"=dir/invalid.proto, invalids/invalid2.proto

%vertx.quarkus.grpc.clients.hello.host=localhost
%vertx.quarkus.grpc.clients.hello.port=8081
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";

option java_package = "org.acme.protos.base";
option java_outer_classname = "BASEProtos";

option optimize_for = CODE_SIZE;

// Import the extra proto file
import "role.proto";

package org.acme.protos.base;

// A basic message representing user information
message User {
int32 id = 1;
string name = 2;
string email = 3;
org.acme.protos.role.Role role = 4; // Use the Role message from extra.proto
}

// A basic message representing an address
message Address {
string street = 1;
string city = 2;
string state = 3;
string zip = 4;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

option java_package = "org.acme.protos.role";
option java_outer_classname = "RoleProtos";

option optimize_for = CODE_SIZE;

package org.acme.protos.role;

// A basic message representing a role
message Role {
int32 role_id = 1;
string role_name = 2;
}

0 comments on commit 686e8af

Please sign in to comment.