Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mgodave committed Nov 19, 2024
1 parent cc93b8a commit 7ed1fe5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions servicetalk-examples/grpc/protoc-options/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ protobuf {
option 'typeNameSuffix=St'
// Option to tell the compiler to exclude all Deprecated fields, types, and methods from the output
option 'skipDeprecated=true'
// Option to generate default throwing service definitions on the service interfaces. This will allow
// teams to evolve their codebases and not break dependent libraries.
option 'defaultServiceMethods=true'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static io.servicetalk.grpc.protoc.Generator.NewRpcMethodFlag.SERVER_RESPONSE;
import static io.servicetalk.grpc.protoc.NoopServiceCommentsMap.NOOP_MAP;
import static io.servicetalk.grpc.protoc.StringUtils.escapeJavaDoc;
import static io.servicetalk.grpc.protoc.StringUtils.isNullOrEmpty;
import static io.servicetalk.grpc.protoc.StringUtils.sanitizeIdentifier;
import static io.servicetalk.grpc.protoc.Types.AllGrpcRoutes;
import static io.servicetalk.grpc.protoc.Types.Arrays;
Expand Down Expand Up @@ -90,6 +91,8 @@
import static io.servicetalk.grpc.protoc.Types.GrpcService;
import static io.servicetalk.grpc.protoc.Types.GrpcServiceContext;
import static io.servicetalk.grpc.protoc.Types.GrpcServiceFactory;
import static io.servicetalk.grpc.protoc.Types.GrpcStatus;
import static io.servicetalk.grpc.protoc.Types.GrpcStatusCode;
import static io.servicetalk.grpc.protoc.Types.GrpcStatusException;
import static io.servicetalk.grpc.protoc.Types.GrpcSupportedCodings;
import static io.servicetalk.grpc.protoc.Types.Identity;
Expand Down Expand Up @@ -1644,21 +1647,31 @@ private TypeSpec newServiceInterfaceSpec(final State state, final boolean blocki

// generate default service methods
if (defaultServiceMethods) {
for (int i = 0; i < state.serviceProto.getMethodList().size(); i++) {
final MethodDescriptorProto methodProto = state.serviceProto.getMethodList().get(i);
final List<MethodDescriptorProto> methodList = state.serviceProto.getMethodList();
for (int i = 0; i < methodList.size(); i++) {
final MethodDescriptorProto methodProto = methodList.get(i);
final ClassName inClass = messageTypesMap.get(methodProto.getInputType());
final ClassName outClass = messageTypesMap.get(methodProto.getOutputType());
final String methodName = sanitizeIdentifier(methodProto.getName(), true);
final String serviceName = isNullOrEmpty(state.serviceProto.getName()) ?
"" : state.serviceProto.getName();
final String fullMethodName = serviceName + "/" + methodName;
final int methodIndex = i;
interfaceSpecBuilder.addMethod(newRpcMethodSpec(inClass, outClass, methodName,
methodProto.getClientStreaming(),
methodProto.getServerStreaming(),
!blocking ? EnumSet.of(INTERFACE) : EnumSet.of(INTERFACE, BLOCKING),
printJavaDocs, (__, c) -> {
c.addModifiers(DEFAULT)
.addParameter(GrpcServiceContext, ctx)
.addStatement("throw new UnsupportedOperationException(\"Method " + methodName +
" is unimplemented\")");
final String errorMessage = "\"Method " + fullMethodName + " is unimplemented\"";
c.addModifiers(DEFAULT).addParameter(GrpcServiceContext, ctx);
if (!blocking) {
final ClassName returnType = methodProto.getServerStreaming() ? Publisher : Single;
c.addStatement("return $T.failed(new $T(new $T($T.UNIMPLEMENTED, $L)))", returnType,
GrpcStatusException, GrpcStatus, GrpcStatusCode, errorMessage);
} else {
c.addStatement("throw new $T(new $T($T.UNIMPLEMENTED, $L))",
GrpcStatusException, GrpcStatus, GrpcStatusCode, errorMessage);
}
if (printJavaDocs) {
extractJavaDocComments(state, methodIndex, c);
c.addJavadoc(JAVADOC_PARAM + ctx +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private static CodeGeneratorResponse generate(final CodeGeneratorRequest request
final String typeSuffixValue = optionsMap.get(TYPE_NAME_SUFFIX_OPTION);
final boolean printJavaDocs = parseBoolean(optionsMap.getOrDefault(PRINT_JAVA_DOCS_OPTION, "true"));
final boolean skipDeprecated = parseBoolean(optionsMap.getOrDefault(SKIP_DEPRECATED_CODE, "false"));
// FIXME: 0.43 - consider changing default value to true
final boolean defaultServiceMethods = parseBoolean(optionsMap.getOrDefault(DEFAULT_SERVICE_METHODS, "false"));

final List<FileDescriptor> fileDescriptors = request.getProtoFileList().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ final class Types {
static final ClassName GrpcExecutionContext = ClassName.get(grpcApiPkg, "GrpcExecutionContext");
static final ClassName GrpcExecutionStrategy = ClassName.get(grpcApiPkg, "GrpcExecutionStrategy");
static final ClassName GrpcStatusException = ClassName.get(grpcApiPkg, "GrpcStatusException");
static final ClassName GrpcStatus = ClassName.get(grpcApiPkg, "GrpcStatus");
static final ClassName GrpcStatusCode = ClassName.get(grpcApiPkg, "GrpcStatusCode");
static final ClassName Identity = ClassName.get(encodingApiPkg, "Identity");
static final ClassName BufferDecoderGroup = ClassName.get(encodingApiPkg, "BufferDecoderGroup");
static final ClassName EmptyBufferDecoderGroup = ClassName.get(encodingApiPkg, "EmptyBufferDecoderGroup");
Expand Down

0 comments on commit 7ed1fe5

Please sign in to comment.