Skip to content

Commit

Permalink
feat: Add REST AIP-151 LRO suport (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
vam-google committed Sep 21, 2021
1 parent 8ae8e6f commit c4c9dfa
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 181 deletions.
Expand Up @@ -17,6 +17,7 @@
import com.google.api.core.BetaApi;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.core.BackgroundResourceAggregation;
import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.ClientStreamingCallable;
Expand Down Expand Up @@ -119,6 +120,7 @@ private static TypeStore createStaticTypes() {
IOException.class,
Operation.class,
OperationCallable.class,
OperationSnapshot.class,
RequestParamsExtractor.class,
ServerStreamingCallable.class,
TimeUnit.class,
Expand Down Expand Up @@ -161,7 +163,7 @@ public GapicClass generate(GapicContext context, Service service) {
}

boolean operationPollingMethod = checkOperationPollingMethod(service);
if(operationPollingMethod) {
if (operationPollingMethod) {
VariableExpr longRunningVarExpr = declareLongRunningClient();
if (longRunningVarExpr != null) {
classMemberVarExprs.put("longRunningClient", longRunningVarExpr);
Expand Down Expand Up @@ -212,7 +214,10 @@ public GapicClass generate(GapicContext context, Service service) {
}

protected abstract Statement createMethodDescriptorVariableDecl(
Service service, Method protoMethod, VariableExpr methodDescriptorVarExpr, Map<String, Message> messageTypes);
Service service,
Method protoMethod,
VariableExpr methodDescriptorVarExpr,
Map<String, Message> messageTypes);

protected boolean generateOperationsStubLogic(Service service) {
return true;
Expand All @@ -227,7 +232,8 @@ protected List<MethodDefinition> createOperationsStubGetterMethod(
String methodName =
String.format(
"get%s",
JavaStyle.toUpperCamelCase(getTransportContext().transportOperationsStubNames().get(0)));
JavaStyle.toUpperCamelCase(
getTransportContext().transportOperationsStubNames().get(0)));

return Arrays.asList(
MethodDefinition.builder()
Expand All @@ -248,15 +254,26 @@ protected List<MethodDefinition> createGetMethodDescriptorsMethod(
return Arrays.asList();
}

protected List<Statement> createTypeRegistry(Service service) {
return Arrays.asList();
}

protected List<Statement> createClassStatements(
Service service,
Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs,
Map<String, VariableExpr> callableClassMemberVarExprs,
Map<String, VariableExpr> classMemberVarExprs,
Map<String, Message> messageTypes) {
List<Statement> classStatements = new ArrayList<>();

classStatements.addAll(createTypeRegistry(service));
if (!classStatements.isEmpty()) {
classStatements.add(EMPTY_LINE_STATEMENT);
}

for (Statement statement :
createMethodDescriptorVariableDecls(service, protoMethodNameToDescriptorVarExprs, messageTypes)) {
createMethodDescriptorVariableDecls(
service, protoMethodNameToDescriptorVarExprs, messageTypes)) {
classStatements.add(statement);
classStatements.add(EMPTY_LINE_STATEMENT);
}
Expand All @@ -265,11 +282,15 @@ protected List<Statement> createClassStatements(
classStatements.add(EMPTY_LINE_STATEMENT);

classStatements.addAll(createClassMemberFieldDeclarations(classMemberVarExprs));
classStatements.add(EMPTY_LINE_STATEMENT);

return classStatements;
}

protected List<Statement> createMethodDescriptorVariableDecls(
Service service, Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs, Map<String, Message> messageTypes) {
Service service,
Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs,
Map<String, Message> messageTypes) {
return service.methods().stream()
.map(
m ->
Expand Down Expand Up @@ -418,14 +439,17 @@ protected List<MethodDefinition> createClassMethods(
createGetMethodDescriptorsMethod(service, typeStore, protoMethodNameToDescriptorVarExprs));
javaMethods.addAll(
createOperationsStubGetterMethod(
service, classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0))));
service,
classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0))));
javaMethods.addAll(createCallableGetterMethods(callableClassMemberVarExprs));
javaMethods.addAll(
createStubOverrideMethods(classMemberVarExprs.get(BACKGROUND_RESOURCES_MEMBER_NAME), service));
createStubOverrideMethods(
classMemberVarExprs.get(BACKGROUND_RESOURCES_MEMBER_NAME), service));
return javaMethods;
}

protected List<MethodDefinition> createStaticCreatorMethods(Service service, TypeStore typeStore, String newBuilderMethod) {
protected List<MethodDefinition> createStaticCreatorMethods(
Service service, TypeStore typeStore, String newBuilderMethod) {
TypeNode creatorMethodReturnType =
typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service));
Function<List<VariableExpr>, MethodDefinition.Builder> creatorMethodStarterFn =
Expand Down Expand Up @@ -584,22 +608,16 @@ protected List<MethodDefinition> createConstructorMethods(
.build())
.setValueExpr(callableFactoryVarExpr)
.build());
VariableExpr operationsStubClassVarExpr = classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0));
//TODO: refactor this
VariableExpr operationsStubClassVarExpr =
classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0));
// TODO: refactor this
if (generateOperationsStubLogic(service)) {
TypeNode opeationsStubType = getTransportOperationsStubType(service);
secondCtorExprs.add(
AssignmentExpr.builder()
.setVariableExpr(
operationsStubClassVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build())
.setValueExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(opeationsStubType)
.setMethodName("create")
.setArguments(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr))
.setReturnType(operationsStubClassVarExpr.type())
.build())
.build());
secondCtorExprs.addAll(createOperationsStubInitExpr(
service,
thisExpr,
operationsStubClassVarExpr,
clientContextVarExpr,
callableFactoryVarExpr));
}
secondCtorStatements.addAll(
secondCtorExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
Expand Down Expand Up @@ -665,7 +683,6 @@ protected List<MethodDefinition> createConstructorMethods(
secondCtorExprs.clear();
secondCtorStatements.add(EMPTY_LINE_STATEMENT);


secondCtorStatements.addAll(createLongRunningClient(service, typeStore));

// Instantiate backgroundResources.
Expand Down Expand Up @@ -699,6 +716,27 @@ protected List<MethodDefinition> createConstructorMethods(
return Arrays.asList(firstCtor, secondCtor);
}

protected List<Expr> createOperationsStubInitExpr(
Service service,
Expr thisExpr,
VariableExpr operationsStubClassVarExpr,
VariableExpr clientContextVarExpr,
VariableExpr callableFactoryVarExpr) {
TypeNode opeationsStubType = getTransportOperationsStubType(service);
return Collections.singletonList(
AssignmentExpr.builder()
.setVariableExpr(
operationsStubClassVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build())
.setValueExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(opeationsStubType)
.setMethodName("create")
.setArguments(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr))
.setReturnType(operationsStubClassVarExpr.type())
.build())
.build());
}

protected List<Statement> createLongRunningClient(Service service, TypeStore typeStore) {
return ImmutableList.of();
}
Expand Down Expand Up @@ -963,8 +1001,8 @@ private List<MethodDefinition> createStubOverrideMethods(
}

private boolean checkOperationPollingMethod(Service service) {
for(Method method : service.methods()) {
if(method.isOperationPollingMethod()) {
for (Method method : service.methods()) {
if (method.isOperationPollingMethod()) {
return true;
}
}
Expand Down Expand Up @@ -1063,15 +1101,15 @@ protected TypeNode getTransportOperationsStubType(Service service) {
TypeNode transportOpeationsStubType = service.operationServiceStubType();
if (transportOpeationsStubType == null) {
transportOpeationsStubType = getTransportContext().transportOperationsStubTypes().get(0);
}
else {
transportOpeationsStubType = TypeNode.withReference(
VaporReference.builder()
.setName("HttpJson" + transportOpeationsStubType.reference().simpleName())
.setPakkage(transportOpeationsStubType.reference().pakkage())
.build());
} else {
transportOpeationsStubType =
TypeNode.withReference(
VaporReference.builder()
.setName("HttpJson" + transportOpeationsStubType.reference().simpleName())
.setPakkage(transportOpeationsStubType.reference().pakkage())
.build());
}

return transportOpeationsStubType;
}
}
}
Expand Up @@ -14,7 +14,6 @@

package com.google.api.generator.gapic.composer.grpcrest;

import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.grpc.ProtoOperationTransformers;
Expand Down Expand Up @@ -72,7 +71,8 @@ public abstract class GrpcRestContext extends TransportContext {
.setTransportCallableFactoryType(null)
.setOperationsStubTypes(
ImmutableList.of(
classToType(OperationsStub.class), classToType(BackgroundResource.class)))
classToType(OperationsStub.class),
classToType(com.google.api.gax.httpjson.longrunning.stub.OperationsStub.class)))
.setTransportCallSettingsName(null)
// For RetrySettingsComposer
// TODO: fix when LRO for REST RE FIXED
Expand Down
Expand Up @@ -27,14 +27,13 @@
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.gapic.composer.common.AbstractServiceCallableFactoryClassComposer;
import com.google.api.generator.gapic.composer.store.TypeStore;
import com.google.api.generator.gapic.model.Service;
import com.google.common.collect.ImmutableList;
import com.google.longrunning.Operation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -46,7 +45,7 @@ public class HttpJsonServiceCallableFactoryClassComposer
new HttpJsonServiceCallableFactoryClassComposer();

private static final TypeNode DEFAULT_OPERATION_TYPE =
TypeNode.withReference(ConcreteReference.withClazz(Object.class));
TypeNode.withReference(ConcreteReference.withClazz(Operation.class));

private HttpJsonServiceCallableFactoryClassComposer() {
super(RestContext.instance());
Expand Down Expand Up @@ -129,15 +128,6 @@ protected MethodDefinition createOperationCallableMethod(Service service, TypeSt
Arrays.asList(betaAnnotation));

List<Statement> createOperationCallableBody = new ArrayList<>();
if (service.operationServiceStubType() == null) {
// It is an Operation polling service, it cannot contain LRO methods
return method
.toBuilder()
.setBody(ImmutableList.of())
.setReturnExpr(ValueExpr.createNullExpr())
.build();
}

List<VariableExpr> arguments = new ArrayList<>(method.arguments());

Variable httpJsonCallSettingsVar = arguments.get(0).variable();
Expand Down

0 comments on commit c4c9dfa

Please sign in to comment.