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

[composer][3/5]Implement paged Rpc method's sample code #513

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.ForStatement;
import com.google.api.generator.engine.ast.LineComment;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.TryCatchStatement;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.Variable;
Expand All @@ -35,6 +37,7 @@

public final class SampleCodeHelperComposer {
private static String RESPONSE_VAR_NAME = "response";
private static String REQUEST_VAR_NAME = "request";

public static TryCatchStatement composeRpcMethodSampleCode(
Method method,
Expand All @@ -47,7 +50,7 @@ public static TryCatchStatement composeRpcMethodSampleCode(
}
// Paged Unary RPC method.
if (method.isPaged()) {
return composePagedUnaryRpcMethodSampleCode(method, arguments, clientType);
return composePagedUnaryRpcMethodSampleCode(method, arguments, clientType, resourceNames);
}
// Long-running operation Unary RPC method.
if (method.hasLro()) {
Expand Down Expand Up @@ -109,16 +112,34 @@ private static TryCatchStatement composeLroUnaryRpcMethodSampleCode(
}

private static TryCatchStatement composePagedUnaryRpcMethodSampleCode(
Method method, List<MethodArgument> arguments, TypeNode clientType) {
// TODO(summerji): compose sample code for unary paged rpc method.
// TODO(summerji): Add unit tests.
Method method,
List<MethodArgument> arguments,
TypeNode clientType,
Map<String, ResourceName> resourceNames) {
// TODO(summerji): Add unit test.
VariableExpr clientVarExpr = createVariableExpr(getClientName(clientType), clientType);
// Assign each method arguments with default value.
List<Statement> bodyStatements =
arguments.stream()
.map(
methodArg ->
ExprStatement.withExpr(
assignMethodArgumentWithDefaultValue(methodArg, resourceNames)))
.collect(Collectors.toList());
// For loop client on iterateAll method.
// e.g. for (LoggingServiceV2Client loggingServiceV2Client :
Copy link
Contributor

Choose a reason for hiding this comment

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

PTAL at the comments on the sample snippet below.

// loggingServiceV2Client.ListLogs(parent).iterateAll()) {
// //doThingsWith(element);
// }
bodyStatements.add(
ForStatement.builder()
.setLocalVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build())
.setCollectionExpr(createIteratorAllMethodExpr(method, clientVarExpr, arguments))
.setBody(Arrays.asList(createLineCommentStatement("doThingsWith(element);")))
.build());
return TryCatchStatement.builder()
.setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientVarExpr))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as in #512 about recreating the variables.

.setTryBody(
Arrays.asList(
createLineCommentStatement(
"Note: Not implemented yet, placeholder for paged unary rpc method sample code.")))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build();
}
Expand Down Expand Up @@ -192,6 +213,24 @@ private static List<Expr> mapMethodArgumentsToVariableExprs(List<MethodArgument>
.collect(Collectors.toList());
}

private static Expr createIteratorAllMethodExpr(
Method method, VariableExpr clientVarExpr, List<MethodArgument> arguments) {
// e.g echoClient.echo(name).iterateAll()
return MethodInvocationExpr.builder()
.setExprReferenceExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(clientVarExpr)
.setMethodName(method.name())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: JavaStyle.toLowerCarmelCase(method.name())

.setArguments(
!arguments.isEmpty()
? mapMethodArgumentsToVariableExprs(arguments)
: Arrays.asList(createVariableExpr("request", method.inputType())))
.build())
.setMethodName("iterateAll")
.setReturnType(clientVarExpr.variable().type())
.build();
}

private static String getClientName(TypeNode clientType) {
return JavaStyle.toLowerCamelCase(clientType.reference().name());
}
Expand Down
15 changes: 13 additions & 2 deletions test/integration/goldens/asset/AssetServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,13 @@ public final UnaryCallable<DeleteFeedRequest, Empty> deleteFeedCallable() {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* String scope = "scope109264468";
* String query = "query107944136";
* List<String> asset_types = new ArrayList<>();
* for (AssetServiceClient assetServiceClient :
* assetServiceClient.SearchAllResources(scope, query, asset_types).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -699,7 +705,12 @@ public final SearchAllResourcesPagedResponse searchAllResources(
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* String scope = "scope109264468";
* String query = "query107944136";
* for (AssetServiceClient assetServiceClient :
* assetServiceClient.SearchAllIamPolicies(scope, query).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down
91 changes: 76 additions & 15 deletions test/integration/goldens/logging/ConfigServiceV2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ public ConfigServiceV2Stub getStub() {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* BillingAccountLocationName parent =
* BillingAccountLocationName.of("[BILLING_ACCOUNT]", "[LOCATION]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -182,7 +187,11 @@ public final ListBucketsPagedResponse listBuckets(BillingAccountLocationName par
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* FolderLocationName parent = FolderLocationName.of("[FOLDER]", "[LOCATION]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -211,7 +220,11 @@ public final ListBucketsPagedResponse listBuckets(FolderLocationName parent) {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -240,7 +253,11 @@ public final ListBucketsPagedResponse listBuckets(LocationName parent) {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* OrganizationLocationName parent = OrganizationLocationName.of("[ORGANIZATION]", "[LOCATION]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -269,7 +286,11 @@ public final ListBucketsPagedResponse listBuckets(OrganizationLocationName paren
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* String parent = "parent-995424086";
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -411,7 +432,11 @@ public final UnaryCallable<UpdateBucketRequest, LogBucket> updateBucketCallable(
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -436,7 +461,11 @@ public final ListSinksPagedResponse listSinks(BillingAccountName parent) {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* FolderName parent = FolderName.of("[FOLDER]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -461,7 +490,11 @@ public final ListSinksPagedResponse listSinks(FolderName parent) {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -486,7 +519,11 @@ public final ListSinksPagedResponse listSinks(OrganizationName parent) {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* ProjectName parent = ProjectName.of("[PROJECT]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -511,7 +548,11 @@ public final ListSinksPagedResponse listSinks(ProjectName parent) {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* String parent = "parent-995424086";
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -1156,7 +1197,11 @@ public final UnaryCallable<DeleteSinkRequest, Empty> deleteSinkCallable() {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -1181,7 +1226,11 @@ public final ListExclusionsPagedResponse listExclusions(BillingAccountName paren
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* FolderName parent = FolderName.of("[FOLDER]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -1206,7 +1255,11 @@ public final ListExclusionsPagedResponse listExclusions(FolderName parent) {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -1231,7 +1284,11 @@ public final ListExclusionsPagedResponse listExclusions(OrganizationName parent)
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* ProjectName parent = ProjectName.of("[PROJECT]");
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -1256,7 +1313,11 @@ public final ListExclusionsPagedResponse listExclusions(ProjectName parent) {
*
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* String parent = "parent-995424086";
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down
38 changes: 32 additions & 6 deletions test/integration/goldens/logging/LoggingServiceV2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,13 @@ public final WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest requ
*
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* List<String> resource_names = new ArrayList<>();
* String filter = "filter-1274492040";
Copy link
Contributor

Choose a reason for hiding this comment

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

The samples currently use empty strings, existing example here. Consider detecting this case at the default value call site, or if this kind of different behavior occurs often, having a separate sample-specific variant of the default value method(s).

* String order_by = "order_by1234304744";
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we stick with Java lowerCamelCase conventions?

* for (LoggingServiceV2Client loggingServiceV2Client :
Copy link
Contributor

Choose a reason for hiding this comment

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

PTAL at the sample, the for-loop variable is not the client.

* loggingServiceV2Client.ListLogEntries(resource_names, filter, order_by).iterateAll()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto on the lowerCamelCasing for the variables. IIRC we'd discussed this in a previous review, but the invoked method should match the Java source as well (which the casing also affects).

* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -568,7 +574,11 @@ public final ListMonitoredResourceDescriptorsPagedResponse listMonitoredResource
*
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]");
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -594,7 +604,11 @@ public final ListLogsPagedResponse listLogs(BillingAccountName parent) {
*
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* FolderName parent = FolderName.of("[FOLDER]");
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -620,7 +634,11 @@ public final ListLogsPagedResponse listLogs(FolderName parent) {
*
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -646,7 +664,11 @@ public final ListLogsPagedResponse listLogs(OrganizationName parent) {
*
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* ProjectName parent = ProjectName.of("[PROJECT]");
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand All @@ -672,7 +694,11 @@ public final ListLogsPagedResponse listLogs(ProjectName parent) {
*
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* String parent = "parent-995424086";
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down
Loading