Skip to content

Commit

Permalink
add missing request assignment expr
Browse files Browse the repository at this point in the history
  • Loading branch information
summer-ji-eng committed Nov 18, 2020
1 parent 4a9fb01 commit bede6ab
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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, List<MethodArgument> arguments, TypeNode clientType) {
Expand Down Expand Up @@ -114,6 +115,10 @@ private static TryCatchStatement composePagedUnaryRpcMethodSampleCode(
methodArg ->
ExprStatement.withExpr(assignMethodArgumentWithDefaultValue(methodArg)))
.collect(Collectors.toList());
// Assign request with set attributes.
// e.g. EchoRequest request = echoClient.newBuilder().setName(name).build();
bodyStatements.add(
ExprStatement.withExpr(createRequestBuilderExpr(method.inputType(), arguments)));
// For loop client on iterateAll method.
// e.g. for (LoggingServiceV2Client loggingServiceV2Client :
// loggingServiceV2Client.ListLogs(parent).iterateAll()) {
Expand Down Expand Up @@ -209,6 +214,35 @@ private static Expr createIteratorAllMethodExpr(
.build();
}

private static Expr createRequestBuilderExpr(
TypeNode requestType, List<MethodArgument> arguments) {
MethodInvocationExpr newBuilderExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(requestType)
.setMethodName("newBuilder")
.build();
for (MethodArgument arg : arguments) {
newBuilderExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(newBuilderExpr)
.setMethodName(String.format("set%s", JavaStyle.toUpperCamelCase(arg.name())))
.setArguments(
VariableExpr.withVariable(
Variable.builder().setName(arg.name()).setType(arg.type()).build()))
.build();
}
MethodInvocationExpr requestBuildExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(newBuilderExpr)
.setMethodName("build")
.setReturnType(requestType)
.build();
return AssignmentExpr.builder()
.setVariableExpr(createVariableDeclExpr(REQUEST_VAR_NAME, requestType))
.setValueExpr(requestBuildExpr)
.build();
}

private static List<Expr> mapMethodArgumentsToVariableExprs(List<MethodArgument> arguments) {
return arguments.stream()
.map(arg -> createVariableExpr(arg.name(), arg.field().type()))
Expand Down
8 changes: 8 additions & 0 deletions test/integration/goldens/asset/AssetServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ public final UnaryCallable<DeleteFeedRequest, Empty> deleteFeedCallable() {
* String scope = "scope109264468";
* String query = "query107944136";
* List<String> asset_types = new ArrayList<>();
* SearchAllResourcesRequest request =
* SearchAllResourcesRequest.newBuilder()
* .setScope(scope)
* .setQuery(query)
* .setAssetTypes(asset_types)
* .build();
* for (AssetServiceClient assetServiceClient :
* assetServiceClient.SearchAllResources(scope, query, asset_types).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -707,6 +713,8 @@ public final SearchAllResourcesPagedResponse searchAllResources(
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* String scope = "scope109264468";
* String query = "query107944136";
* SearchAllIamPoliciesRequest request =
* SearchAllIamPoliciesRequest.newBuilder().setScope(scope).setQuery(query).build();
* for (AssetServiceClient assetServiceClient :
* assetServiceClient.SearchAllIamPolicies(scope, query).iterateAll()) {
* // doThingsWith(element);
Expand Down
15 changes: 15 additions & 0 deletions test/integration/goldens/logging/ConfigServiceV2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public ConfigServiceV2Stub getStub() {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListBucketsRequest request = ListBucketsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -187,6 +188,7 @@ public final ListBucketsPagedResponse listBuckets(BillingAccountLocationName par
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListBucketsRequest request = ListBucketsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -220,6 +222,7 @@ public final ListBucketsPagedResponse listBuckets(FolderLocationName parent) {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListBucketsRequest request = ListBucketsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -253,6 +256,7 @@ public final ListBucketsPagedResponse listBuckets(LocationName parent) {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListBucketsRequest request = ListBucketsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -286,6 +290,7 @@ public final ListBucketsPagedResponse listBuckets(OrganizationLocationName paren
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListBucketsRequest request = ListBucketsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListBuckets(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -432,6 +437,7 @@ public final UnaryCallable<UpdateBucketRequest, LogBucket> updateBucketCallable(
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListSinksRequest request = ListSinksRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -461,6 +467,7 @@ public final ListSinksPagedResponse listSinks(BillingAccountName parent) {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListSinksRequest request = ListSinksRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -490,6 +497,7 @@ public final ListSinksPagedResponse listSinks(FolderName parent) {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListSinksRequest request = ListSinksRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -519,6 +527,7 @@ public final ListSinksPagedResponse listSinks(OrganizationName parent) {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListSinksRequest request = ListSinksRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -548,6 +557,7 @@ public final ListSinksPagedResponse listSinks(ProjectName parent) {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListSinksRequest request = ListSinksRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListSinks(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -1197,6 +1207,7 @@ public final UnaryCallable<DeleteSinkRequest, Empty> deleteSinkCallable() {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListExclusionsRequest request = ListExclusionsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -1226,6 +1237,7 @@ public final ListExclusionsPagedResponse listExclusions(BillingAccountName paren
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListExclusionsRequest request = ListExclusionsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -1255,6 +1267,7 @@ public final ListExclusionsPagedResponse listExclusions(FolderName parent) {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListExclusionsRequest request = ListExclusionsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -1284,6 +1297,7 @@ public final ListExclusionsPagedResponse listExclusions(OrganizationName parent)
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListExclusionsRequest request = ListExclusionsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -1313,6 +1327,7 @@ public final ListExclusionsPagedResponse listExclusions(ProjectName parent) {
* <pre>{@code
* try (ConfigServiceV2Client configServiceV2Client = ConfigServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListExclusionsRequest request = ListExclusionsRequest.newBuilder().setParent(parent).build();
* for (ConfigServiceV2Client configServiceV2Client :
* configServiceV2Client.ListExclusions(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down
11 changes: 11 additions & 0 deletions test/integration/goldens/logging/LoggingServiceV2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ public final WriteLogEntriesResponse writeLogEntries(WriteLogEntriesRequest requ
* List<String> resource_names = new ArrayList<>();
* String filter = "filter-1274492040";
* String order_by = "order_by1234304744";
* ListLogEntriesRequest request =
* ListLogEntriesRequest.newBuilder()
* .setResourceNames(resource_names)
* .setFilter(filter)
* .setOrderBy(order_by)
* .build();
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogEntries(resource_names, filter, order_by).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -575,6 +581,7 @@ public final ListMonitoredResourceDescriptorsPagedResponse listMonitoredResource
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListLogsRequest request = ListLogsRequest.newBuilder().setParent(parent).build();
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -605,6 +612,7 @@ public final ListLogsPagedResponse listLogs(BillingAccountName parent) {
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListLogsRequest request = ListLogsRequest.newBuilder().setParent(parent).build();
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -635,6 +643,7 @@ public final ListLogsPagedResponse listLogs(FolderName parent) {
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListLogsRequest request = ListLogsRequest.newBuilder().setParent(parent).build();
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -665,6 +674,7 @@ public final ListLogsPagedResponse listLogs(OrganizationName parent) {
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListLogsRequest request = ListLogsRequest.newBuilder().setParent(parent).build();
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -695,6 +705,7 @@ public final ListLogsPagedResponse listLogs(ProjectName parent) {
* <pre>{@code
* try (LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListLogsRequest request = ListLogsRequest.newBuilder().setParent(parent).build();
* for (LoggingServiceV2Client loggingServiceV2Client :
* loggingServiceV2Client.ListLogs(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down
2 changes: 2 additions & 0 deletions test/integration/goldens/logging/MetricsServiceV2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public MetricsServiceV2Stub getStub() {
* <pre>{@code
* try (MetricsServiceV2Client metricsServiceV2Client = MetricsServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(parent).build();
* for (MetricsServiceV2Client metricsServiceV2Client :
* metricsServiceV2Client.ListLogMetrics(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -181,6 +182,7 @@ public final ListLogMetricsPagedResponse listLogMetrics(ProjectName parent) {
* <pre>{@code
* try (MetricsServiceV2Client metricsServiceV2Client = MetricsServiceV2Client.create()) {
* String parent = "parent-995424086";
* ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(parent).build();
* for (MetricsServiceV2Client metricsServiceV2Client :
* metricsServiceV2Client.ListLogMetrics(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down
2 changes: 2 additions & 0 deletions test/integration/goldens/redis/CloudRedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public final OperationsClient getOperationsClient() {
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* String parent = "parent-995424086";
* ListInstancesRequest request = ListInstancesRequest.newBuilder().setParent(parent).build();
* for (CloudRedisClient cloudRedisClient :
* cloudRedisClient.ListInstances(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down Expand Up @@ -231,6 +232,7 @@ public final ListInstancesPagedResponse listInstances(LocationName parent) {
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* String parent = "parent-995424086";
* ListInstancesRequest request = ListInstancesRequest.newBuilder().setParent(parent).build();
* for (CloudRedisClient cloudRedisClient :
* cloudRedisClient.ListInstances(parent).iterateAll()) {
* // doThingsWith(element);
Expand Down

0 comments on commit bede6ab

Please sign in to comment.