Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][codegen] feat: add ServiceClient.MethodPage list method inner class #326

Merged
merged 27 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fd218de
feat: add protobuf comment parser util
miraleung Sep 15, 2020
8c792e3
fix: add basic proto build rules
miraleung Sep 15, 2020
d6ee068
feat: add header comments to ServiceClient
miraleung Sep 15, 2020
82aa0b6
fix: build protoc at test time
miraleung Sep 15, 2020
c8f2233
Merge branch 'gp/g0' of github.com:googleapis/gapic-generator-java in…
miraleung Sep 15, 2020
2405b1a
fix!: wrap protobuf location and process comments
miraleung Sep 15, 2020
a59fd3f
feat: add comment parsing to methods and fields
miraleung Sep 16, 2020
66dec11
fix: test
miraleung Sep 16, 2020
8325450
feat: add protobuf comments to ServiceClient
miraleung Sep 16, 2020
5acc151
fix: solidify codegen method order with TypeNode/MethodArg and Compar…
miraleung Sep 16, 2020
97e780a
fix: clean up tests
miraleung Sep 16, 2020
5508e67
fix: merge gp/g2_5
miraleung Sep 17, 2020
0b7288e
fix: merge
miraleung Sep 17, 2020
c6ca60b
fix: ServiceClient member variables and method calls
miraleung Sep 18, 2020
ac89e12
fix: ServiceStubSettings builder type
miraleung Sep 18, 2020
b5e6585
fix: ServiceSettings Builder construction
miraleung Sep 18, 2020
1e10b6b
fix: ServiceStub callable types
miraleung Sep 18, 2020
006aff4
feat: java_gapic_library rule impl
miraleung Sep 18, 2020
f25274f
fix: remove debugging comments
miraleung Sep 18, 2020
76d26d8
feat: add gradle assembly Bazel rules
miraleung Sep 18, 2020
c56271f
feat: add java_gapic_test Bazel rule
miraleung Sep 18, 2020
98ccc00
fix: use Java packages for resname codegen
miraleung Sep 18, 2020
5eed26a
fix: build resnames separately and extract into proto/ dir
miraleung Sep 18, 2020
3b238ff
fix: remove debug printf
miraleung Sep 18, 2020
1ea3899
feat: add ServiceClient.MethodPagedResponse inner class
miraleung Sep 19, 2020
798bd05
feat: add ServiceClient.MethodPage inner class
miraleung Sep 19, 2020
9b62055
Merge branch 'master' into gp/g15
miraleung Sep 19, 2020
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 @@ -20,6 +20,7 @@
import com.google.api.core.BetaApi;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.paging.AbstractPage;
import com.google.api.gax.paging.AbstractPagedListResponse;
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.ClientStreamingCallable;
Expand All @@ -43,6 +44,7 @@
import com.google.api.generator.engine.ast.ReferenceConstructorExpr;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.SuperObjectValue;
import com.google.api.generator.engine.ast.TernaryExpr;
import com.google.api.generator.engine.ast.ThisObjectValue;
import com.google.api.generator.engine.ast.TypeNode;
Expand Down Expand Up @@ -75,6 +77,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Generated;

Expand Down Expand Up @@ -892,37 +895,43 @@ private static List<ClassDefinition> createNestedPagingClasses(
if (!method.isPaged()) {
continue;
}
nestedClasses.add(createNestedRpcPagedResponseClass(method, messageTypes, types));
// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().name());
TypeNode repeatedResponseType = null;
for (Field field : methodOutputMessage.fields()) {
if (field.isRepeated() && !field.isMap()) {
Reference repeatedGenericRef = field.type().reference().generics().get(0);
repeatedResponseType = TypeNode.withReference(repeatedGenericRef);
break;
}
}

Preconditions.checkNotNull(
repeatedResponseType,
String.format(
"No repeated field found on message %s for method %s",
methodOutputMessage.name(), method.name()));

nestedClasses.add(
createNestedRpcPagedResponseClass(method, repeatedResponseType, messageTypes, types));
nestedClasses.add(
createNestedRpcPageClass(method, repeatedResponseType, messageTypes, types));
}

return nestedClasses;
}

private static ClassDefinition createNestedRpcPagedResponseClass(
Method method, Map<String, Message> messageTypes, Map<String, TypeNode> types) {
Method method,
TypeNode repeatedResponseType,
Map<String, Message> messageTypes,
Map<String, TypeNode> types) {
Preconditions.checkState(
method.isPaged(), String.format("Expected method %s to be paged", method.name()));

String className = String.format("%sPagedResponse", JavaStyle.toUpperCamelCase(method.name()));
TypeNode thisClassType = types.get(className);

// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().name());
TypeNode repeatedResponseType = null;
for (Field field : methodOutputMessage.fields()) {
if (field.isRepeated() && !field.isMap()) {
Reference repeatedGenericRef = field.type().reference().generics().get(0);
repeatedResponseType = TypeNode.withReference(repeatedGenericRef);
break;
}
}

Preconditions.checkNotNull(
repeatedResponseType,
String.format(
"No repeated field found on message %s for method %s",
methodOutputMessage.name(), method.name()));

String upperJavaMethodName = JavaStyle.toUpperCamelCase(method.name());
TypeNode methodPageType = types.get(String.format("%sPage", upperJavaMethodName));
TypeNode classExtendsType =
Expand Down Expand Up @@ -1108,6 +1117,153 @@ private static ClassDefinition createNestedRpcPagedResponseClass(
.build();
}

private static ClassDefinition createNestedRpcPageClass(
Method method,
TypeNode repeatedResponseType,
Map<String, Message> messageTypes,
Map<String, TypeNode> types) {
Preconditions.checkState(
method.isPaged(), String.format("Expected method %s to be paged", method.name()));

String upperJavaMethodName = JavaStyle.toUpperCamelCase(method.name());
String className = String.format("%sPage", upperJavaMethodName);
TypeNode classType = types.get(className);
TypeNode classExtendsType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(AbstractPage.class)
.setGenerics(
Arrays.asList(
method.inputType(),
method.outputType(),
repeatedResponseType,
classType)
.stream()
.map(t -> t.reference())
.collect(Collectors.toList()))
.build());

// Private constructor.
VariableExpr contextVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setName("context")
.setType(
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(PageContext.class)
.setGenerics(
Arrays.asList(
method.inputType(),
method.outputType(),
repeatedResponseType)
.stream()
.map(t -> t.reference())
.collect(Collectors.toList()))
.build()))
.build());
VariableExpr responseVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("response").setType(method.outputType()).build());
MethodDefinition privateCtor =
MethodDefinition.constructorBuilder()
.setScope(ScopeNode.PRIVATE)
.setReturnType(classType)
.setArguments(
Arrays.asList(contextVarExpr, responseVarExpr).stream()
.map(e -> e.toBuilder().setIsDecl(true).build())
.collect(Collectors.toList()))
.setBody(
Arrays.asList(
ExprStatement.withExpr(
ReferenceConstructorExpr.superBuilder()
.setType(classExtendsType)
.setArguments(contextVarExpr, responseVarExpr)
.build())))
.build();

// createEmptyPage method.
ValueExpr nullExpr = ValueExpr.withValue(NullObjectValue.create());
MethodDefinition createEmptyPageMethod =
MethodDefinition.builder()
.setScope(ScopeNode.PRIVATE)
.setIsStatic(true)
.setReturnType(classType)
.setName("createEmptyPage")
.setReturnExpr(
NewObjectExpr.builder().setType(classType).setArguments(nullExpr, nullExpr).build())
.build();

// createPage method.
MethodDefinition createPageMethod =
MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PROTECTED)
.setReturnType(classType)
.setName("createPage")
.setArguments(
Arrays.asList(contextVarExpr, responseVarExpr).stream()
.map(e -> e.toBuilder().setIsDecl(true).build())
.collect(Collectors.toList()))
.setReturnExpr(
NewObjectExpr.builder()
.setType(classType)
.setArguments(contextVarExpr, responseVarExpr)
.build())
.build();

// createPageAsync method.
Function<TypeNode, TypeNode> futureTypeFn =
t ->
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(ApiFuture.class)
.setGenerics(Arrays.asList(t.reference()))
.build());
VariableExpr futureResponseVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setName("futureResponse")
.setType(futureTypeFn.apply(method.outputType()))
.build());
TypeNode futurePageType = futureTypeFn.apply(classType);
MethodDefinition createPageAsyncMethod =
MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(futurePageType)
.setName("createPageAsync")
.setArguments(
Arrays.asList(contextVarExpr, futureResponseVarExpr).stream()
.map(e -> e.toBuilder().setIsDecl(true).build())
.collect(Collectors.toList()))
.setReturnExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(
ValueExpr.withValue(SuperObjectValue.withType(classExtendsType)))
.setMethodName("createPageAsync")
.setArguments(contextVarExpr, futureResponseVarExpr)
.setReturnType(futurePageType)
.build())
.build();

// Build the class.
List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.add(privateCtor);
javaMethods.add(createEmptyPageMethod);
javaMethods.add(createPageMethod);
javaMethods.add(createPageAsyncMethod);

return ClassDefinition.builder()
.setIsNested(true)
.setScope(ScopeNode.PUBLIC)
.setIsStatic(true)
.setExtendsType(classExtendsType)
.setName(className)
.setMethods(javaMethods)
.build();
}

private static Map<String, TypeNode> createTypes(
Service service, Map<String, Message> messageTypes) {
Map<String, TypeNode> types = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void generateServiceClasses() {
+ "import com.google.api.core.BetaApi;\n"
+ "import com.google.api.gax.core.BackgroundResource;\n"
+ "import com.google.api.gax.longrunning.OperationFuture;\n"
+ "import com.google.api.gax.paging.AbstractPage;\n"
+ "import com.google.api.gax.paging.AbstractPagedListResponse;\n"
+ "import com.google.api.gax.rpc.BidiStreamingCallable;\n"
+ "import com.google.api.gax.rpc.ClientStreamingCallable;\n"
Expand Down Expand Up @@ -475,5 +476,34 @@ public void generateServiceClasses() {
+ " super(page, PagedExpandFixedSizeCollection.createEmptyCollection());\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " public static class PagedExpandPage\n"
+ " extends AbstractPage<PagedExpandRequest, PagedExpandResponse, EchoResponse,"
+ " PagedExpandPage> {\n"
+ "\n"
+ " private PagedExpandPage(\n"
+ " PageContext<PagedExpandRequest, PagedExpandResponse, EchoResponse> context,\n"
+ " PagedExpandResponse response) {\n"
+ " super(context, response);\n"
+ " }\n"
+ "\n"
+ " private static PagedExpandPage createEmptyPage() {\n"
+ " return new PagedExpandPage(null, null);\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " protected PagedExpandPage createPage(\n"
+ " PageContext<PagedExpandRequest, PagedExpandResponse, EchoResponse> context,\n"
+ " PagedExpandResponse response) {\n"
+ " return new PagedExpandPage(context, response);\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public ApiFuture<PagedExpandPage> createPageAsync(\n"
+ " PageContext<PagedExpandRequest, PagedExpandResponse, EchoResponse> context,\n"
+ " ApiFuture<PagedExpandResponse> futureResponse) {\n"
+ " return super.createPageAsync(context, futureResponse);\n"
+ " }\n"
+ " }\n"
+ "}\n";
}