Skip to content

Commit

Permalink
[ggj][codegen] feat: add ServiceClient.MethodFixedSizeCollection list…
Browse files Browse the repository at this point in the history
… method inner class (#327)

* feat: add protobuf comment parser util

* fix: add basic proto build rules

* feat: add header comments to ServiceClient

* fix: build protoc at test time

* fix!: wrap protobuf location and process comments

* feat: add comment parsing to methods and fields

* fix: test

* feat: add protobuf comments to ServiceClient

* fix: solidify codegen method order with TypeNode/MethodArg and Comparable

* fix: clean up tests

* fix: ServiceClient member variables and method calls

* fix: ServiceStubSettings builder type

* fix: ServiceSettings Builder construction

* fix: ServiceStub callable types

* feat: java_gapic_library rule impl

* fix: remove debugging comments

* feat: add gradle assembly Bazel rules

* feat: add java_gapic_test Bazel rule

* fix: use Java packages for resname codegen

* fix: build resnames separately and extract into proto/ dir

* fix: remove debug printf

* feat: add ServiceClient.MethodPagedResponse inner class

* feat: add ServiceClient.MethodPage inner class

* feat: add ServiceClient.MethodFixedSizeCollection innser class
  • Loading branch information
miraleung authored Sep 19, 2020
1 parent d553c43 commit 1a1422e
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
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.AbstractFixedSizeCollection;
import com.google.api.gax.paging.AbstractPage;
import com.google.api.gax.paging.AbstractPagedListResponse;
import com.google.api.gax.rpc.BidiStreamingCallable;
Expand All @@ -40,6 +41,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.PrimitiveValue;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.ReferenceConstructorExpr;
import com.google.api.generator.engine.ast.ScopeNode;
Expand Down Expand Up @@ -916,6 +918,9 @@ private static List<ClassDefinition> createNestedPagingClasses(
createNestedRpcPagedResponseClass(method, repeatedResponseType, messageTypes, types));
nestedClasses.add(
createNestedRpcPageClass(method, repeatedResponseType, messageTypes, types));
nestedClasses.add(
createNestedRpcFixedSizeCollectionClass(
method, repeatedResponseType, messageTypes, types));
}

return nestedClasses;
Expand Down Expand Up @@ -1264,6 +1269,115 @@ private static ClassDefinition createNestedRpcPageClass(
.build();
}

private static ClassDefinition createNestedRpcFixedSizeCollectionClass(
Method method,
TypeNode repeatedResponseType,
Map<String, Message> messageTypes,
Map<String, TypeNode> types) {
String upperJavaMethodName = JavaStyle.toUpperCamelCase(method.name());
String className = String.format("%sFixedSizeCollection", upperJavaMethodName);
TypeNode classType = types.get(className);
TypeNode methodPageType = types.get(String.format("%sPage", upperJavaMethodName));

TypeNode classExtendsType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(AbstractFixedSizeCollection.class)
.setGenerics(
Arrays.asList(
method.inputType(),
method.outputType(),
repeatedResponseType,
methodPageType,
classType)
.stream()
.map(t -> t.reference())
.collect(Collectors.toList()))
.build());

// Private constructor.
VariableExpr pagesVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setName("pages")
.setType(
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(List.class)
.setGenerics(Arrays.asList(methodPageType.reference()))
.build()))
.build());
VariableExpr collectionSizeVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("collectionSize").setType(TypeNode.INT).build());

MethodDefinition privateCtor =
MethodDefinition.constructorBuilder()
.setScope(ScopeNode.PRIVATE)
.setReturnType(classType)
.setArguments(
Arrays.asList(pagesVarExpr, collectionSizeVarExpr).stream()
.map(e -> e.toBuilder().setIsDecl(true).build())
.collect(Collectors.toList()))
.setBody(
Arrays.asList(
ExprStatement.withExpr(
ReferenceConstructorExpr.superBuilder()
.setType(classExtendsType)
.setArguments(pagesVarExpr, collectionSizeVarExpr)
.build())))
.build();

// createEmptyCollection method.
MethodDefinition createEmptyCollectionMethod =
MethodDefinition.builder()
.setScope(ScopeNode.PRIVATE)
.setIsStatic(true)
.setReturnType(classType)
.setName("createEmptyCollection")
.setReturnExpr(
NewObjectExpr.builder()
.setType(classType)
.setArguments(
ValueExpr.withValue(NullObjectValue.create()),
ValueExpr.withValue(
PrimitiveValue.builder().setType(TypeNode.INT).setValue("0").build()))
.build())
.build();

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

List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.add(privateCtor);
javaMethods.add(createEmptyCollectionMethod);
javaMethods.add(createCollectionMethod);

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.AbstractFixedSizeCollection;\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"
Expand All @@ -86,6 +87,7 @@ public void generateServiceClasses() {
+ "import com.google.showcase.v1beta1.stub.EchoStub;\n"
+ "import com.google.showcase.v1beta1.stub.EchoStubSettings;\n"
+ "import java.io.IOException;\n"
+ "import java.util.List;\n"
+ "import java.util.Objects;\n"
+ "import java.util.concurrent.TimeUnit;\n"
+ "import javax.annotation.Generated;\n"
Expand Down Expand Up @@ -505,5 +507,29 @@ public void generateServiceClasses() {
+ " return super.createPageAsync(context, futureResponse);\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " public static class PagedExpandFixedSizeCollection\n"
+ " extends AbstractFixedSizeCollection<\n"
+ " PagedExpandRequest,\n"
+ " PagedExpandResponse,\n"
+ " EchoResponse,\n"
+ " PagedExpandPage,\n"
+ " PagedExpandFixedSizeCollection> {\n"
+ "\n"
+ " private PagedExpandFixedSizeCollection(List<PagedExpandPage> pages, int"
+ " collectionSize) {\n"
+ " super(pages, collectionSize);\n"
+ " }\n"
+ "\n"
+ " private static PagedExpandFixedSizeCollection createEmptyCollection() {\n"
+ " return new PagedExpandFixedSizeCollection(null, 0);\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " protected PagedExpandFixedSizeCollection createCollection(\n"
+ " List<PagedExpandPage> pages, int collectionSize) {\n"
+ " return new PagedExpandFixedSizeCollection(pages, collectionSize);\n"
+ " }\n"
+ " }\n"
+ "}\n";
}

0 comments on commit 1a1422e

Please sign in to comment.