-
Notifications
You must be signed in to change notification settings - Fork 48
/
AbstractServiceCallableFactoryClassComposer.java
329 lines (303 loc) · 13.1 KB
/
AbstractServiceCallableFactoryClassComposer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.api.generator.gapic.composer.common;
import com.google.api.core.BetaApi;
import com.google.api.gax.rpc.BatchingCallSettings;
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.api.gax.rpc.OperationCallSettings;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.PagedCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.StreamingCallSettings;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.TypeNode;
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;
import com.google.api.generator.gapic.composer.store.TypeStore;
import com.google.api.generator.gapic.composer.utils.PackageChecker;
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.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Generated;
public abstract class AbstractServiceCallableFactoryClassComposer implements ClassComposer {
private final TransportContext transportContext;
protected AbstractServiceCallableFactoryClassComposer(TransportContext transportContext) {
this.transportContext = transportContext;
}
protected TransportContext getTransportContext() {
return transportContext;
}
@Override
public GapicClass generate(GapicContext context, Service service) {
TypeStore typeStore = createTypes(service);
String className =
getTransportContext().classNames().getTransportServiceCallableFactoryClassName(service);
GapicClass.Kind kind = Kind.STUB;
String pakkage = String.format("%s.stub", service.pakkage());
StubCommentComposer commentComposer =
new StubCommentComposer(getTransportContext().transportName());
ClassDefinition classDef =
ClassDefinition.builder()
.setPackageString(pakkage)
.setHeaderCommentStatements(
commentComposer.createTransportServiceCallableFactoryClassHeaderComments(
service.name(), service.isDeprecated()))
.setAnnotations(createClassAnnotations(service, typeStore))
.setImplementsTypes(createClassImplements(typeStore, service))
.setName(className)
.setMethods(createClassMethods(service, typeStore))
.setScope(ScopeNode.PUBLIC)
.build();
return GapicClass.create(kind, classDef);
}
protected List<AnnotationNode> createClassAnnotations(Service service, TypeStore typeStore) {
List<AnnotationNode> annotations = new ArrayList<>();
if (!PackageChecker.isGaApi(service.pakkage())) {
annotations.add(AnnotationNode.withType(typeStore.get("BetaApi")));
}
if (service.isDeprecated()) {
annotations.add(AnnotationNode.withType(TypeNode.DEPRECATED));
}
annotations.add(
AnnotationNode.builder()
.setType(typeStore.get("Generated"))
.setDescription("by gapic-generator-java")
.build());
return annotations;
}
/**
* Construct the type to be implemented by the generated callable factory.
*
* @param typeStore type store with common types
* @return {@code TypeNode} containing the interface to be implemented by the generated callable
* factory class.
*/
protected abstract List<TypeNode> createClassImplements(TypeStore typeStore, Service service);
protected List<MethodDefinition> createClassMethods(Service service, TypeStore typeStore) {
return Arrays.asList(
createUnaryCallableMethod(service, typeStore),
createPagedCallableMethod(service, typeStore),
createBatchingCallableMethod(service, typeStore),
createOperationCallableMethod(service, 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,
/*returnCallableTemplateNames=*/ methodTemplateNames,
/*methodVariantName=*/ methodVariantName,
/*transportCallSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()),
/*callSettingsVariantName=*/ methodVariantName,
/*callSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()));
}
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",
/*returnCallableTemplateNames=*/ Arrays.asList(
requestTemplateName, pagedResponseTemplateName),
/*methodVariantName=*/ methodVariantName,
/*transportCallSettingsTemplateObjects=*/ Arrays.asList(
requestTemplateName, responseTemplateName),
/*callSettingsVariantName=*/ methodVariantName,
/*callSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()));
}
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",
/*returnCallableTemplateNames=*/ methodTemplateNames,
/*methodVariantName=*/ methodVariantName,
/*transportCallSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()),
/*callSettingsVariantName=*/ methodVariantName,
/*callSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()));
}
protected abstract MethodDefinition createOperationCallableMethod(
Service service, TypeStore typeStore);
protected MethodDefinition createGenericCallableMethod(
Service service,
TypeStore typeStore,
List<String> methodTemplateNames,
String returnCallableKindName,
List<String> returnCallableTemplateNames,
String methodVariantName,
List<Object> transportCallSettingsTemplateObjects,
String callSettingsVariantName,
List<Object> callSettingsTemplateObjects) {
return createGenericCallableMethod(
service,
typeStore,
methodTemplateNames,
returnCallableKindName,
returnCallableTemplateNames,
methodVariantName,
transportCallSettingsTemplateObjects,
callSettingsVariantName,
callSettingsTemplateObjects,
Collections.emptyList());
}
protected MethodDefinition createGenericCallableMethod(
Service service,
TypeStore typeStore,
List<String> methodTemplateNames,
String returnCallableKindName,
List<String> returnCallableTemplateNames,
String methodVariantName,
List<Object> transportCallSettingsTemplateObjects,
String callSettingsVariantName,
List<Object> callSettingsTemplateObjects,
List<AnnotationNode> annotations) {
String methodName = String.format("create%sCallable", methodVariantName);
String callSettingsTypeName = String.format("%sCallSettings", callSettingsVariantName);
String returnTypeName = String.format("%sCallable", returnCallableKindName);
boolean isOperationCallable = methodVariantName.equals("Operation");
List<VariableExpr> arguments = new ArrayList<>();
arguments.add(
VariableExpr.builder()
.setVariable(
Variable.builder()
.setName(getTransportContext().transportCallSettingsName())
.setType(getTransportContext().transportCallSettingsType())
.build())
.setIsDecl(true)
.setTemplateObjects(transportCallSettingsTemplateObjects)
.build());
arguments.add(
VariableExpr.builder()
.setVariable(
Variable.builder()
.setName("callSettings")
.setType(typeStore.get(callSettingsTypeName))
.build())
.setIsDecl(true)
.setTemplateObjects(callSettingsTemplateObjects)
.build());
arguments.add(
VariableExpr.builder()
.setVariable(
Variable.builder()
.setName("clientContext")
.setType(typeStore.get("ClientContext"))
.build())
.setIsDecl(true)
.build());
if (isOperationCallable) {
arguments.add(
VariableExpr.builder()
.setVariable(
Variable.builder()
.setName("operationsStub")
.setType(getOperationsStubType(service))
.build())
.setIsDecl(true)
.build());
}
TypeNode returnType = typeStore.get(returnTypeName);
MethodInvocationExpr returnExpr =
MethodInvocationExpr.builder()
.setMethodName(methodName)
.setStaticReferenceType(getTransportContext().transportCallableFactoryType())
.setArguments(
arguments.stream()
.map(v -> v.toBuilder().setIsDecl(false).build())
.collect(Collectors.toList()))
.setReturnType(returnType)
.build();
return MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setTemplateNames(methodTemplateNames)
.setReturnType(returnType)
.setReturnTemplateNames(returnCallableTemplateNames)
.setName(methodName)
.setArguments(arguments)
.setReturnExpr(returnExpr)
.setAnnotations(annotations)
.build();
}
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.
BatchingCallSettings.class,
BetaApi.class,
BidiStreamingCallable.class,
ClientContext.class,
ClientStreamingCallable.class,
OperationCallSettings.class,
OperationCallable.class,
PagedCallSettings.class,
ServerStreamingCallSettings.class,
ServerStreamingCallable.class,
StreamingCallSettings.class,
UnaryCallSettings.class,
UnaryCallable.class,
Generated.class,
UnsupportedOperationException.class);
return new TypeStore(concreteClazzes);
}
}