-
Notifications
You must be signed in to change notification settings - Fork 55
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
Changes from all commits
6cd1965
5411d6a
fdae55e
86805b5
045430a
d54bb6d
ea13f59
247a8be
b82635a
14cbbd3
51a0cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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()) { | ||
|
@@ -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 : | ||
// 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we stick with Java lowerCamelCase conventions? |
||
* for (LoggingServiceV2Client loggingServiceV2Client : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
* | ||
|
@@ -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> | ||
* | ||
|
@@ -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> | ||
* | ||
|
@@ -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> | ||
* | ||
|
@@ -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> | ||
* | ||
|
@@ -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> | ||
* | ||
|
There was a problem hiding this comment.
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.