Skip to content

Commit

Permalink
fix: fix diregapic-lro logic (#834)
Browse files Browse the repository at this point in the history
There are 2 more things left:
1) longRunnignOperation() getter method on the transport-agnostic (parent) class
2) proper (transport-specific) Response and Metadata transformers for OperationSettings initialization
  • Loading branch information
vam-google authored Sep 6, 2021
1 parent d7b29e0 commit 957f69a
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
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 All @@ -63,19 +62,13 @@ protected TransportContext getTransportContext() {

@Override
public GapicClass generate(GapicContext context, Service service) {
TypeStore typeStore = createTypes();
TypeStore typeStore = createTypes(service);

String className =
getTransportContext().classNames().getTransportServiceCallableFactoryClassName(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 @@ -85,9 +78,9 @@ public GapicClass generate(GapicContext context, Service service) {
commentComposer.createTransportServiceCallableFactoryClassHeaderComments(
service.name(), service.isDeprecated()))
.setAnnotations(createClassAnnotations(service, typeStore))
.setImplementsTypes(createClassImplements(typeStore))
.setImplementsTypes(createClassImplements(typeStore, service))
.setName(className)
.setMethods(createClassMethods(typeStore, operationService))
.setMethods(createClassMethods(service, typeStore))
.setScope(ScopeNode.PUBLIC)
.build();
return GapicClass.create(kind, classDef);
Expand Down Expand Up @@ -118,22 +111,23 @@ protected List<AnnotationNode> createClassAnnotations(Service service, TypeStore
* @return {@code TypeNode} containing the interface to be implemented by the generated callable
* factory class.
*/
protected abstract List<TypeNode> createClassImplements(TypeStore typeStore);
protected abstract List<TypeNode> createClassImplements(TypeStore typeStore, Service service);

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

protected MethodDefinition createUnaryCallableMethod(TypeStore typeStore) {
protected MethodDefinition createUnaryCallableMethod(Service service, TypeStore typeStore) {
String methodVariantName = "Unary";
String requestTemplateName = "RequestT";
String responseTemplateName = "ResponseT";
List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName);
return createGenericCallableMethod(
service,
typeStore,
/*methodTemplateNames=*/ methodTemplateNames,
/*returnCallableKindName=*/ methodVariantName,
Expand All @@ -148,14 +142,15 @@ protected MethodDefinition createUnaryCallableMethod(TypeStore typeStore) {
.collect(Collectors.toList()));
}

protected MethodDefinition createPagedCallableMethod(TypeStore typeStore) {
protected MethodDefinition createPagedCallableMethod(Service service, TypeStore typeStore) {
String methodVariantName = "Paged";
String requestTemplateName = "RequestT";
String pagedResponseTemplateName = "PagedListResponseT";
String responseTemplateName = "ResponseT";
List<String> methodTemplateNames =
Arrays.asList(requestTemplateName, responseTemplateName, pagedResponseTemplateName);
return createGenericCallableMethod(
service,
typeStore,
/*methodTemplateNames=*/ methodTemplateNames,
/*returnCallableKindName=*/ "Unary",
Expand All @@ -170,12 +165,13 @@ protected MethodDefinition createPagedCallableMethod(TypeStore typeStore) {
.collect(Collectors.toList()));
}

protected MethodDefinition createBatchingCallableMethod(TypeStore typeStore) {
protected MethodDefinition createBatchingCallableMethod(Service service, TypeStore typeStore) {
String methodVariantName = "Batching";
String requestTemplateName = "RequestT";
String responseTemplateName = "ResponseT";
List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName);
return createGenericCallableMethod(
service,
typeStore,
/*methodTemplateNames=*/ methodTemplateNames,
/*returnCallableKindName=*/ "Unary",
Expand All @@ -190,9 +186,11 @@ protected MethodDefinition createBatchingCallableMethod(TypeStore typeStore) {
.collect(Collectors.toList()));
}

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

protected MethodDefinition createGenericCallableMethod(
Service service,
TypeStore typeStore,
List<String> methodTemplateNames,
String returnCallableKindName,
Expand All @@ -202,6 +200,7 @@ protected MethodDefinition createGenericCallableMethod(
String callSettingsVariantName,
List<Object> callSettingsTemplateObjects) {
return createGenericCallableMethod(
service,
typeStore,
methodTemplateNames,
returnCallableKindName,
Expand All @@ -214,6 +213,7 @@ protected MethodDefinition createGenericCallableMethod(
}

protected MethodDefinition createGenericCallableMethod(
Service service,
TypeStore typeStore,
List<String> methodTemplateNames,
String returnCallableKindName,
Expand Down Expand Up @@ -265,7 +265,7 @@ protected MethodDefinition createGenericCallableMethod(
.setVariable(
Variable.builder()
.setName("operationsStub")
.setType(getTransportContext().operationsStubType())
.setType(getOperationsStubType(service))
.build())
.setIsDecl(true)
.build());
Expand Down Expand Up @@ -296,7 +296,16 @@ protected MethodDefinition createGenericCallableMethod(
.build();
}

private static TypeStore createTypes() {
protected TypeNode getOperationsStubType(Service service) {
TypeNode opeationsStubType = service.operationServiceStubType();
if (opeationsStubType == null) {
opeationsStubType = getTransportContext().operationsStubType();
}
return opeationsStubType;
}


private TypeStore createTypes(Service service) {
List<Class> concreteClazzes =
Arrays.asList(
// Gax-java classes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.api.generator.engine.ast.TryCatchStatement;
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.comment.StubCommentComposer;
Expand All @@ -63,6 +64,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -144,19 +146,25 @@ public GapicClass generate(GapicContext context, Service service) {
.setName(BACKGROUND_RESOURCES_MEMBER_NAME)
.setType(FIXED_TYPESTORE.get("BackgroundResource"))
.build()));
if (getTransportContext().transportOperationsStubType() != null) {


TypeNode opeationsStubType = getTransportOperationsStubType(service);
if (opeationsStubType != null) {
classMemberVarExprs.put(
OPERATIONS_STUB_MEMBER_NAME,
VariableExpr.withVariable(
Variable.builder()
.setName(OPERATIONS_STUB_MEMBER_NAME)
.setType(getTransportContext().transportOperationsStubType())
.setType(opeationsStubType)
.build()));
}

boolean operationPollingMethod = checkOperationPollingMethod(service);
if(operationPollingMethod) {
declareLongRunningClient(classMemberVarExprs);
VariableExpr longRunningVarExpr = declareLongRunningClient();
if (longRunningVarExpr != null) {
classMemberVarExprs.put("longRunningClient", longRunningVarExpr);
}
}

classMemberVarExprs.put(
Expand Down Expand Up @@ -554,14 +562,16 @@ protected List<MethodDefinition> createConstructorMethods(
.setValueExpr(callableFactoryVarExpr)
.build());
VariableExpr operationsStubClassVarExpr = classMemberVarExprs.get(OPERATIONS_STUB_MEMBER_NAME);
if (getTransportContext().transportOperationsStubType() != null) {

TypeNode opeationsStubType = getTransportOperationsStubType(service);
if (opeationsStubType != null) {
secondCtorExprs.add(
AssignmentExpr.builder()
.setVariableExpr(
operationsStubClassVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build())
.setValueExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(getTransportContext().transportOperationsStubType())
.setStaticReferenceType(opeationsStubType)
.setMethodName("create")
.setArguments(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr))
.setReturnType(operationsStubClassVarExpr.type())
Expand Down Expand Up @@ -670,8 +680,8 @@ protected List<Statement> createLongRunningClient(Service service, TypeStore typ
return ImmutableList.of();
}

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

protected VariableExpr declareLongRunningClient() {
return null;
}

private static Expr createCallableInitExpr(
Expand Down Expand Up @@ -845,10 +855,8 @@ 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);
if (service.operationPollingMethod() != null) {
javaMethods.addAll(createLongRunningClientGetter());
}
javaMethods.add(
methodMakerStarterFn
Expand Down Expand Up @@ -931,8 +939,8 @@ private boolean checkOperationPollingMethod(Service service) {
return false;
}

protected void getterLongRunningClient(List<MethodDefinition> javaMethods) {

protected List<MethodDefinition> createLongRunningClientGetter() {
return Collections.emptyList();
}

private TypeStore createDynamicTypes(Service service, String stubPakkage) {
Expand Down Expand Up @@ -1002,4 +1010,20 @@ protected String getProtoRpcFullMethodName(Service protoService, Method protoMet
return String.format(
"%s.%s/%s", protoService.protoPakkage(), protoService.name(), protoMethod.name());
}

protected TypeNode getTransportOperationsStubType(Service service) {
TypeNode transportOpeationsStubType = service.operationServiceStubType();
if (transportOpeationsStubType == null) {
transportOpeationsStubType = getTransportContext().transportOperationsStubType();
}
else {
transportOpeationsStubType = TypeNode.withReference(
VaporReference.builder()
.setName("HttpJson" + transportOpeationsStubType.reference().simpleName())
.setPakkage(transportOpeationsStubType.reference().pakkage())
.build());
}

return transportOpeationsStubType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public GapicClass generate(GapicContext context, Service service) {
String className = ClassNames.getServiceClientClassName(service);
GapicClass.Kind kind = Kind.MAIN;
String pakkage = service.pakkage();
boolean hasLroClient = hasLroMethods(service);
boolean hasLroClient = exposeOperationsClient(service);

Map<String, List<String>> grpcRpcsToJavaMethodNames = new HashMap<>();

Expand Down Expand Up @@ -216,9 +216,9 @@ private static List<MethodDefinition> createClassMethods(
return methods;
}

private static boolean hasLroMethods(Service service) {
private static boolean exposeOperationsClient(Service service) {
for (Method method : service.methods()) {
if (method.hasLro()) {
if (method.hasLro() && method.lro().operationServiceStubType() == null) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ private static List<MethodDefinition> createClassMethods(
boolean hasLroClient = hasLroMethods(service);
List<MethodDefinition> methods = new ArrayList<>();
if (hasLroClient) {
methods.add(createOperationsStubGetter(typeStore));
TypeNode operationsStubType = service.operationServiceStubType();
if (operationsStubType == null) {
operationsStubType = typeStore.get("OperationsStub");
}
methods.add(createOperationsStubGetter(typeStore, operationsStubType));
}
methods.addAll(createCallableGetters(service, messageTypes, typeStore));
methods.addAll(createBackgroundResourceMethodOverrides());
Expand Down Expand Up @@ -203,11 +207,11 @@ private static MethodDefinition createCallableGetterHelper(
.build();
}

private static MethodDefinition createOperationsStubGetter(TypeStore typeStore) {
private static MethodDefinition createOperationsStubGetter(TypeStore typeStore, TypeNode operationsStubType) {
String methodName = "getOperationsStub";
return MethodDefinition.builder()
.setScope(ScopeNode.PUBLIC)
.setReturnType(typeStore.get("OperationsStub"))
.setReturnType(operationsStubType)
.setName(methodName)
.setBody(createThrowUOEBody(methodName, typeStore))
.build();
Expand Down
Loading

0 comments on commit 957f69a

Please sign in to comment.