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

fix java keywords #325

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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<String> 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<Location> 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();
@@ -234,7 +253,8 @@ public List<MethodContext> 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;
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);
}
}
Original file line number Diff line number Diff line change
@@ -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<BoolResponse> boolean_(Single<BoolRequest> 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);
}
}
14 changes: 13 additions & 1 deletion rx-java/rxgrpc-test/src/test/proto/helloworld.proto
Original file line number Diff line number Diff line change
@@ -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;
}
}

message BoolRequest {
bool value = 1;
}

message BoolResponse {
bool value = 1;
}