Skip to content

Commit

Permalink
gRPC - align service names generated by quarkus with grpc-java
Browse files Browse the repository at this point in the history
- resolves #27170
  • Loading branch information
mkouba committed Aug 8, 2022
1 parent b08a139 commit d4cbb35
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions extensions/grpc/deployment/src/test/proto/goodbyeworld.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package goodbyeworld;
// The farewell service definition.
service Farewell {
// Sends a farewell
rpc SayGoodbye (GoodbyeRequest) returns (GoodbyeReply) {}
rpc Say_goodbye (GoodbyeRequest) returns (GoodbyeReply) {}
}

// The request message containing the user's name.
Expand All @@ -21,4 +21,4 @@ message GoodbyeRequest {
// The response message containing the farewells
message GoodbyeReply {
string message = 1;
}
}
5 changes: 5 additions & 0 deletions extensions/grpc/protoc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.grpc.protoc.plugin;

import java.beans.Introspector;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -8,6 +9,8 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.lang.model.SourceVersion;

import com.google.common.base.Strings;
import com.google.common.html.HtmlEscapers;
import com.google.protobuf.DescriptorProtos;
Expand Down Expand Up @@ -137,7 +140,7 @@ private ServiceContext buildServiceContext(DescriptorProtos.ServiceDescriptorPro
private MethodContext buildMethodContext(DescriptorProtos.MethodDescriptorProto methodProto, ProtoTypeMap typeMap,
List<DescriptorProtos.SourceCodeInfo.Location> locations, int methodNumber) {
MethodContext methodContext = new MethodContext();
methodContext.methodName = lowerCaseFirst(methodProto.getName());
methodContext.methodName = adaptMethodName(methodProto.getName());
methodContext.inputType = typeMap.toJavaTypeName(methodProto.getInputType());
methodContext.outputType = typeMap.toJavaTypeName(methodProto.getOutputType());
methodContext.deprecated = methodProto.getOptions() != null && methodProto.getOptions().getDeprecated();
Expand Down Expand Up @@ -171,8 +174,31 @@ private MethodContext buildMethodContext(DescriptorProtos.MethodDescriptorProto
return methodContext;
}

private String lowerCaseFirst(String s) {
return Character.toLowerCase(s.charAt(0)) + s.substring(1);
static String adaptMethodName(String name) {
// We need to adjust the method name in the same way as the grpc-java compiler does:
// 1. Decapitalize
// 2. Replace an underscore and capitalize the following letter
// 3. Append the underscore for a java keyword
// (these rules are bit odd but we need to follow them unless we get rid of the grpc-java compiler)
// https://github.com/quarkusio/quarkus/issues/27170
name = Introspector.decapitalize(name);
if (name.contains("_")) {
StringBuilder res = new StringBuilder();
for (String str : name.split("_")) {
if (res.length() == 0) {
res.append(str);
} else {
res.append(Character.toUpperCase(str.charAt(0)));
if (str.length() > 1) {
res.append(str.substring(1));
}
}
}
name = res.toString();
} else if (SourceVersion.isKeyword(name)) {
name += "_";
}
return name;
}

private List<PluginProtos.CodeGeneratorResponse.File> generateFiles(List<ServiceContext> services) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.grpc.protoc.plugin;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class MutinyGrpcGeneratorTest {

@Test
public void testAdaptMethodName() {
assertEquals("sayHello", MutinyGrpcGenerator.adaptMethodName("SayHello"));
assertEquals("sayHello", MutinyGrpcGenerator.adaptMethodName("Say_Hello"));
assertEquals("sayHello", MutinyGrpcGenerator.adaptMethodName("Say_hello"));
assertEquals("sayHelloAndBye", MutinyGrpcGenerator.adaptMethodName("Say_Hello_and_Bye"));
assertEquals("return", MutinyGrpcGenerator.adaptMethodName("return_"));
}

}

0 comments on commit d4cbb35

Please sign in to comment.