forked from googleapis/sdk-platform-java
/
HttpJsonServiceCallableFactoryClassComposer.java
253 lines (231 loc) · 10.9 KB
/
HttpJsonServiceCallableFactoryClassComposer.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
// Copyright 2021 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.rest;
import com.google.api.gax.httpjson.HttpJsonCallableFactory;
import com.google.api.gax.httpjson.HttpJsonOperationSnapshotCallable;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
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.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.longrunning.Operation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class HttpJsonServiceCallableFactoryClassComposer
extends AbstractServiceCallableFactoryClassComposer {
private static final HttpJsonServiceCallableFactoryClassComposer INSTANCE =
new HttpJsonServiceCallableFactoryClassComposer();
private static final TypeNode DEFAULT_OPERATION_TYPE =
TypeNode.withReference(ConcreteReference.withClazz(Operation.class));
private HttpJsonServiceCallableFactoryClassComposer() {
super(RestContext.instance());
}
public static HttpJsonServiceCallableFactoryClassComposer instance() {
return INSTANCE;
}
@Override
protected List<AnnotationNode> createClassAnnotations(Service service, TypeStore typeStore) {
List<AnnotationNode> annotations = super.createClassAnnotations(service, typeStore);
// Always add @BetaApi annotation to the generated CallableFactory for now. It is a public class
// for technical reasons, end users are not expected to interact with it, but it may change
// when we add LRO support, that is why making it @BetaApi for now.
//
// Remove the @BetaApi annotation once the LRO feature is fully implemented and stabilized.
if (annotations.stream().noneMatch(a -> a.type().equals(typeStore.get("BetaApi")))) {
annotations.add(AnnotationNode.withType(typeStore.get("BetaApi")));
}
return annotations;
}
@Override
protected List<TypeNode> createClassImplements(TypeStore typeStore, Service service) {
TypeNode operationsStubType = getOperationsStubType(service);
TypeNode operationType = service.operationType();
if (operationType == null) {
operationType = DEFAULT_OPERATION_TYPE;
}
return Arrays.asList(
TypeNode.withReference(
getTransportContext()
.stubCallableFactoryType()
.reference()
.copyAndSetGenerics(
Arrays.asList(operationType.reference(), operationsStubType.reference()))));
}
@Override
protected MethodDefinition createOperationCallableMethod(Service service, TypeStore typeStore) {
String methodVariantName = "Operation";
String requestTemplateName = "RequestT";
String responseTemplateName = "ResponseT";
List<String> methodTemplateNames =
Arrays.asList(requestTemplateName, responseTemplateName, "MetadataT");
// Always add @BetaApi annotation to the generated createOperationCallable() method for now,
// until LRO is fully implemented.
//
// Remove the @BetaApi annotation once the LRO feature is fully implemented and stabilized.
AnnotationNode betaAnnotation =
AnnotationNode.withTypeAndDescription(
typeStore.get("BetaApi"),
"The surface for long-running operations is not stable yet and may change in the"
+ " future.");
// Generate generic method without the body
TypeNode operationType = service.operationType();
if (operationType == null) {
operationType = DEFAULT_OPERATION_TYPE;
}
MethodDefinition method =
createGenericCallableMethod(
service,
typeStore,
/*methodTemplateNames=*/ methodTemplateNames,
/*returnCallableKindName=*/ methodVariantName,
/*returnCallableTemplateNames=*/ methodTemplateNames,
/*methodVariantName=*/ methodVariantName,
/*httpJsonCallSettingsTemplateObjects=*/ Arrays.asList(
requestTemplateName, operationType),
/*callSettingsVariantName=*/ methodVariantName,
/*callSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()),
Arrays.asList(betaAnnotation));
List<Statement> createOperationCallableBody = new ArrayList<>();
List<VariableExpr> arguments = new ArrayList<>(method.arguments());
Variable httpJsonCallSettingsVar = arguments.get(0).variable();
Variable operationCallSettingsVar = arguments.get(1).variable();
Variable clientContextVar = arguments.get(2).variable();
Variable operationsStubVar = arguments.get(3).variable();
// Generate innerCallable
VariableExpr innerCallableVarExpr =
VariableExpr.builder()
.setVariable(
Variable.builder()
.setName("innerCallable")
.setType(
TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class)))
.build())
.setTemplateObjects(Arrays.asList(requestTemplateName, methodVariantName))
.build();
MethodInvocationExpr getInitialCallSettingsExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(VariableExpr.withVariable(operationCallSettingsVar))
.setMethodName("getInitialCallSettings")
.build();
MethodInvocationExpr createBaseUnaryCallableExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(
TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class)))
.setMethodName("createBaseUnaryCallable")
.setArguments(
VariableExpr.withVariable(httpJsonCallSettingsVar),
getInitialCallSettingsExpr,
VariableExpr.withVariable(clientContextVar))
.setReturnType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class)))
.build();
AssignmentExpr innerCallableAssignExpr =
AssignmentExpr.builder()
.setVariableExpr(innerCallableVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(createBaseUnaryCallableExpr)
.build();
createOperationCallableBody.add(ExprStatement.withExpr(innerCallableAssignExpr));
// This is a temporary solution
VaporReference requestT =
VaporReference.builder()
.setName("RequestT")
.setPakkage(service.pakkage() + ".stub")
.build();
TypeNode initialCallableType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(HttpJsonOperationSnapshotCallable.class)
.setGenerics(requestT, operationType.reference())
.build());
// Generate initialCallable
VariableExpr initialCallableVarExpr =
VariableExpr.builder()
.setVariable(
Variable.builder()
.setName("initialCallable")
.setType(
initialCallableType) // TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class)))
.build())
// .setTemplateObjects(Arrays.asList(requestTemplateName, "OperationSnapshot"))
.build();
MethodInvocationExpr getMethodDescriptorExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(VariableExpr.withVariable(httpJsonCallSettingsVar))
.setMethodName("getMethodDescriptor")
.build();
MethodInvocationExpr getOperationSnapshotFactoryExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(getMethodDescriptorExpr)
.setMethodName("getOperationSnapshotFactory")
.build();
TypeNode operationSnapshotCallableType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(HttpJsonOperationSnapshotCallable.class)
.setGenerics(requestT, operationType.reference())
.build());
NewObjectExpr initialCallableObject =
NewObjectExpr.builder()
.setType(operationSnapshotCallableType)
.setIsGeneric(true)
.setArguments(innerCallableVarExpr, getOperationSnapshotFactoryExpr)
.build();
AssignmentExpr initialCallableAssignExpr =
AssignmentExpr.builder()
.setVariableExpr(initialCallableVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(initialCallableObject)
.build();
createOperationCallableBody.add(ExprStatement.withExpr(initialCallableAssignExpr));
// Generate return statement
MethodInvocationExpr longRunningClient =
MethodInvocationExpr.builder()
.setExprReferenceExpr(VariableExpr.withVariable(operationsStubVar))
.setMethodName("longRunningClient")
.build();
MethodInvocationExpr createOperationCallable =
MethodInvocationExpr.builder()
.setStaticReferenceType(
TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class)))
.setMethodName("createOperationCallable")
.setArguments(
VariableExpr.withVariable(operationCallSettingsVar),
VariableExpr.withVariable(clientContextVar),
longRunningClient,
initialCallableVarExpr)
.setReturnType(
TypeNode.withReference(ConcreteReference.withClazz(OperationCallable.class)))
.build();
// Add body and return statement to method
return method
.toBuilder()
.setBody(createOperationCallableBody)
.setReturnExpr(createOperationCallable)
.build();
}
}