Skip to content

Commit

Permalink
Implement default unary rpc sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
summer-ji-eng committed Nov 18, 2020
1 parent 8c42a62 commit 37497db
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.api.generator.gapic.model.ResourceName;
import com.google.api.generator.gapic.utils.JavaStyle;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -47,7 +48,7 @@ public static TryCatchStatement composeRpcMethodSampleCode(
Map<String, ResourceName> resourceNames) {
// Default Unary RPC method.
if (arguments.isEmpty()) {
return composeUnaryRpcDefaultMethodSampleCode(method, clientType);
return composeUnaryRpcDefaultMethodSampleCode(method, clientType, resourceNames);
}
// Paged Unary RPC method.
if (method.isPaged()) {
Expand Down Expand Up @@ -174,18 +175,78 @@ private static TryCatchStatement composePagedUnaryRpcMethodSampleCode(
}

private static TryCatchStatement composeUnaryRpcDefaultMethodSampleCode(
Method method, TypeNode clientType) {
Method method, TypeNode clientType, Map<String, ResourceName> resourceNames) {
// TODO(summerji): compose sample code for unary default rpc method.
// TODO(summerji): Add unit tests.
String content =
String.format(
"Note: Not Implement yet, placeholder for unary %s rpc method sample code.",
(!method.hasLro() && !method.isPaged()
? "default"
: (method.hasLro() ? "lro default" : "paged default")));
// If variant method signatures exists, use the first one.
List<MethodArgument> arguments =
!method.methodSignatures().isEmpty()
? method.methodSignatures().get(0)
: Collections.emptyList();
// Assign each method arguments with default value.
List<Statement> bodyStatements =
arguments.stream()
.map(
methodArg ->
ExprStatement.withExpr(
assignMethodArgumentWithDefaultValue(methodArg, resourceNames)))
.collect(Collectors.toList());
// Assign request variables with set argument attributes.
// e.g EchoRequest
bodyStatements.add(
ExprStatement.withExpr(createRequestBuilderExpr(method.inputType(), arguments)));

if (method.isPaged()) {
// For loop on invoke client method's iterator with comment.
// e.g. for (EchoClient echoClient : echoClient.PagedExpand(request).iterateAll()) {//..}
bodyStatements.add(
ForStatement.builder()
.setLocalVariableExpr(createVariableDeclExpr(getClientName(clientType), clientType))
.setCollectionExpr(createIteratorAllMethodExpr(method, clientType, arguments))
.setBody(Arrays.asList(createLineCommentStatement("doThingsWith(element);")))
.build());
} else if (method.hasLro()) {
// Create response variable by invoke client's async method.
// e.g Operation response = EchoClient.waitAsync(request).get();
Expr getResponseMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(clientType)
.setMethodName(getLroMethodName(method.name()))
.setArguments(
Arrays.asList(createVariableExpr(REQUEST_VAR_NAME, method.inputType())))
.build())
.setMethodName("get")
.setReturnType(method.outputType())
.build();
bodyStatements.add(
ExprStatement.withExpr(
AssignmentExpr.builder()
.setVariableExpr(createVariableDeclExpr(RESPONSE_VAR_NAME, method.outputType()))
.setValueExpr(getResponseMethodExpr)
.build()));
} else {
// Create response variable by invoke client's method by passing request.
// e.g. EchoResponse response = echoClient.Echo(request);
Expr invokeMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(createVariableExpr(getClientName(clientType), clientType))
.setMethodName(method.name())
.setArguments(createVariableExpr("request", method.inputType()))
.setReturnType(method.outputType())
.build();
bodyStatements.add(
ExprStatement.withExpr(
AssignmentExpr.builder()
.setVariableExpr(createVariableDeclExpr(RESPONSE_VAR_NAME, method.outputType()))
.setValueExpr(invokeMethodExpr)
.build()));
}

return TryCatchStatement.builder()
.setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientType))
.setTryBody(Arrays.asList(createLineCommentStatement(content)))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build();
}
Expand All @@ -206,6 +267,37 @@ private static AssignmentExpr assignClientVariableWithCreateMethodExpr(TypeNode
.build();
}

// Create request variable by set attributes.
// e.g. EchoRequest request = EchoRequest.newBuilder().setParent(parent).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", requestType))
.setValueExpr(requestBuildExpr)
.build();
}

private static Expr assignMethodArgumentWithDefaultValue(
MethodArgument argument, Map<String, ResourceName> resourceNames) {
return AssignmentExpr.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

public class ServiceClientSampleCodeComposer {
// TODO(summerji): Add unit tests for ServiceClientSampleCodeComposer.
// TODO(summerji): Refactor signatures as sample code context.

public static String composeClassHeaderCredentialsSampleCode(
TypeNode clientType, TypeNode settingsType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* ResourceName parent = FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]");
* EchoRequest request = EchoRequest.newBuilder().setParent(parent).build();
* EchoResponse response = echoClient.Echo(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -334,7 +336,10 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not Implement yet, placeholder for unary paged default rpc method sample code.
* PagedExpandRequest request = PagedExpandRequest.newBuilder().build();
* for (EchoClient echoClient : echoClient.PagedExpand(request).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -402,7 +407,9 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not Implement yet, placeholder for unary lro default rpc method sample code.
* Duration ttl = Duration.newBuilder().build();
* WaitRequest request = WaitRequest.newBuilder().setTtl(ttl).build();
* Operation response = EchoClient.waitAsync(request).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -431,7 +438,8 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* BlockRequest request = BlockRequest.newBuilder().build();
* BlockResponse response = echoClient.Block(request);
* }
* }</pre>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,16 @@ public class IdentityClient implements BackgroundResource {
*
* <pre>{@code
* try (IdentityClient identityClient = IdentityClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* String parent = "parent-995424086";
* String display_name = "display_name1615086568";
* String email = "email96619420";
* CreateUserRequest request =
* CreateUserRequest.newBuilder()
* .setParent(parent)
* .setDisplayName(display_name)
* .setEmail(email)
* .build();
* User response = identityClient.CreateUser(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -266,7 +275,9 @@ public class IdentityClient implements BackgroundResource {
*
* <pre>{@code
* try (IdentityClient identityClient = IdentityClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* UserName name = UserName.of("[USER]");
* GetUserRequest request = GetUserRequest.newBuilder().setName(name).build();
* User response = identityClient.GetUser(request);
* }
* }</pre>
*
Expand All @@ -289,7 +300,8 @@ public class IdentityClient implements BackgroundResource {
*
* <pre>{@code
* try (IdentityClient identityClient = IdentityClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* UpdateUserRequest request = UpdateUserRequest.newBuilder().build();
* User response = identityClient.UpdateUser(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -353,7 +365,9 @@ public class IdentityClient implements BackgroundResource {
*
* <pre>{@code
* try (IdentityClient identityClient = IdentityClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* UserName name = UserName.of("[USER]");
* DeleteUserRequest request = DeleteUserRequest.newBuilder().setName(name).build();
* Empty response = identityClient.DeleteUser(request);
* }
* }</pre>
*
Expand All @@ -376,7 +390,10 @@ public class IdentityClient implements BackgroundResource {
*
* <pre>{@code
* try (IdentityClient identityClient = IdentityClient.create()) {
* // Note: Not Implement yet, placeholder for unary paged default rpc method sample code.
* ListUsersRequest request = ListUsersRequest.newBuilder().build();
* for (IdentityClient identityClient : identityClient.ListUsers(request).iterateAll()) {
* // doThingsWith(element);
* }
* }
* }</pre>
*
Expand Down
49 changes: 40 additions & 9 deletions test/integration/goldens/asset/AssetServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ public final OperationsClient getOperationsClient() {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary lro default rpc method sample code.
* ExportAssetsRequest request = ExportAssetsRequest.newBuilder().build();
* Operation response = AssetServiceClient.exportAssetsAsync(request).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -229,7 +230,8 @@ public final UnaryCallable<ExportAssetsRequest, Operation> exportAssetsCallable(
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* BatchGetAssetsHistoryRequest request = BatchGetAssetsHistoryRequest.newBuilder().build();
* BatchGetAssetsHistoryResponse response = assetServiceClient.BatchGetAssetsHistory(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -288,7 +290,9 @@ public final Feed createFeed(String parent) {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* String parent = "parent-995424086";
* CreateFeedRequest request = CreateFeedRequest.newBuilder().setParent(parent).build();
* Feed response = assetServiceClient.CreateFeed(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -364,7 +368,9 @@ public final Feed getFeed(String name) {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]");
* GetFeedRequest request = GetFeedRequest.newBuilder().setName(name).build();
* Feed response = assetServiceClient.GetFeed(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -416,7 +422,9 @@ public final ListFeedsResponse listFeeds(String parent) {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* String parent = "parent-995424086";
* ListFeedsRequest request = ListFeedsRequest.newBuilder().setParent(parent).build();
* ListFeedsResponse response = assetServiceClient.ListFeeds(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -468,7 +476,9 @@ public final Feed updateFeed(Feed feed) {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* Feed feed = Feed.newBuilder().build();
* UpdateFeedRequest request = UpdateFeedRequest.newBuilder().setFeed(feed).build();
* Feed response = assetServiceClient.UpdateFeed(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -546,7 +556,9 @@ public final Empty deleteFeed(String name) {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]");
* DeleteFeedRequest request = DeleteFeedRequest.newBuilder().setName(name).build();
* Empty response = assetServiceClient.DeleteFeed(request);
* }
* }</pre>
*
Expand Down Expand Up @@ -657,7 +669,19 @@ public final SearchAllResourcesPagedResponse searchAllResources(
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary paged default rpc method sample code.
* 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);
* }
* }
* }</pre>
*
Expand Down Expand Up @@ -768,7 +792,14 @@ public final SearchAllIamPoliciesPagedResponse searchAllIamPolicies(String scope
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary paged default rpc method sample code.
* 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);
* }
* }
* }</pre>
*
Expand Down
Loading

0 comments on commit 37497db

Please sign in to comment.