Skip to content

Commit 957f69a

Browse files
authored
fix: fix diregapic-lro logic (googleapis#834)
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
1 parent d7b29e0 commit 957f69a

24 files changed

Lines changed: 562 additions & 218 deletions

src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceCallableFactoryClassComposer.java

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import com.google.api.generator.gapic.model.GapicClass;
4242
import com.google.api.generator.gapic.model.GapicClass.Kind;
4343
import com.google.api.generator.gapic.model.GapicContext;
44-
import com.google.api.generator.gapic.model.Method;
4544
import com.google.api.generator.gapic.model.Service;
4645
import java.util.ArrayList;
4746
import java.util.Arrays;
@@ -63,19 +62,13 @@ protected TransportContext getTransportContext() {
6362

6463
@Override
6564
public GapicClass generate(GapicContext context, Service service) {
66-
TypeStore typeStore = createTypes();
65+
TypeStore typeStore = createTypes(service);
66+
6767
String className =
6868
getTransportContext().classNames().getTransportServiceCallableFactoryClassName(service);
6969
GapicClass.Kind kind = Kind.STUB;
7070
String pakkage = String.format("%s.stub", service.pakkage());
7171

72-
String operationService = "";
73-
for(Method method : service.methods()) {
74-
if(method.operationService() != null) {
75-
operationService = method.operationService();
76-
}
77-
}
78-
7972
StubCommentComposer commentComposer =
8073
new StubCommentComposer(getTransportContext().transportName());
8174
ClassDefinition classDef =
@@ -85,9 +78,9 @@ public GapicClass generate(GapicContext context, Service service) {
8578
commentComposer.createTransportServiceCallableFactoryClassHeaderComments(
8679
service.name(), service.isDeprecated()))
8780
.setAnnotations(createClassAnnotations(service, typeStore))
88-
.setImplementsTypes(createClassImplements(typeStore))
81+
.setImplementsTypes(createClassImplements(typeStore, service))
8982
.setName(className)
90-
.setMethods(createClassMethods(typeStore, operationService))
83+
.setMethods(createClassMethods(service, typeStore))
9184
.setScope(ScopeNode.PUBLIC)
9285
.build();
9386
return GapicClass.create(kind, classDef);
@@ -118,22 +111,23 @@ protected List<AnnotationNode> createClassAnnotations(Service service, TypeStore
118111
* @return {@code TypeNode} containing the interface to be implemented by the generated callable
119112
* factory class.
120113
*/
121-
protected abstract List<TypeNode> createClassImplements(TypeStore typeStore);
114+
protected abstract List<TypeNode> createClassImplements(TypeStore typeStore, Service service);
122115

123-
protected List<MethodDefinition> createClassMethods(TypeStore typeStore, String operationService) {
116+
protected List<MethodDefinition> createClassMethods(Service service, TypeStore typeStore) {
124117
return Arrays.asList(
125-
createUnaryCallableMethod(typeStore),
126-
createPagedCallableMethod(typeStore),
127-
createBatchingCallableMethod(typeStore),
128-
createOperationCallableMethod(typeStore, operationService));
118+
createUnaryCallableMethod(service, typeStore),
119+
createPagedCallableMethod(service, typeStore),
120+
createBatchingCallableMethod(service, typeStore),
121+
createOperationCallableMethod(service, typeStore));
129122
}
130123

131-
protected MethodDefinition createUnaryCallableMethod(TypeStore typeStore) {
124+
protected MethodDefinition createUnaryCallableMethod(Service service, TypeStore typeStore) {
132125
String methodVariantName = "Unary";
133126
String requestTemplateName = "RequestT";
134127
String responseTemplateName = "ResponseT";
135128
List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName);
136129
return createGenericCallableMethod(
130+
service,
137131
typeStore,
138132
/*methodTemplateNames=*/ methodTemplateNames,
139133
/*returnCallableKindName=*/ methodVariantName,
@@ -148,14 +142,15 @@ protected MethodDefinition createUnaryCallableMethod(TypeStore typeStore) {
148142
.collect(Collectors.toList()));
149143
}
150144

151-
protected MethodDefinition createPagedCallableMethod(TypeStore typeStore) {
145+
protected MethodDefinition createPagedCallableMethod(Service service, TypeStore typeStore) {
152146
String methodVariantName = "Paged";
153147
String requestTemplateName = "RequestT";
154148
String pagedResponseTemplateName = "PagedListResponseT";
155149
String responseTemplateName = "ResponseT";
156150
List<String> methodTemplateNames =
157151
Arrays.asList(requestTemplateName, responseTemplateName, pagedResponseTemplateName);
158152
return createGenericCallableMethod(
153+
service,
159154
typeStore,
160155
/*methodTemplateNames=*/ methodTemplateNames,
161156
/*returnCallableKindName=*/ "Unary",
@@ -170,12 +165,13 @@ protected MethodDefinition createPagedCallableMethod(TypeStore typeStore) {
170165
.collect(Collectors.toList()));
171166
}
172167

173-
protected MethodDefinition createBatchingCallableMethod(TypeStore typeStore) {
168+
protected MethodDefinition createBatchingCallableMethod(Service service, TypeStore typeStore) {
174169
String methodVariantName = "Batching";
175170
String requestTemplateName = "RequestT";
176171
String responseTemplateName = "ResponseT";
177172
List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName);
178173
return createGenericCallableMethod(
174+
service,
179175
typeStore,
180176
/*methodTemplateNames=*/ methodTemplateNames,
181177
/*returnCallableKindName=*/ "Unary",
@@ -190,9 +186,11 @@ protected MethodDefinition createBatchingCallableMethod(TypeStore typeStore) {
190186
.collect(Collectors.toList()));
191187
}
192188

193-
protected abstract MethodDefinition createOperationCallableMethod(TypeStore typeStore, String operationService);
189+
protected abstract MethodDefinition createOperationCallableMethod(
190+
Service service, TypeStore typeStore);
194191

195192
protected MethodDefinition createGenericCallableMethod(
193+
Service service,
196194
TypeStore typeStore,
197195
List<String> methodTemplateNames,
198196
String returnCallableKindName,
@@ -202,6 +200,7 @@ protected MethodDefinition createGenericCallableMethod(
202200
String callSettingsVariantName,
203201
List<Object> callSettingsTemplateObjects) {
204202
return createGenericCallableMethod(
203+
service,
205204
typeStore,
206205
methodTemplateNames,
207206
returnCallableKindName,
@@ -214,6 +213,7 @@ protected MethodDefinition createGenericCallableMethod(
214213
}
215214

216215
protected MethodDefinition createGenericCallableMethod(
216+
Service service,
217217
TypeStore typeStore,
218218
List<String> methodTemplateNames,
219219
String returnCallableKindName,
@@ -265,7 +265,7 @@ protected MethodDefinition createGenericCallableMethod(
265265
.setVariable(
266266
Variable.builder()
267267
.setName("operationsStub")
268-
.setType(getTransportContext().operationsStubType())
268+
.setType(getOperationsStubType(service))
269269
.build())
270270
.setIsDecl(true)
271271
.build());
@@ -296,7 +296,16 @@ protected MethodDefinition createGenericCallableMethod(
296296
.build();
297297
}
298298

299-
private static TypeStore createTypes() {
299+
protected TypeNode getOperationsStubType(Service service) {
300+
TypeNode opeationsStubType = service.operationServiceStubType();
301+
if (opeationsStubType == null) {
302+
opeationsStubType = getTransportContext().operationsStubType();
303+
}
304+
return opeationsStubType;
305+
}
306+
307+
308+
private TypeStore createTypes(Service service) {
300309
List<Class> concreteClazzes =
301310
Arrays.asList(
302311
// Gax-java classes.

src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.google.api.generator.engine.ast.TryCatchStatement;
4545
import com.google.api.generator.engine.ast.TypeNode;
4646
import com.google.api.generator.engine.ast.ValueExpr;
47+
import com.google.api.generator.engine.ast.VaporReference;
4748
import com.google.api.generator.engine.ast.Variable;
4849
import com.google.api.generator.engine.ast.VariableExpr;
4950
import com.google.api.generator.gapic.composer.comment.StubCommentComposer;
@@ -63,6 +64,7 @@
6364
import java.io.IOException;
6465
import java.util.ArrayList;
6566
import java.util.Arrays;
67+
import java.util.Collections;
6668
import java.util.LinkedHashMap;
6769
import java.util.List;
6870
import java.util.Map;
@@ -144,19 +146,25 @@ public GapicClass generate(GapicContext context, Service service) {
144146
.setName(BACKGROUND_RESOURCES_MEMBER_NAME)
145147
.setType(FIXED_TYPESTORE.get("BackgroundResource"))
146148
.build()));
147-
if (getTransportContext().transportOperationsStubType() != null) {
149+
150+
151+
TypeNode opeationsStubType = getTransportOperationsStubType(service);
152+
if (opeationsStubType != null) {
148153
classMemberVarExprs.put(
149154
OPERATIONS_STUB_MEMBER_NAME,
150155
VariableExpr.withVariable(
151156
Variable.builder()
152157
.setName(OPERATIONS_STUB_MEMBER_NAME)
153-
.setType(getTransportContext().transportOperationsStubType())
158+
.setType(opeationsStubType)
154159
.build()));
155160
}
156161

157162
boolean operationPollingMethod = checkOperationPollingMethod(service);
158163
if(operationPollingMethod) {
159-
declareLongRunningClient(classMemberVarExprs);
164+
VariableExpr longRunningVarExpr = declareLongRunningClient();
165+
if (longRunningVarExpr != null) {
166+
classMemberVarExprs.put("longRunningClient", longRunningVarExpr);
167+
}
160168
}
161169

162170
classMemberVarExprs.put(
@@ -554,14 +562,16 @@ protected List<MethodDefinition> createConstructorMethods(
554562
.setValueExpr(callableFactoryVarExpr)
555563
.build());
556564
VariableExpr operationsStubClassVarExpr = classMemberVarExprs.get(OPERATIONS_STUB_MEMBER_NAME);
557-
if (getTransportContext().transportOperationsStubType() != null) {
565+
566+
TypeNode opeationsStubType = getTransportOperationsStubType(service);
567+
if (opeationsStubType != null) {
558568
secondCtorExprs.add(
559569
AssignmentExpr.builder()
560570
.setVariableExpr(
561571
operationsStubClassVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build())
562572
.setValueExpr(
563573
MethodInvocationExpr.builder()
564-
.setStaticReferenceType(getTransportContext().transportOperationsStubType())
574+
.setStaticReferenceType(opeationsStubType)
565575
.setMethodName("create")
566576
.setArguments(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr))
567577
.setReturnType(operationsStubClassVarExpr.type())
@@ -670,8 +680,8 @@ protected List<Statement> createLongRunningClient(Service service, TypeStore typ
670680
return ImmutableList.of();
671681
}
672682

673-
protected void declareLongRunningClient(Map<String, VariableExpr> classMemberVarExprs) {
674-
683+
protected VariableExpr declareLongRunningClient() {
684+
return null;
675685
}
676686

677687
private static Expr createCallableInitExpr(
@@ -845,10 +855,8 @@ private List<MethodDefinition> createStubOverrideMethods(
845855
.build())
846856
.build();
847857
List<MethodDefinition> javaMethods = new ArrayList<>();
848-
//TODO: check for operation polling method
849-
boolean operationPollingMethod = checkOperationPollingMethod(service);
850-
if (operationPollingMethod) {
851-
getterLongRunningClient(javaMethods);
858+
if (service.operationPollingMethod() != null) {
859+
javaMethods.addAll(createLongRunningClientGetter());
852860
}
853861
javaMethods.add(
854862
methodMakerStarterFn
@@ -931,8 +939,8 @@ private boolean checkOperationPollingMethod(Service service) {
931939
return false;
932940
}
933941

934-
protected void getterLongRunningClient(List<MethodDefinition> javaMethods) {
935-
942+
protected List<MethodDefinition> createLongRunningClientGetter() {
943+
return Collections.emptyList();
936944
}
937945

938946
private TypeStore createDynamicTypes(Service service, String stubPakkage) {
@@ -1002,4 +1010,20 @@ protected String getProtoRpcFullMethodName(Service protoService, Method protoMet
10021010
return String.format(
10031011
"%s.%s/%s", protoService.protoPakkage(), protoService.name(), protoMethod.name());
10041012
}
1013+
1014+
protected TypeNode getTransportOperationsStubType(Service service) {
1015+
TypeNode transportOpeationsStubType = service.operationServiceStubType();
1016+
if (transportOpeationsStubType == null) {
1017+
transportOpeationsStubType = getTransportContext().transportOperationsStubType();
1018+
}
1019+
else {
1020+
transportOpeationsStubType = TypeNode.withReference(
1021+
VaporReference.builder()
1022+
.setName("HttpJson" + transportOpeationsStubType.reference().simpleName())
1023+
.setPakkage(transportOpeationsStubType.reference().pakkage())
1024+
.build());
1025+
}
1026+
1027+
return transportOpeationsStubType;
1028+
}
10051029
}

src/main/java/com/google/api/generator/gapic/composer/common/ServiceClientClassComposer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public GapicClass generate(GapicContext context, Service service) {
129129
String className = ClassNames.getServiceClientClassName(service);
130130
GapicClass.Kind kind = Kind.MAIN;
131131
String pakkage = service.pakkage();
132-
boolean hasLroClient = hasLroMethods(service);
132+
boolean hasLroClient = exposeOperationsClient(service);
133133

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

@@ -216,9 +216,9 @@ private static List<MethodDefinition> createClassMethods(
216216
return methods;
217217
}
218218

219-
private static boolean hasLroMethods(Service service) {
219+
private static boolean exposeOperationsClient(Service service) {
220220
for (Method method : service.methods()) {
221-
if (method.hasLro()) {
221+
if (method.hasLro() && method.lro().operationServiceStubType() == null) {
222222
return true;
223223
}
224224
}

src/main/java/com/google/api/generator/gapic/composer/common/ServiceStubClassComposer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ private static List<MethodDefinition> createClassMethods(
112112
boolean hasLroClient = hasLroMethods(service);
113113
List<MethodDefinition> methods = new ArrayList<>();
114114
if (hasLroClient) {
115-
methods.add(createOperationsStubGetter(typeStore));
115+
TypeNode operationsStubType = service.operationServiceStubType();
116+
if (operationsStubType == null) {
117+
operationsStubType = typeStore.get("OperationsStub");
118+
}
119+
methods.add(createOperationsStubGetter(typeStore, operationsStubType));
116120
}
117121
methods.addAll(createCallableGetters(service, messageTypes, typeStore));
118122
methods.addAll(createBackgroundResourceMethodOverrides());
@@ -203,11 +207,11 @@ private static MethodDefinition createCallableGetterHelper(
203207
.build();
204208
}
205209

206-
private static MethodDefinition createOperationsStubGetter(TypeStore typeStore) {
210+
private static MethodDefinition createOperationsStubGetter(TypeStore typeStore, TypeNode operationsStubType) {
207211
String methodName = "getOperationsStub";
208212
return MethodDefinition.builder()
209213
.setScope(ScopeNode.PUBLIC)
210-
.setReturnType(typeStore.get("OperationsStub"))
214+
.setReturnType(operationsStubType)
211215
.setName(methodName)
212216
.setBody(createThrowUOEBody(methodName, typeStore))
213217
.build();

0 commit comments

Comments
 (0)