Skip to content

Commit

Permalink
[ggj][composer][1/2]feat: add pagedCallable getter in serviceClient f…
Browse files Browse the repository at this point in the history
…or list method (#292)

* add pagedCallable getter

* fix

* feedback

* feedback

* enum
  • Loading branch information
xiaozhenliu-gg5 authored Sep 12, 2020
1 parent fb39e5a commit 9ffbd10
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 19 deletions.
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 @@ -440,6 +451,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 @@ -568,7 +582,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 @@ -637,18 +651,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 @@ -673,20 +695,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 @@ -899,6 +911,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 @@ -920,4 +947,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)) {
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 @@ -227,6 +227,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 @@ -238,8 +243,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

0 comments on commit 9ffbd10

Please sign in to comment.