Skip to content

Commit

Permalink
add pagedCallable getter in serviceStub (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhenliu-gg5 committed Sep 11, 2020
1 parent 52c3eb7 commit b16cba7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
public class ServiceStubClassComposer implements ClassComposer {
private static final ServiceStubClassComposer INSTANCE = new ServiceStubClassComposer();
private static final String DOT = ".";
private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse";

private ServiceStubClassComposer() {}

Expand Down Expand Up @@ -110,22 +111,30 @@ private static List<MethodDefinition> createCallableGetters(
if (method.hasLro()) {
javaMethods.add(createOperationCallableGetter(method, types));
}
if (method.isPaged()) {
javaMethods.add(createPagedCallableGetter(method, types));
}
javaMethods.add(createCallableGetter(method, types));
}
return javaMethods;
}

private static MethodDefinition createOperationCallableGetter(
Method method, Map<String, TypeNode> types) {
return createCallableGetterHelper(method, types, true);
return createCallableGetterHelper(method, types, true, false);
}

private static MethodDefinition createPagedCallableGetter(
Method method, Map<String, TypeNode> types) {
return createCallableGetterHelper(method, types, false, true);
}

private static MethodDefinition createCallableGetter(Method method, Map<String, TypeNode> types) {
return createCallableGetterHelper(method, types, false);
return createCallableGetterHelper(method, types, false, false);
}

private static MethodDefinition createCallableGetterHelper(
Method method, Map<String, TypeNode> types, boolean isLroCallable) {
Method method, Map<String, TypeNode> types, boolean isLroCallable, boolean isPaged) {
TypeNode returnType;
switch (method.stream()) {
case CLIENT:
Expand All @@ -146,12 +155,16 @@ private static MethodDefinition createCallableGetterHelper(
String methodName =
String.format(
"%s%sCallable",
JavaStyle.toLowerCamelCase(method.name()), (isLroCallable ? "Operation" : ""));
JavaStyle.toLowerCamelCase(method.name()),
(isLroCallable ? "Operation" : isPaged ? "Paged" : ""));
List<Reference> genericRefs = new ArrayList<>();
genericRefs.add(method.inputType().reference());
if (method.hasLro()) {
genericRefs.add(method.lro().responseType().reference());
genericRefs.add(method.lro().metadataType().reference());
} else if (isPaged) {
genericRefs.add(
types.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name())).reference());
} else {
genericRefs.add(method.outputType().reference());
}
Expand Down Expand Up @@ -237,6 +250,22 @@ private static Map<String, TypeNode> createTypes(
.setName("OperationsStub")
.setPakkage("com.google.longrunning.stub")
.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 @@ -249,4 +278,8 @@ private static List<Statement> createThrowUOEBody(
.setMessageExpr(String.format("Not implemented: %s()", methodName))
.build()));
}

private static String getClientClassName(String serviceName) {
return String.format("%sClient", serviceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public void generateServiceClasses() {
// TODO(miraleung): Update this when a file-diffing test mechanism is in place.
private static final String EXPECTED_CLASS_STRING =
"package com.google.showcase.v1beta1.stub;\n"
+ "\n"
+ "import static com.google.showcase.v1beta1.EchoClient.PagedExpandPagedResponse;\n"
+ "\n"
+ "import com.google.api.gax.core.BackgroundResource;\n"
+ "import com.google.api.gax.rpc.BidiStreamingCallable;\n"
Expand Down Expand Up @@ -118,6 +120,12 @@ public void generateServiceClasses() {
+ " chatAgainCallable()\");\n"
+ " }\n"
+ "\n"
+ " public UnaryCallable<PagedExpandRequest, PagedExpandPagedResponse> pagedExpandPagedCallable()"
+ " {\n"
+ " throw new UnsupportedOperationException(\"Not implemented:"
+ " pagedExpandPagedCallable()\");\n"
+ " }\n"
+ "\n"
+ " public UnaryCallable<PagedExpandRequest, PagedExpandResponse> pagedExpandCallable()"
+ " {\n"
+ " throw new UnsupportedOperationException(\"Not implemented:"
Expand Down

0 comments on commit b16cba7

Please sign in to comment.