Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.NullObjectValue;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.TernaryExpr;
Expand Down Expand Up @@ -73,6 +74,16 @@

public class ServiceClientClassComposer implements ClassComposer {
private static final ServiceClientClassComposer INSTANCE = new ServiceClientClassComposer();
private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse";
private static final String CALLABLE_NAME_PATTERN = "%sCallable";
private static final String PAGED_CALLABLE_NAME_PATTERN = "%sPagedCallable";
private static final String OPERATION_CALLABLE_NAME_PATTERN = "%sOperationCallable";

private enum CallableMethodKind {
REGULAR,
LRO,
PAGED,
}

private ServiceClientClassComposer() {}

Expand Down Expand Up @@ -425,6 +436,9 @@ private static List<MethodDefinition> createServiceMethods(
javaMethods.add(createLroAsyncMethod(service.name(), method, types));
javaMethods.add(createLroCallable(service.name(), method, types));
}
if (method.isPaged()) {
javaMethods.add(createPagedCallableMethod(service.name(), method, types));
}
javaMethods.add(createCallableMethod(service.name(), method, types));
}
return javaMethods;
Expand Down Expand Up @@ -553,7 +567,7 @@ private static List<MethodDefinition> createMethodVariants(

MethodInvocationExpr methodReturnExpr =
MethodInvocationExpr.builder()
.setMethodName(String.format("%sCallable", methodName))
.setMethodName(String.format(CALLABLE_NAME_PATTERN, methodName))
.build();
methodReturnExpr =
MethodInvocationExpr.builder()
Expand Down Expand Up @@ -622,18 +636,26 @@ private static MethodDefinition createLroAsyncMethod(

private static MethodDefinition createLroCallable(
String serviceName, Method method, Map<String, TypeNode> types) {
return createCallableMethod(serviceName, method, types, true);
return createCallableMethod(serviceName, method, types, CallableMethodKind.LRO);
}

private static MethodDefinition createCallableMethod(
String serviceName, Method method, Map<String, TypeNode> types) {
return createCallableMethod(serviceName, method, types, false);
return createCallableMethod(serviceName, method, types, CallableMethodKind.REGULAR);
}

private static MethodDefinition createPagedCallableMethod(
String serviceName, Method method, Map<String, TypeNode> types) {
return createCallableMethod(serviceName, method, types, CallableMethodKind.PAGED);
}

private static MethodDefinition createCallableMethod(
String serviceName, Method method, Map<String, TypeNode> types, boolean isLroCallable) {
String serviceName,
Method method,
Map<String, TypeNode> types,
CallableMethodKind callableMethodKind) {
TypeNode rawCallableReturnType = null;
if (isLroCallable) {
if (callableMethodKind.equals(CallableMethodKind.LRO)) {
rawCallableReturnType = types.get("OperationCallable");
} else {
switch (method.stream()) {
Expand All @@ -658,20 +680,10 @@ private static MethodDefinition createCallableMethod(
TypeNode.withReference(
rawCallableReturnType
.reference()
.copyAndSetGenerics(
isLroCallable
? Arrays.asList(
method.inputType().reference(),
method.lro().responseType().reference(),
method.lro().metadataType().reference())
: Arrays.asList(
method.inputType().reference(), method.outputType().reference())));
.copyAndSetGenerics(getGenericsForCallable(callableMethodKind, method, types)));

String rawMethodName = JavaStyle.toLowerCamelCase(method.name());
String methodName =
isLroCallable
? String.format("%sOperationCallabke", rawMethodName)
: String.format("%sCallable", rawMethodName);
String methodName = getCallableName(callableMethodKind, rawMethodName);
TypeNode stubType = types.get(String.format("%sStub", serviceName));
MethodInvocationExpr returnExpr =
MethodInvocationExpr.builder()
Expand Down Expand Up @@ -884,6 +896,21 @@ private static Map<String, TypeNode> createVaporTypes(Service service) {
.setName("OperationsClient")
.setPakkage("com.google.longrunning")
.build()));
// Pagination types.
types.putAll(
service.methods().stream()
.filter(m -> m.isPaged())
.collect(
Collectors.toMap(
m -> String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()),
m ->
TypeNode.withReference(
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(getClientClassName(service.name()))
.setIsStaticImport(true)
.build()))));
return types;
}

Expand All @@ -905,4 +932,34 @@ private static Map<String, TypeNode> createProtoMessageTypes(
private static Variable createVariable(String name, TypeNode type) {
return Variable.builder().setName(name).setType(type).build();
}

private static String getClientClassName(String serviceName) {
return String.format("%sClient", serviceName);
}

private static List<Reference> getGenericsForCallable(
CallableMethodKind kind, Method method, Map<String, TypeNode> types) {
if (kind.equals(CallableMethodKind.LRO)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI that switch would also work here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, was considering about switch but since there are only three conditions, probably just if is more clearer. But open to refactor this later if more condition is added.

return Arrays.asList(
method.inputType().reference(),
method.lro().responseType().reference(),
method.lro().metadataType().reference());
}
if (kind.equals(CallableMethodKind.PAGED)) {
return Arrays.asList(
method.inputType().reference(),
types.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name())).reference());
}
return Arrays.asList(method.inputType().reference(), method.outputType().reference());
}

private static String getCallableName(CallableMethodKind kind, String rawMethodName) {
if (kind.equals(CallableMethodKind.LRO)) {
return String.format(OPERATION_CALLABLE_NAME_PATTERN, rawMethodName);
}
if (kind.equals(CallableMethodKind.PAGED)) {
return String.format(PAGED_CALLABLE_NAME_PATTERN, rawMethodName);
}
return String.format(CALLABLE_NAME_PATTERN, rawMethodName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ public void generateServiceClasses() {
+ " return pagedExpandCallable().call(request);\n"
+ " }\n"
+ "\n"
+ " public final UnaryCallable<PagedExpandRequest, PagedExpandPagedResponse>\n"
+ " pagedExpandPagedCallable() {\n"
+ " return stub.pagedExpandPagedCallable();\n"
+ " }\n"
+ "\n"
+ " public final UnaryCallable<PagedExpandRequest, PagedExpandResponse>"
+ " pagedExpandCallable() {\n"
+ " return stub.pagedExpandCallable();\n"
Expand All @@ -216,8 +221,8 @@ public void generateServiceClasses() {
+ " }\n"
+ "\n"
+ " public final OperationCallable<WaitRequest, WaitResponse, WaitMetadata>"
+ " waitOperationCallabke() {\n"
+ " return stub.waitOperationCallabke();\n"
+ " waitOperationCallable() {\n"
+ " return stub.waitOperationCallable();\n"
+ " }\n"
+ "\n"
+ " public final UnaryCallable<WaitRequest, Operation> waitCallable() {\n"
Expand Down