Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
summer-ji-eng committed Nov 20, 2020
1 parent 0a85061 commit 4e5fcef
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@

public interface ClassComposer {
GapicClass generate(
// TODO(miraleung): clean up the hierarchy to avoid pass another parameter (resourceNames is
// only used for composing sample code).
Service service, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.api.generator.engine.ast.ExprStatement;
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 Down Expand Up @@ -64,36 +63,32 @@ private static TryCatchStatement composeUnaryRpcMethodSampleCode(
TypeNode clientType,
Map<String, ResourceName> resourceNames) {
// TODO(summerji): Add unit tests.
VariableExpr clientVarExpr = createVariableExpr(getClientName(clientType), clientType);
// Assign each method arguments with default value.
List<Statement> bodyStatements =
List<Expr> bodyExpr =
arguments.stream()
.map(
methodArg ->
ExprStatement.withExpr(
assignMethodArgumentWithDefaultValue(methodArg, resourceNames)))
.map(methodArg -> assignMethodArgumentWithDefaultValue(methodArg, resourceNames))
.collect(Collectors.toList());
// Invoke current method based on return type.
// e.g. if return void, echoClient.echo(..); or,
// e.g. if return other type, EchoResponse response = echoClient.echo(...);
if (method.outputType().equals(TypeNode.VOID)) {
bodyStatements.add(
ExprStatement.withExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(
createVariableDeclExpr(getClientName(clientType), clientType))
.setMethodName(method.name())
.setReturnType(clientType)
.build()));
bodyExpr.add(
MethodInvocationExpr.builder()
.setExprReferenceExpr(clientVarExpr)
.setMethodName(method.name())
.setReturnType(clientType)
.build());
} else {
bodyStatements.add(
ExprStatement.withExpr(
createAssignExprForVariableWithClientMethod(
RESPONSE_VAR_NAME, method.outputType(), clientType, method.name(), arguments)));
bodyExpr.add(
createAssignExprForVariableWithClientMethod(
RESPONSE_VAR_NAME, method.outputType(), clientVarExpr, method.name(), arguments));
}

return TryCatchStatement.builder()
.setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientType))
.setTryBody(bodyStatements)
.setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(
bodyExpr.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()))
.setIsSampleCode(true)
.build();
}
Expand Down Expand Up @@ -174,13 +169,13 @@ private static Expr assignMethodArgumentWithDefaultValue(
private static Expr createAssignExprForVariableWithClientMethod(
String variableName,
TypeNode variableType,
TypeNode clientType,
VariableExpr clientVarExpr,
String methodName,
List<MethodArgument> arguments) {
VariableExpr varExpr = createVariableExpr(variableName, variableType);
MethodInvocationExpr clientMethodInvocationExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(createVariableExpr(getClientName(clientType), clientType))
.setExprReferenceExpr(clientVarExpr)
.setMethodName(methodName)
.setArguments(mapMethodArgumentsToVariableExprs(arguments))
.setReturnType(variableType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ServiceClientSampleCodeComposer {
// TODO(summerji): Add unit tests for ServiceClientSampleCodeComposer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not Implement yet, placeholder for unary default rpc method sample code.
* // Note: Not implemented yet, placeholder for unary default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -334,7 +334,7 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not Implement yet, placeholder for unary paged rpc method sample code.
* // Note: Not implemented yet, placeholder for unary paged default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -364,7 +364,7 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not Implement yet, placeholder for lro Unary rpc method sample code.
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* }
* }</pre>
*
Expand Down
26 changes: 13 additions & 13 deletions test/integration/goldens/asset/AssetServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
* <pre>{@code
* AssetServiceSettings assetServiceSettings =
* AssetServiceSettings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
* .build();
* AssetServiceClient assetServiceClient = AssetServiceClient.create(assetServiceSettings);
* }</pre>
Expand All @@ -87,7 +87,7 @@
*
* <pre>{@code
* AssetServiceSettings assetServiceSettings =
* AssetServiceSettings.newBuilder().setEndpoint("myEndpoint").build();
* AssetServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
* AssetServiceClient assetServiceClient = AssetServiceClient.create(assetServiceSettings);
* }</pre>
*/
Expand Down Expand Up @@ -170,7 +170,7 @@ public final OperationsClient getOperationsClient() {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary lro default rpc method sample code.
* // Note: Not implemented yet, placeholder for unary lro default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -229,7 +229,7 @@ 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.
* // Note: Not implemented yet, placeholder for unary default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -288,7 +288,7 @@ 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.
* // Note: Not implemented yet, placeholder for unary default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -364,7 +364,7 @@ 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.
* // Note: Not implemented yet, placeholder for unary default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -416,7 +416,7 @@ 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.
* // Note: Not implemented yet, placeholder for unary default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -468,7 +468,7 @@ 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.
* // Note: Not implemented yet, placeholder for unary default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -546,7 +546,7 @@ 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.
* // Note: Not implemented yet, placeholder for unary default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -577,7 +577,7 @@ public final UnaryCallable<DeleteFeedRequest, Empty> deleteFeedCallable() {
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for paged unary rpc method sample code.
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -651,7 +651,7 @@ public final SearchAllResourcesPagedResponse searchAllResources(
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for unary paged default rpc method sample code.
* // Note: Not implemented yet, placeholder for unary paged default rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -699,7 +699,7 @@ public final SearchAllResourcesPagedResponse searchAllResources(
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not Implement yet, placeholder for paged unary rpc method sample code.
* // Note: Not implemented yet, placeholder for paged unary rpc method sample code.
* }
* }</pre>
*
Expand Down Expand Up @@ -757,7 +757,7 @@ 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.
* // Note: Not implemented yet, placeholder for unary paged default rpc method sample code.
* }
* }</pre>
*
Expand Down
Loading

0 comments on commit 4e5fcef

Please sign in to comment.