From 3c3903298beb4a54dc219cc4305367b770e1f123 Mon Sep 17 00:00:00 2001 From: Ruslan Sennov Date: Thu, 23 Nov 2023 15:28:20 +0300 Subject: [PATCH 1/2] fix java keywords --- .../gen/ReactiveGrpcGenerator.java | 37 ++++++++-- .../com/salesforce/rxgrpc/KeywordsTest.java | 72 +++++++++++++++++++ .../src/test/proto/helloworld.proto | 14 +++- 3 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 rx-java/rxgrpc-test/src/test/java/com/salesforce/rxgrpc/KeywordsTest.java diff --git a/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java b/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java index 9266f120..3057f161 100644 --- a/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java +++ b/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java @@ -19,10 +19,7 @@ import com.salesforce.jprotoc.GeneratorException; import com.salesforce.jprotoc.ProtoTypeMap; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; /** @@ -33,6 +30,19 @@ public abstract class ReactiveGrpcGenerator extends Generator { private static final int SERVICE_NUMBER_OF_PATHS = 2; private static final int METHOD_NUMBER_OF_PATHS = 4; + // https://github.com/grpc/grpc-java/blob/v1.59.0/compiler/src/java_plugin/cpp/java_generator.cpp#L150 + private static final Set JAVA_KEYWORDS = new HashSet<>(Arrays.asList( + "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", + "class", "const", "continue", "default", "do", "double", "else", "enum", + "extends", "final", "finally", "float", "for", "goto", "if", "implements", + "import", "instanceof", "int", "interface", "long", "native", "new", "package", + "private", "protected", "public", "return", "short", "static", "strictfp", + "super", "switch", "synchronized", "this", "throw", "throws", "transient", + "try", "void", "volatile", "while", + // additional ones added by us + "true", "false" + )); + protected abstract String getClassPrefix(); private String getServiceJavaDocPrefix() { @@ -126,9 +136,18 @@ private ServiceContext buildServiceContext(ServiceDescriptorProto serviceProto, return serviceContext; } + private String fixKeywords(String s) { + if (JAVA_KEYWORDS.contains(s)) { + return s + "_"; + } else { + return s; + } + } + private MethodContext buildMethodContext(MethodDescriptorProto methodProto, ProtoTypeMap typeMap, List locations, int methodNumber) { MethodContext methodContext = new MethodContext(); - methodContext.methodName = lowerCaseFirst(methodProto.getName()); + methodContext.originalName = lowerCaseFirst(methodProto.getName()); + methodContext.methodName = fixKeywords(methodContext.originalName); methodContext.inputType = typeMap.toJavaTypeName(methodProto.getInputType()); methodContext.outputType = typeMap.toJavaTypeName(methodProto.getOutputType()); methodContext.deprecated = methodProto.getOptions() != null && methodProto.getOptions().getDeprecated(); @@ -235,6 +254,7 @@ public List unaryRequestMethods() { */ private class MethodContext { // CHECKSTYLE DISABLE VisibilityModifier FOR 10 LINES + public String originalName; public String methodName; public String inputType; public String outputType; @@ -266,7 +286,12 @@ public String methodNamePascalCase() { } public String methodNameCamelCase() { - String mn = methodName.replace("_", ""); + String mn; + if (JAVA_KEYWORDS.contains(originalName)) { + mn = methodName; + } else { + mn = methodName.replace("_", ""); + } return String.valueOf(Character.toLowerCase(mn.charAt(0))) + mn.substring(1); } } diff --git a/rx-java/rxgrpc-test/src/test/java/com/salesforce/rxgrpc/KeywordsTest.java b/rx-java/rxgrpc-test/src/test/java/com/salesforce/rxgrpc/KeywordsTest.java new file mode 100644 index 00000000..d96ca3e8 --- /dev/null +++ b/rx-java/rxgrpc-test/src/test/java/com/salesforce/rxgrpc/KeywordsTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2019, Salesforce.com, Inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ + +package com.salesforce.rxgrpc; + +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import io.grpc.Server; +import io.grpc.ServerBuilder; +import io.reactivex.Single; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; + +@SuppressWarnings("Duplicates") +public class KeywordsTest { + @Rule + public UnhandledRxJavaErrorRule errorRule = new UnhandledRxJavaErrorRule().autoVerifyNoError(); + + private static Server server; + private static ManagedChannel channel; + + @BeforeClass + public static void setupServer() throws Exception { + RxKeywordsGrpc.KeywordsImplBase svc = new RxKeywordsGrpc.KeywordsImplBase() { + + @Override + public Single boolean_(Single request) { + return request.map(r -> !r.getValue()).map(b -> BoolResponse.newBuilder().setValue(b).build()); + } + }; + + server = ServerBuilder.forPort(9000).addService(svc).build().start(); + channel = ManagedChannelBuilder.forAddress("localhost", server.getPort()).usePlaintext().build(); + } + + @AfterClass + public static void stopServer() throws InterruptedException { + server.shutdown(); + server.awaitTermination(); + channel.shutdown(); + + server = null; + channel = null; + } + + @Test + public void not() { + AtomicBoolean called = new AtomicBoolean(false); + RxKeywordsGrpc.RxKeywordsStub stub = RxKeywordsGrpc.newRxStub(channel); + + BoolRequest request = BoolRequest.newBuilder().setValue(false).build(); + + stub.boolean_(request).map(BoolResponse::getValue).subscribe(r -> { + assertThat(r.booleanValue()).isTrue(); + called.set(true); + }); + + await().atMost(1, TimeUnit.SECONDS).untilTrue(called); + } +} diff --git a/rx-java/rxgrpc-test/src/test/proto/helloworld.proto b/rx-java/rxgrpc-test/src/test/proto/helloworld.proto index 665564ca..f957d79e 100644 --- a/rx-java/rxgrpc-test/src/test/proto/helloworld.proto +++ b/rx-java/rxgrpc-test/src/test/proto/helloworld.proto @@ -15,6 +15,10 @@ service Greeter { rpc SayHelloBothStream (stream HelloRequest) returns (stream HelloResponse) {} } +service Keywords { + rpc Boolean (BoolRequest) returns (BoolResponse) {} +} + service Dismisser { rpc SayGoodbye (HelloRequest) returns (HelloResponse) {} } @@ -27,4 +31,12 @@ message HelloRequest { // The response message containing the greetings message HelloResponse { string message = 1; -} \ No newline at end of file +} + +message BoolRequest { + bool value = 1; +} + +message BoolResponse { + bool value = 1; +} From a7e15b7f6315e44dfc2e849d57a5a972c471d60f Mon Sep 17 00:00:00 2001 From: Ruslan Sennov Date: Thu, 23 Nov 2023 15:43:31 +0300 Subject: [PATCH 2/2] fix CHECKSTYLE --- .../com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java b/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java index 3057f161..ba60d714 100644 --- a/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java +++ b/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java @@ -253,7 +253,7 @@ public List unaryRequestMethods() { * Template class for proto RPC objects. */ private class MethodContext { - // CHECKSTYLE DISABLE VisibilityModifier FOR 10 LINES + // CHECKSTYLE DISABLE VisibilityModifier FOR 11 LINES public String originalName; public String methodName; public String inputType;