Skip to content

Commit

Permalink
feat: Lambda-ize single-method anon classes (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung committed Aug 3, 2021
1 parent c98c0c7 commit 19b661c
Show file tree
Hide file tree
Showing 36 changed files with 889 additions and 1,711 deletions.
Expand Up @@ -126,6 +126,9 @@ public MethodInvocationExpr build() {
"generics",
String.format("method invocation of %s", methodInvocationExpr.methodIdentifier().name()));

// TODO(v2): If type-checking is ever added for arguments, beware of lambdas and their type
// workarounds.

return methodInvocationExpr;
}
}
Expand Down
Expand Up @@ -613,13 +613,13 @@ private static Expr createPagedListDescriptorAssignExpr(
.setReturnType(returnType)
.build();
thenExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(
TypeNode.withReference(ConcreteReference.withClazz(ImmutableList.class)))
.setGenerics(Arrays.asList(repeatedResponseType.reference()))
.setMethodName("of")
.setReturnType(returnType)
.build();
MethodInvocationExpr.builder()
.setStaticReferenceType(
TypeNode.withReference(ConcreteReference.withClazz(ImmutableList.class)))
.setGenerics(Arrays.asList(repeatedResponseType.reference()))
.setMethodName("of")
.setReturnType(returnType)
.build();
elseExpr = getResponsesExpr;
}
// While protobufs should not be null, this null-check is needed to protect against NPEs
Expand Down
Expand Up @@ -30,14 +30,14 @@
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.AnonymousClassExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.LambdaExpr;
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
Expand Down Expand Up @@ -1165,23 +1165,14 @@ private static ClassDefinition createNestedRpcPagedResponseClass(
.setClazz(ApiFunction.class)
.setGenerics(Arrays.asList(methodPageType.reference(), thisClassType.reference()))
.build());

// Overrides ApiFunction.apply.
// (https://github.com/googleapis/api-common-java/blob/debf25960dea0367b0d3b5e16d57d76c1d01947e/src/main/java/com/google/api/core/ApiFunction.java).
Expr pageToTransformExpr =
AnonymousClassExpr.builder()
.setType(anonClassType)
.setMethods(
Arrays.asList(
MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(thisClassType)
.setName("apply")
.setArguments(inputVarExpr.toBuilder().setIsDecl(true).build())
.setReturnExpr(
NewObjectExpr.builder()
.setType(thisClassType)
.setArguments(inputVarExpr)
.build())
.build()))
LambdaExpr.builder()
.setArguments(inputVarExpr.toBuilder().setIsDecl(true).build())
.setReturnExpr(
NewObjectExpr.builder().setType(thisClassType).setArguments(inputVarExpr).build())
.build();

// createAsync method - return expression.
Expand Down Expand Up @@ -1739,9 +1730,7 @@ private static boolean isProtoEmptyType(TypeNode type) {
private static void updateGapicMetadata(
GapicContext context, String protoPackage, String javaPackage) {
context.updateGapicMetadata(
context
.gapicMetadata()
.toBuilder()
context.gapicMetadata().toBuilder()
.setProtoPackage(protoPackage)
.setLibraryPackage(javaPackage)
.build());
Expand Down
Expand Up @@ -16,13 +16,12 @@

import com.google.api.gax.grpc.GrpcCallSettings;
import com.google.api.gax.grpc.GrpcStubCallableFactory;
import com.google.api.gax.rpc.RequestParamsExtractor;
import com.google.api.generator.engine.ast.AnonymousClassExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.LambdaExpr;
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.ScopeNode;
Expand Down Expand Up @@ -223,7 +222,7 @@ protected Expr createTransportSettingsInitExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(callSettingsBuilderExpr)
.setMethodName("setParamsExtractor")
.setArguments(createRequestParamsExtractorAnonClass(method))
.setArguments(createRequestParamsExtractorClassInstance(method))
.build();
}

Expand Down Expand Up @@ -255,7 +254,7 @@ protected String getProtoRpcFullMethodName(Service protoService, Method protoMet
return String.format("google.iam.v1.IAMPolicy/%s", protoMethod.name());
}

private AnonymousClassExpr createRequestParamsExtractorAnonClass(Method method) {
private LambdaExpr createRequestParamsExtractorClassInstance(Method method) {
Preconditions.checkState(
method.hasHttpBindings(), String.format("Method %s has no HTTP binding", method.name()));

Expand Down Expand Up @@ -339,24 +338,13 @@ private AnonymousClassExpr createRequestParamsExtractorAnonClass(Method method)
.setReturnType(returnType)
.build();

MethodDefinition extractMethod =
MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(returnType)
.setName("extract")
.setArguments(requestVarExpr.toBuilder().setIsDecl(true).build())
.setBody(
bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()))
.setReturnExpr(returnExpr)
.build();

TypeNode anonClassType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(RequestParamsExtractor.class)
.setGenerics(method.inputType().reference())
.build());
return AnonymousClassExpr.builder().setType(anonClassType).setMethods(extractMethod).build();
// Overrides extract().
// https://github.com/googleapis/gax-java/blob/8d45d186e36ae97b789a6f89d80ae5213a773b65/gax/src/main/java/com/google/api/gax/rpc/RequestParamsExtractor.java#L55
return LambdaExpr.builder()
.setArguments(requestVarExpr.toBuilder().setIsDecl(true).build())
.setBody(
bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()))
.setReturnExpr(returnExpr)
.build();
}
}
Expand Up @@ -354,7 +354,7 @@ private static MethodDefinition createGenericClientStreamingProtoMethodOverride(
AssignmentExpr.builder()
.setVariableExpr(requestObserverVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(
createStreamObserverAnonymousClassExpr(
createStreamObserverClassInstance(
protoMethod,
returnType,
responseObserverVarExpr,
Expand All @@ -364,7 +364,7 @@ private static MethodDefinition createGenericClientStreamingProtoMethodOverride(
.build();
}

private static AnonymousClassExpr createStreamObserverAnonymousClassExpr(
private static AnonymousClassExpr createStreamObserverClassInstance(
Method protoMethod,
TypeNode classType,
VariableExpr responseObserverVarExpr,
Expand Down
Expand Up @@ -24,13 +24,13 @@
import com.google.api.gax.httpjson.ProtoMessageResponseParser;
import com.google.api.gax.httpjson.ProtoRestSerializer;
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.AnonymousClassExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.IfStatement;
import com.google.api.generator.engine.ast.LambdaExpr;
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
Expand Down Expand Up @@ -277,7 +277,7 @@ private List<Expr> getRequestFormatterExpr(Method protoMethod) {
ValueExpr.withValue(
StringObjectValue.withValue(
protoMethod.httpBindings().patternLowerCamel())),
createFieldsExtractorAnonClass(
createFieldsExtractorClassInstance(
protoMethod,
extractorVarType,
protoMethod.httpBindings().pathParameters(),
Expand All @@ -303,7 +303,7 @@ private List<Expr> getRequestFormatterExpr(Method protoMethod) {
.apply(
"setQueryParamsExtractor",
Arrays.asList(
createFieldsExtractorAnonClass(
createFieldsExtractorClassInstance(
protoMethod,
extractorVarType,
protoMethod.httpBindings().queryParameters(),
Expand All @@ -316,7 +316,7 @@ private List<Expr> getRequestFormatterExpr(Method protoMethod) {
.apply(
"setRequestBodyExtractor",
Arrays.asList(
createFieldsExtractorAnonClass(
createFieldsExtractorClassInstance(
protoMethod,
extractorVarType,
protoMethod.httpBindings().bodyParameters(),
Expand Down Expand Up @@ -356,7 +356,7 @@ private List<Expr> setResponseParserExpr(Method protoMethod) {
return Collections.singletonList(expr);
}

private Expr createFieldsExtractorAnonClass(
private Expr createFieldsExtractorClassInstance(
Method method,
TypeNode extractorReturnType,
Set<HttpBinding> httpBindingFieldNames,
Expand Down Expand Up @@ -495,25 +495,13 @@ private Expr createFieldsExtractorAnonClass(
}
}

MethodDefinition extractMethod =
MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(extractorReturnType)
.setName("extract")
.setArguments(requestVarExpr.toBuilder().setIsDecl(true).build())
.setBody(bodyStatements)
.setReturnExpr(returnExpr)
.build();

TypeNode anonClassType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(FieldsExtractor.class)
.setGenerics(method.inputType().reference(), extractorReturnType.reference())
.build());

return AnonymousClassExpr.builder().setType(anonClassType).setMethods(extractMethod).build();
// Overrides FieldsExtractor
// (https://github.com/googleapis/gax-java/blob/12b18ee255d3fabe13bb3969df40753b29f830d5/gax-httpjson/src/main/java/com/google/api/gax/httpjson/FieldsExtractor.java).
return LambdaExpr.builder()
.setArguments(requestVarExpr.toBuilder().setIsDecl(true).build())
.setBody(bodyStatements)
.setReturnExpr(returnExpr)
.build();
}

private List<Expr> getHttpMethodTypeExpr(Method protoMethod) {
Expand Down
@@ -1,6 +1,5 @@
package com.google.showcase.v1beta1;

import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.BetaApi;
Expand Down Expand Up @@ -860,14 +859,7 @@ public class EchoClient implements BackgroundResource {
ApiFuture<PagedExpandPage> futurePage =
PagedExpandPage.createEmptyPage().createPageAsync(context, futureResponse);
return ApiFutures.transform(
futurePage,
new ApiFunction<PagedExpandPage, PagedExpandPagedResponse>() {
@Override
public PagedExpandPagedResponse apply(PagedExpandPage input) {
return new PagedExpandPagedResponse(input);
}
},
MoreExecutors.directExecutor());
futurePage, input -> new PagedExpandPagedResponse(input), MoreExecutors.directExecutor());
}

private PagedExpandPagedResponse(PagedExpandPage page) {
Expand Down Expand Up @@ -941,12 +933,7 @@ public class EchoClient implements BackgroundResource {
SimplePagedExpandPage.createEmptyPage().createPageAsync(context, futureResponse);
return ApiFutures.transform(
futurePage,
new ApiFunction<SimplePagedExpandPage, SimplePagedExpandPagedResponse>() {
@Override
public SimplePagedExpandPagedResponse apply(SimplePagedExpandPage input) {
return new SimplePagedExpandPagedResponse(input);
}
},
input -> new SimplePagedExpandPagedResponse(input),
MoreExecutors.directExecutor());
}

Expand Down
@@ -1,6 +1,5 @@
package com.google.showcase.v1beta1;

import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.BetaApi;
Expand Down Expand Up @@ -644,14 +643,7 @@ public class IdentityClient implements BackgroundResource {
ApiFuture<ListUsersPage> futurePage =
ListUsersPage.createEmptyPage().createPageAsync(context, futureResponse);
return ApiFutures.transform(
futurePage,
new ApiFunction<ListUsersPage, ListUsersPagedResponse>() {
@Override
public ListUsersPagedResponse apply(ListUsersPage input) {
return new ListUsersPagedResponse(input);
}
},
MoreExecutors.directExecutor());
futurePage, input -> new ListUsersPagedResponse(input), MoreExecutors.directExecutor());
}

private ListUsersPagedResponse(ListUsersPage page) {
Expand Down

0 comments on commit 19b661c

Please sign in to comment.