Skip to content

Commit

Permalink
[ggj] feat(gapic): propagate protobuf 'deprecated' to classes/methods (
Browse files Browse the repository at this point in the history
…#708)

* feat(gapic): propagate protobuf 'deprecated' to classes/methods

* fix method params
  • Loading branch information
miraleung authored Apr 7, 2021
1 parent ba93722 commit c9a7e11
Show file tree
Hide file tree
Showing 43 changed files with 2,807 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public enum TypeKind {
public static final TypeNode STRING = withReference(ConcreteReference.withClazz(String.class));
public static final TypeNode VOID_OBJECT = withReference(ConcreteReference.withClazz(Void.class));

public static final TypeNode DEPRECATED =
withReference(ConcreteReference.withClazz(Deprecated.class));

public static final TypeNode STRING_ARRAY =
builder()
.setTypeKind(TypeKind.OBJECT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public GapicClass generate(GapicContext ignored, Service service) {
.setPackageString(pakkage)
.setHeaderCommentStatements(
StubCommentComposer.createGrpcServiceCallableFactoryClassHeaderComments(
service.name()))
.setAnnotations(createClassAnnotations(service.pakkage(), typeStore))
service.name(), service.isDeprecated()))
.setAnnotations(createClassAnnotations(service, typeStore))
.setImplementsTypes(createClassImplements(typeStore))
.setName(className)
.setMethods(createClassMethods(typeStore))
Expand All @@ -87,11 +87,16 @@ public GapicClass generate(GapicContext ignored, Service service) {
return GapicClass.create(kind, classDef);
}

private static List<AnnotationNode> createClassAnnotations(String pakkage, TypeStore typeStore) {
private static List<AnnotationNode> createClassAnnotations(Service service, TypeStore typeStore) {
List<AnnotationNode> annotations = new ArrayList<>();
if (!PackageChecker.isGaApi(pakkage)) {
if (!PackageChecker.isGaApi(service.pakkage())) {
annotations.add(AnnotationNode.withType(typeStore.get("BetaApi")));
}

if (service.isDeprecated()) {
annotations.add(AnnotationNode.withType(TypeNode.DEPRECATED));
}

annotations.add(
AnnotationNode.builder()
.setType(typeStore.get("Generated"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ public GapicClass generate(GapicContext ignored, Service service) {
ClassDefinition.builder()
.setPackageString(pakkage)
.setHeaderCommentStatements(
StubCommentComposer.createGrpcServiceStubClassHeaderComments(service.name()))
.setAnnotations(createClassAnnotations(service.pakkage()))
StubCommentComposer.createGrpcServiceStubClassHeaderComments(
service.name(), service.isDeprecated()))
.setAnnotations(createClassAnnotations(service))
.setScope(ScopeNode.PUBLIC)
.setName(className)
.setExtendsType(typeStore.get(ClassNames.getServiceStubClassName(service)))
Expand Down Expand Up @@ -387,11 +388,16 @@ private static Map<String, VariableExpr> createCallableClassMembers(
return callableClassMembers;
}

private static List<AnnotationNode> createClassAnnotations(String pakkage) {
private static List<AnnotationNode> createClassAnnotations(Service service) {
List<AnnotationNode> annotations = new ArrayList<>();
if (!PackageChecker.isGaApi(pakkage)) {
if (!PackageChecker.isGaApi(service.pakkage())) {
annotations.add(AnnotationNode.withType(FIXED_TYPESTORE.get("BetaApi")));
}

if (service.isDeprecated()) {
annotations.add(AnnotationNode.withType(TypeNode.DEPRECATED));
}

annotations.add(
AnnotationNode.builder()
.setType(FIXED_TYPESTORE.get("Generated"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public GapicClass generate(GapicContext context, Service service) {
.setHeaderCommentStatements(
createClassHeaderComments(service, typeStore, resourceNames, messageTypes))
.setPackageString(pakkage)
.setAnnotations(createClassAnnotations(pakkage, typeStore))
.setAnnotations(createClassAnnotations(service, typeStore))
.setScope(ScopeNode.PUBLIC)
.setName(className)
.setImplementsTypes(createClassImplements(typeStore))
Expand All @@ -158,11 +158,14 @@ public GapicClass generate(GapicContext context, Service service) {
return GapicClass.create(kind, classDef);
}

private static List<AnnotationNode> createClassAnnotations(String pakkage, TypeStore typeStore) {
private static List<AnnotationNode> createClassAnnotations(Service service, TypeStore typeStore) {
List<AnnotationNode> annotations = new ArrayList<>();
if (!PackageChecker.isGaApi(pakkage)) {
if (!PackageChecker.isGaApi(service.pakkage())) {
annotations.add(AnnotationNode.withType(typeStore.get("BetaApi")));
}
if (service.isDeprecated()) {
annotations.add(AnnotationNode.withType(TypeNode.DEPRECATED));
}
annotations.add(
AnnotationNode.builder()
.setType(typeStore.get("Generated"))
Expand Down Expand Up @@ -544,6 +547,8 @@ private static List<MethodDefinition> createServiceMethods(
messageTypes,
typeStore,
resourceNames);

// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata
.get(method.name())
.addAll(
Expand All @@ -559,23 +564,31 @@ private static List<MethodDefinition> createServiceMethods(
messageTypes,
typeStore,
resourceNames);

// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
if (method.hasLro()) {
MethodDefinition generatedMethod =
createLroCallableMethod(service, method, typeStore, messageTypes, resourceNames);

// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
if (method.isPaged()) {
MethodDefinition generatedMethod =
createPagedCallableMethod(service, method, typeStore, messageTypes, resourceNames);

// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
MethodDefinition generatedMethod =
createCallableMethod(service, method, typeStore, messageTypes, resourceNames);

// Collect data for the gapic_metadata.json file.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
Expand Down Expand Up @@ -670,6 +683,12 @@ private static List<MethodDefinition> createMethodVariants(
methodVariantBuilder =
methodVariantBuilder.setReturnType(methodOutputType).setReturnExpr(rpcInvocationExpr);
}

if (method.isDeprecated()) {
methodVariantBuilder =
methodVariantBuilder.setAnnotations(
Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)));
}
methodVariantBuilder = methodVariantBuilder.setBody(statements);
javaMethods.add(methodVariantBuilder.build());
}
Expand Down Expand Up @@ -739,6 +758,11 @@ private static MethodDefinition createMethodDefaultMethod(
.setName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName))
.setArguments(Arrays.asList(requestArgVarExpr));

if (method.isDeprecated()) {
methodBuilder =
methodBuilder.setAnnotations(Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)));
}

if (isProtoEmptyType(methodOutputType)) {
methodBuilder =
methodBuilder
Expand Down Expand Up @@ -866,7 +890,14 @@ private static MethodDefinition createCallableMethod(
}
}

return MethodDefinition.builder()
MethodDefinition.Builder methodDefBuilder = MethodDefinition.builder();
if (method.isDeprecated()) {
methodDefBuilder =
methodDefBuilder.setAnnotations(
Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)));
}

return methodDefBuilder
.setHeaderCommentStatements(
ServiceClientCommentComposer.createRpcCallableMethodHeaderComment(
method, sampleCodeOpt))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -104,7 +105,7 @@ public GapicClass generate(GapicContext ignored, Service service) {
.setPackageString(pakkage)
.setHeaderCommentStatements(
createClassHeaderComments(service, typeStore.get(className)))
.setAnnotations(createClassAnnotations(service.pakkage()))
.setAnnotations(createClassAnnotations(service))
.setScope(ScopeNode.PUBLIC)
.setName(className)
.setExtendsType(
Expand Down Expand Up @@ -143,16 +144,22 @@ private static List<CommentStatement> createClassHeaderComments(
return SettingsCommentComposer.createClassHeaderComments(
ClassNames.getServiceClientClassName(service),
service.defaultHost(),
service.isDeprecated(),
methodNameOpt,
sampleCodeOpt,
classType);
}

private static List<AnnotationNode> createClassAnnotations(String pakkage) {
private static List<AnnotationNode> createClassAnnotations(Service service) {
List<AnnotationNode> annotations = new ArrayList<>();
if (!PackageChecker.isGaApi(pakkage)) {
if (!PackageChecker.isGaApi(service.pakkage())) {
annotations.add(AnnotationNode.withType(FIXED_TYPESTORE.get("BetaApi")));
}

if (service.isDeprecated()) {
annotations.add(AnnotationNode.withType(TypeNode.DEPRECATED));
}

annotations.add(
AnnotationNode.builder()
.setType(FIXED_TYPESTORE.get("Generated"))
Expand Down Expand Up @@ -194,18 +201,16 @@ private static MethodDefinition createConstructorMethod(Service service, TypeSto
.build();
}

// TODO(miraleung): Consider merging this with createNestedBuilderSettingsGetterMethods.
private static List<MethodDefinition> createSettingsGetterMethods(
Service service, TypeStore typeStore) {
TypeNode stubSettingsType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
BiFunction<TypeNode, String, MethodDefinition> methodMakerFn =
(retType, methodName) ->
BiFunction<TypeNode, String, MethodDefinition.Builder> methodMakerFn =
(retType, javaMethodName) ->
MethodDefinition.builder()
.setHeaderCommentStatements(
SettingsCommentComposer.createCallSettingsGetterComment(
getMethodNameFromSettingsVarName(methodName)))
.setScope(ScopeNode.PUBLIC)
.setReturnType(retType)
.setName(methodName)
.setName(javaMethodName)
.setReturnExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(
Expand All @@ -217,22 +222,41 @@ private static List<MethodDefinition> createSettingsGetterMethods(
.setReturnType(FIXED_TYPESTORE.get("StubSettings"))
.build())
.build())
.setMethodName(methodName)
.setMethodName(javaMethodName)
.setReturnType(retType)
.build())
.build();
.build());
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method protoMethod : service.methods()) {
String javaStyleName = JavaStyle.toLowerCamelCase(protoMethod.name());
String javaMethodName =
String.format("%sSettings", JavaStyle.toLowerCamelCase(protoMethod.name()));
MethodDefinition.Builder methodBuilder =
methodMakerFn.apply(getCallSettingsType(protoMethod, typeStore), javaMethodName);
javaMethods.add(
methodMakerFn.apply(
getCallSettingsType(protoMethod, typeStore),
String.format("%sSettings", javaStyleName)));
methodBuilder
.setHeaderCommentStatements(
SettingsCommentComposer.createCallSettingsGetterComment(
getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated()))
.setAnnotations(
protoMethod.isDeprecated()
? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED))
: Collections.emptyList())
.build());
if (protoMethod.hasLro()) {
javaMethodName = String.format("%sOperationSettings", javaStyleName);
methodBuilder =
methodMakerFn.apply(getOperationCallSettingsType(protoMethod), javaMethodName);
javaMethods.add(
methodMakerFn.apply(
getOperationCallSettingsType(protoMethod),
String.format("%sOperationSettings", javaStyleName)));
methodBuilder
.setHeaderCommentStatements(
SettingsCommentComposer.createCallSettingsGetterComment(
getMethodNameFromSettingsVarName(javaMethodName),
protoMethod.isDeprecated()))
.setAnnotations(
protoMethod.isDeprecated()
? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED))
: Collections.emptyList())
.build());
}
}
return javaMethods;
Expand Down Expand Up @@ -635,12 +659,9 @@ private static MethodDefinition createNestedBuilderApplyToAllUnaryMethod(

private static List<MethodDefinition> createNestedBuilderSettingsGetterMethods(
Service service, TypeStore typeStore) {
BiFunction<TypeNode, String, MethodDefinition> methodMakerFn =
BiFunction<TypeNode, String, MethodDefinition.Builder> methodMakerFn =
(retType, methodName) ->
MethodDefinition.builder()
.setHeaderCommentStatements(
SettingsCommentComposer.createCallSettingsBuilderGetterComment(
getMethodNameFromSettingsVarName(methodName)))
.setScope(ScopeNode.PUBLIC)
.setReturnType(retType)
.setName(methodName)
Expand All @@ -652,20 +673,39 @@ private static List<MethodDefinition> createNestedBuilderSettingsGetterMethods(
.build())
.setMethodName(methodName)
.setReturnType(retType)
.build())
.build();
.build());
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method protoMethod : service.methods()) {
String javaStyleName = JavaStyle.toLowerCamelCase(protoMethod.name());
String javaMethodName = String.format("%sSettings", javaStyleName);
MethodDefinition.Builder methodBuilder =
methodMakerFn.apply(getCallSettingsBuilderType(protoMethod, typeStore), javaMethodName);
javaMethods.add(
methodMakerFn.apply(
getCallSettingsBuilderType(protoMethod, typeStore),
String.format("%sSettings", javaStyleName)));
methodBuilder
.setHeaderCommentStatements(
SettingsCommentComposer.createCallSettingsBuilderGetterComment(
getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated()))
.setAnnotations(
protoMethod.isDeprecated()
? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED))
: Collections.emptyList())
.build());

if (protoMethod.hasLro()) {
javaMethodName = String.format("%sOperationSettings", javaStyleName);
methodBuilder =
methodMakerFn.apply(getOperationCallSettingsBuilderType(protoMethod), javaMethodName);
javaMethods.add(
methodMakerFn.apply(
getOperationCallSettingsBuilderType(protoMethod),
String.format("%sOperationSettings", javaStyleName)));
methodBuilder
.setHeaderCommentStatements(
SettingsCommentComposer.createCallSettingsBuilderGetterComment(
getMethodNameFromSettingsVarName(javaMethodName),
protoMethod.isDeprecated()))
.setAnnotations(
protoMethod.isDeprecated()
? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED))
: Collections.emptyList())
.build());
}
}
return javaMethods;
Expand Down
Loading

0 comments on commit c9a7e11

Please sign in to comment.