Skip to content

Commit

Permalink
Feat: Implement DIREGAPIC LRO annotations (#832)
Browse files Browse the repository at this point in the history
* fix ServiceStub Goldens

* fix Stub golden

* fix Stub  golden

* fix CallableFactory golden

* fix java format

* add annotation placement comments

* only add machinery to methods that return operation

* add grpc file that contained method that was edited on abstract class

* update HttpJsonComplianceStub.golden

* java format

* add initial methods from annotations

* add initial methods from annotations

* Implement annotations

* java format

* fix package for Status

* fix CallableFactory generics

* java format

* initialize new fields in message for parser test

* set default value for operation_polling_method

* add brackets to if statement

* remove comments and invoke methods

* add brackets to if statements and remove invoke methods

* java formet
  • Loading branch information
GabrielGonzalezDiaz authored Sep 3, 2021
1 parent 00d94ed commit d7b29e0
Show file tree
Hide file tree
Showing 16 changed files with 532 additions and 163 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jvm_maven_import_external(
# gapic-generator-java dependencies to match the order in googleapis repository,
# which in its turn, prioritizes actual generated clients runtime dependencies
# over the generator dependencies.

_gax_java_version = "2.3.0"

http_archive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicClass.Kind;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.Method;
import com.google.api.generator.gapic.model.Service;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -68,6 +69,13 @@ public GapicClass generate(GapicContext context, Service service) {
GapicClass.Kind kind = Kind.STUB;
String pakkage = String.format("%s.stub", service.pakkage());

String operationService = "";
for(Method method : service.methods()) {
if(method.operationService() != null) {
operationService = method.operationService();
}
}

StubCommentComposer commentComposer =
new StubCommentComposer(getTransportContext().transportName());
ClassDefinition classDef =
Expand All @@ -79,7 +87,7 @@ public GapicClass generate(GapicContext context, Service service) {
.setAnnotations(createClassAnnotations(service, typeStore))
.setImplementsTypes(createClassImplements(typeStore))
.setName(className)
.setMethods(createClassMethods(typeStore))
.setMethods(createClassMethods(typeStore, operationService))
.setScope(ScopeNode.PUBLIC)
.build();
return GapicClass.create(kind, classDef);
Expand Down Expand Up @@ -112,12 +120,12 @@ protected List<AnnotationNode> createClassAnnotations(Service service, TypeStore
*/
protected abstract List<TypeNode> createClassImplements(TypeStore typeStore);

protected List<MethodDefinition> createClassMethods(TypeStore typeStore) {
protected List<MethodDefinition> createClassMethods(TypeStore typeStore, String operationService) {
return Arrays.asList(
createUnaryCallableMethod(typeStore),
createPagedCallableMethod(typeStore),
createBatchingCallableMethod(typeStore),
createOperationCallableMethod(typeStore));
createOperationCallableMethod(typeStore, operationService));
}

protected MethodDefinition createUnaryCallableMethod(TypeStore typeStore) {
Expand Down Expand Up @@ -182,7 +190,7 @@ protected MethodDefinition createBatchingCallableMethod(TypeStore typeStore) {
.collect(Collectors.toList()));
}

protected abstract MethodDefinition createOperationCallableMethod(TypeStore typeStore);
protected abstract MethodDefinition createOperationCallableMethod(TypeStore typeStore, String operationService);

protected MethodDefinition createGenericCallableMethod(
TypeStore typeStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.longrunning.Operation;
import java.io.IOException;
Expand Down Expand Up @@ -152,6 +153,12 @@ public GapicClass generate(GapicContext context, Service service) {
.setType(getTransportContext().transportOperationsStubType())
.build()));
}

boolean operationPollingMethod = checkOperationPollingMethod(service);
if(operationPollingMethod) {
declareLongRunningClient(classMemberVarExprs);
}

classMemberVarExprs.put(
CALLABLE_FACTORY_MEMBER_NAME,
VariableExpr.withVariable(
Expand Down Expand Up @@ -383,7 +390,7 @@ protected List<MethodDefinition> createClassMethods(
createOperationsStubGetterMethod(classMemberVarExprs.get(OPERATIONS_STUB_MEMBER_NAME)));
javaMethods.addAll(createCallableGetterMethods(callableClassMemberVarExprs));
javaMethods.addAll(
createStubOverrideMethods(classMemberVarExprs.get(BACKGROUND_RESOURCES_MEMBER_NAME)));
createStubOverrideMethods(classMemberVarExprs.get(BACKGROUND_RESOURCES_MEMBER_NAME), service));
return javaMethods;
}

Expand Down Expand Up @@ -625,12 +632,16 @@ protected List<MethodDefinition> createConstructorMethods(
secondCtorExprs.clear();
secondCtorStatements.add(EMPTY_LINE_STATEMENT);


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

// Instantiate backgroundResources.
MethodInvocationExpr getBackgroundResourcesMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(clientContextVarExpr)
.setMethodName("getBackgroundResources")
.build();

VariableExpr backgroundResourcesVarExpr = classMemberVarExprs.get("backgroundResources");
secondCtorExprs.add(
AssignmentExpr.builder()
Expand All @@ -655,6 +666,14 @@ protected List<MethodDefinition> createConstructorMethods(
return Arrays.asList(firstCtor, secondCtor);
}

protected List<Statement> createLongRunningClient(Service service, TypeStore typeStore) {
return ImmutableList.of();
}

protected void declareLongRunningClient(Map<String, VariableExpr> classMemberVarExprs) {

}

private static Expr createCallableInitExpr(
String callableVarName,
VariableExpr callableVarExpr,
Expand Down Expand Up @@ -762,7 +781,7 @@ private static List<MethodDefinition> createCallableGetterMethods(
}

private List<MethodDefinition> createStubOverrideMethods(
VariableExpr backgroundResourcesVarExpr) {
VariableExpr backgroundResourcesVarExpr, Service service) {
Function<String, MethodDefinition.Builder> methodMakerStarterFn =
methodName ->
MethodDefinition.builder()
Expand Down Expand Up @@ -826,6 +845,11 @@ private List<MethodDefinition> createStubOverrideMethods(
.build())
.build();
List<MethodDefinition> javaMethods = new ArrayList<>();
//TODO: check for operation polling method
boolean operationPollingMethod = checkOperationPollingMethod(service);
if (operationPollingMethod) {
getterLongRunningClient(javaMethods);
}
javaMethods.add(
methodMakerStarterFn
.apply("close")
Expand Down Expand Up @@ -898,6 +922,19 @@ private List<MethodDefinition> createStubOverrideMethods(
return javaMethods;
}

private boolean checkOperationPollingMethod(Service service) {
for(Method method : service.methods()) {
if(method.isOperationPollingMethod()) {
return true;
}
}
return false;
}

protected void getterLongRunningClient(List<MethodDefinition> javaMethods) {

}

private TypeStore createDynamicTypes(Service service, String stubPakkage) {
TypeStore typeStore = new TypeStore();
typeStore.putAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ protected List<TypeNode> createClassImplements(TypeStore typeStore) {
return Arrays.asList(getTransportContext().stubCallableFactoryType());
}

protected List<MethodDefinition> createClassMethods(TypeStore typeStore) {
List<MethodDefinition> classMethods = new ArrayList<>(super.createClassMethods(typeStore));
protected List<MethodDefinition> createClassMethods(
TypeStore typeStore, String operationService) {
List<MethodDefinition> classMethods =
new ArrayList<>(super.createClassMethods(typeStore, operationService));
classMethods.addAll(
Arrays.asList(
createBidiStreamingCallableMethod(typeStore),
Expand Down Expand Up @@ -98,7 +100,8 @@ protected MethodDefinition createPagedCallableMethod(TypeStore typeStore) {
}

@Override
protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
protected MethodDefinition createOperationCallableMethod(
TypeStore typeStore, String operationService) {
String methodVariantName = "Operation";
String requestTemplateName = "RequestT";
String responseTemplateName = "ResponseT";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.google.api.generator.gapic.composer.rest;

import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.httpjson.ApiMessage;
import com.google.api.gax.httpjson.HttpJsonCallableFactory;
import com.google.api.gax.httpjson.HttpJsonOperationSnapshotCallable;
import com.google.api.gax.rpc.OperationCallable;
Expand Down Expand Up @@ -47,7 +46,7 @@ public class HttpJsonServiceCallableFactoryClassComposer
new HttpJsonServiceCallableFactoryClassComposer();

private static final TypeNode MESSAGE_TYPE =
TypeNode.withReference(ConcreteReference.withClazz(ApiMessage.class));
TypeNode.withReference(ConcreteReference.withClazz(Operation.class));
private static final TypeNode BACKGROUND_RESOURCE_TYPE =
TypeNode.withReference(ConcreteReference.withClazz(BackgroundResource.class));

Expand Down Expand Up @@ -86,7 +85,8 @@ protected List<TypeNode> createClassImplements(TypeStore typeStore) {
}

@Override
protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
protected MethodDefinition createOperationCallableMethod(
TypeStore typeStore, String operationService) {
String methodVariantName = "Operation";
String requestTemplateName = "RequestT";
String responseTemplateName = "ResponseT";
Expand All @@ -104,6 +104,7 @@ protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
+ " future.");

// Generate generic method without the body
// TODO: change static usages to vapor references
MethodDefinition method =
createGenericCallableMethod(
typeStore,
Expand All @@ -119,13 +120,25 @@ protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
.collect(Collectors.toList()),
Arrays.asList(betaAnnotation));

// if (operationService.equals("")) {
// return method.toBuilder().setReturnExpr(ValueExpr.createNullExpr()).build();
// }

List<Statement> createOperationCallableBody = new ArrayList<Statement>(2);

List<VariableExpr> arguments = method.arguments();
List<VariableExpr> arguments = new ArrayList<>(method.arguments());
// Variable stubVar = arguments.get(3).variable();
// stubVar = Variable.builder()
// .setName(stubVar.identifier().name())
// .setType(typeStore.get(operationService+"Stub"))
// .build();
// arguments.set(3,VariableExpr.withVariable(stubVar));
// method = method.toBuilder().setArguments(arguments).build();

Variable httpJsonCallSettingsVar = arguments.get(0).variable();
Variable callSettingsVar = arguments.get(1).variable();
Variable operationCallSettingsVar = arguments.get(1).variable();
Variable clientContextVar = arguments.get(2).variable();
Variable operationsStub = arguments.get(3).variable();
Variable operationsStubVar = arguments.get(3).variable();
// Generate innerCallable
VariableExpr innerCallableVarExpr =
VariableExpr.builder()
Expand All @@ -139,7 +152,7 @@ protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
.build();
MethodInvocationExpr getInitialCallSettingsExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(VariableExpr.withVariable(callSettingsVar))
.setExprReferenceExpr(VariableExpr.withVariable(operationCallSettingsVar))
.setMethodName("getInitialCallSettings")
.build();
MethodInvocationExpr createBaseUnaryCallableExpr =
Expand All @@ -160,16 +173,30 @@ protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
.build();
createOperationCallableBody.add(ExprStatement.withExpr(innerCallableAssignExpr));

// This is a temporary solution
VaporReference requestT =
VaporReference.builder()
.setName("RequestT")
.setPakkage("com.google.cloud.compute.v1.stub")
.build();

TypeNode initialCallableType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(HttpJsonOperationSnapshotCallable.class)
.setGenerics(requestT, ConcreteReference.withClazz(Operation.class))
.build());

// Generate initialCallable
VariableExpr initialCallableVarExpr =
VariableExpr.builder()
.setVariable(
Variable.builder()
.setName("initialCallable")
.setType(
TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class)))
initialCallableType) // TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class)))
.build())
.setTemplateObjects(Arrays.asList(requestTemplateName, methodVariantName))
// .setTemplateObjects(Arrays.asList(requestTemplateName, "OperationSnapshot"))
.build();
MethodInvocationExpr getMethodDescriptorExpr =
MethodInvocationExpr.builder()
Expand All @@ -181,12 +208,7 @@ protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
.setExprReferenceExpr(getMethodDescriptorExpr)
.setMethodName("getOperationSnapshotFactory")
.build();
// This is a temporary solution
VaporReference requestT =
VaporReference.builder()
.setName("RequestT")
.setPakkage("com.google.cloud.compute.v1.stub")
.build();

TypeNode operationSnapshotCallableType =
TypeNode.withReference(
ConcreteReference.builder()
Expand All @@ -209,7 +231,7 @@ protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
// Generate return statement
MethodInvocationExpr longRunningClient =
MethodInvocationExpr.builder()
.setExprReferenceExpr(VariableExpr.withVariable(operationsStub))
.setExprReferenceExpr(VariableExpr.withVariable(operationsStubVar))
.setMethodName("longRunningClient")
.build();
MethodInvocationExpr createOperationCallable =
Expand All @@ -218,7 +240,7 @@ protected MethodDefinition createOperationCallableMethod(TypeStore typeStore) {
TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class)))
.setMethodName("createOperationCallable")
.setArguments(
VariableExpr.withVariable(callSettingsVar),
VariableExpr.withVariable(operationCallSettingsVar),
VariableExpr.withVariable(clientContextVar),
longRunningClient,
initialCallableVarExpr)
Expand Down
Loading

0 comments on commit d7b29e0

Please sign in to comment.