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

[draft][4/5]Implement LRO unary rpc method's sample code #514

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
de732d4
Implement sample code of customize credentials in service client clas…
summer-ji-eng Nov 16, 2020
e5df14b
Add license
summer-ji-eng Nov 16, 2020
81d71db
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng Nov 16, 2020
e214824
Add comments in composer, add TODOs, refactor the signatures
summer-ji-eng Nov 17, 2020
b9f8024
format the files
summer-ji-eng Nov 17, 2020
85b6f27
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng Nov 17, 2020
2ae0cce
refactor signature
summer-ji-eng Nov 17, 2020
e390c07
Merge branch 'service_client_credential_sample_code1' of github.com:g…
summer-ji-eng Nov 17, 2020
cc843c6
simplify the input parameters
summer-ji-eng Nov 17, 2020
aa87b09
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng Nov 17, 2020
d164c52
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng Nov 18, 2020
f997138
Implement set endpoint in service client class comment header
summer-ji-eng Nov 16, 2020
e5e6014
Refactor signatures, add todos, and comments
summer-ji-eng Nov 17, 2020
260d668
simplify the input parameters
summer-ji-eng Nov 17, 2020
6306d16
Structure sample code in rpc methods
summer-ji-eng Nov 17, 2020
5b7a57c
add license
summer-ji-eng Nov 18, 2020
0a5b153
fix note for default unary rpc
summer-ji-eng Nov 18, 2020
4b74392
use placeholder
summer-ji-eng Nov 18, 2020
cacf7d0
Implement pure rpc sample code
summer-ji-eng Nov 18, 2020
afc5a36
refactor unary rpc method
summer-ji-eng Nov 18, 2020
9abb259
fix revert changed
summer-ji-eng Nov 18, 2020
4d9e3d7
refactor signature pass Map<String, ResourceName> resourceNames
summer-ji-eng Nov 18, 2020
790cf67
run google format
summer-ji-eng Nov 18, 2020
5d53029
Implement paged unary rpc method' sample code
summer-ji-eng Nov 18, 2020
3a9e88c
fix client
summer-ji-eng Nov 18, 2020
a3f5f22
rebase previous branch
summer-ji-eng Nov 18, 2020
bb14577
add missing comment
summer-ji-eng Nov 18, 2020
d21b247
add missing request assignment expr
summer-ji-eng Nov 18, 2020
34877cf
Implement lro unary rpc method's sample code
summer-ji-eng Nov 18, 2020
06fe431
remove request assignment
summer-ji-eng Nov 18, 2020
8c42a62
rebase upstream
summer-ji-eng Nov 18, 2020
720aba1
Merge branch 'rpc_unary_paged_sc_33' into rpc_unary_lro_sc34
summer-ji-eng Nov 20, 2020
3fbdd1e
rebase 33 branch and refactor varialbe expr
summer-ji-eng Nov 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
public final class SampleCodeHelperComposer {
private static String RESPONSE_VAR_NAME = "response";
private static String REQUEST_VAR_NAME = "request";
private static String ASYNC_NAME_PATTERN = "%sAsync";

public static TryCatchStatement composeRpcMethodSampleCode(
Method method,
Expand All @@ -54,7 +55,7 @@ public static TryCatchStatement composeRpcMethodSampleCode(
}
// Long-running operation Unary RPC method.
if (method.hasLro()) {
return composeLroUnaryRpcMethodSampleCode(method, arguments, clientType);
return composeLroUnaryRpcMethodSampleCode(method, arguments, clientType, resourceNames);
}
// Pure Unary RPC method.
return composeUnaryRpcMethodSampleCode(method, arguments, clientType, resourceNames);
Expand Down Expand Up @@ -97,16 +98,40 @@ private static TryCatchStatement composeUnaryRpcMethodSampleCode(
}

private static TryCatchStatement composeLroUnaryRpcMethodSampleCode(
Method method, List<MethodArgument> arguments, TypeNode clientType) {
Method method,
List<MethodArgument> arguments,
TypeNode clientType,
Map<String, ResourceName> resourceNames) {
// TODO(summerji): compose sample code for unary lro rpc method.
// TODO(summerji): Add unit tests.
VariableExpr clientVarExpr = createVariableExpr(getClientName(clientType), clientType);
// Assign each method arguments with default value.
List<Expr> bodyExprs =
arguments.stream()
.map(methodArg -> assignMethodArgumentWithDefaultValue(methodArg, resourceNames))
.collect(Collectors.toList());
// Assign response variable with get method.
// e.g EchoResponse response = echoClient.waitAsync().get();
Expr getResponseMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(clientVarExpr)
.setMethodName(getLroMethodName(method.name()))
.setArguments(mapMethodArgumentsToVariableExprs(arguments))
.build())
.setMethodName("get")
.setReturnType(method.outputType())
.build();
bodyExprs.add(
AssignmentExpr.builder()
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: fix consider method.outputType is VOID

.setVariableExpr(createVariableDeclExpr(RESPONSE_VAR_NAME, method.outputType()))
.setValueExpr(getResponseMethodExpr)
.build());
return TryCatchStatement.builder()
.setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(
Arrays.asList(
createLineCommentStatement(
"Note: Not implemented yet, placeholder for lro Unary rpc method sample code.")))
bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()))
.setIsSampleCode(true)
.build();
}
Expand All @@ -128,8 +153,8 @@ private static TryCatchStatement composePagedUnaryRpcMethodSampleCode(
.collect(Collectors.toList());
// For loop client on iterateAll method.
// e.g. for (LoggingServiceV2Client loggingServiceV2Client :
// loggingServiceV2Client.ListLogs(parent).iterateAll()) {
// //doThingsWith(element);
// loggingServiceV2Client.ListLogs(parent).iterateAll()) {
// //doThingsWith(element);
// }
bodyStatements.add(
ForStatement.builder()
Expand Down Expand Up @@ -235,6 +260,10 @@ private static String getClientName(TypeNode clientType) {
return JavaStyle.toLowerCamelCase(clientType.reference().name());
}

private static String getLroMethodName(String methodName) {
return JavaStyle.toLowerCamelCase(String.format(ASYNC_NAME_PATTERN, methodName));
}

private static CommentStatement createLineCommentStatement(String content) {
return CommentStatement.withComment(LineComment.withComment(content));
}
Expand All @@ -250,8 +279,12 @@ private static VariableExpr createVariableDeclExpr(String variableName, TypeNode
private static VariableExpr createVariableExpr(
String variableName, TypeNode type, boolean isDecl) {
return VariableExpr.builder()
.setVariable(Variable.builder().setName(variableName).setType(type).build())
.setVariable(createVariable(variableName, type))
.setIsDecl(isDecl)
.build();
}

private static Variable createVariable(String varName, TypeNode type) {
return Variable.builder().setName(varName).setType(type).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* Duration ttl = Duration.newBuilder().build();
* Operation response = echoClient.waitAsync(ttl).get();
* }
* }</pre>
*
Expand All @@ -382,7 +383,8 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* Timestamp end_time = Timestamp.newBuilder().build();
* Operation response = echoClient.waitAsync(end_time).get();
* }
* }</pre>
*
Expand Down
48 changes: 37 additions & 11 deletions test/integration/goldens/redis/CloudRedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ public final UnaryCallable<GetInstanceRequest, Instance> getInstanceCallable() {
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
* String instance_id = "instance_id-2101995259";
* Instance instance = Instance.newBuilder().build();
* Operation response =
* cloudRedisClient.createInstanceAsync(parent, instance_id, instance).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -463,7 +467,11 @@ public final OperationFuture<Instance, OperationMetadata> createInstanceAsync(
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* String parent = "parent-995424086";
* String instance_id = "instance_id-2101995259";
* Instance instance = Instance.newBuilder().build();
* Operation response =
* cloudRedisClient.createInstanceAsync(parent, instance_id, instance).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -579,7 +587,9 @@ public final UnaryCallable<CreateInstanceRequest, Operation> createInstanceCalla
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* FieldMask update_mask = FieldMask.newBuilder().build();
* Instance instance = Instance.newBuilder().build();
* Operation response = cloudRedisClient.updateInstanceAsync(update_mask, instance).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -658,7 +668,9 @@ public final UnaryCallable<UpdateInstanceRequest, Operation> updateInstanceCalla
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* InstanceName name = InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]");
* String redis_version = "redis_version-685310444";
* Operation response = cloudRedisClient.upgradeInstanceAsync(name, redis_version).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -686,7 +698,9 @@ public final OperationFuture<Instance, OperationMetadata> upgradeInstanceAsync(
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* String name = "name3373707";
* String redis_version = "redis_version-685310444";
* Operation response = cloudRedisClient.upgradeInstanceAsync(name, redis_version).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -758,7 +772,9 @@ public final UnaryCallable<UpgradeInstanceRequest, Operation> upgradeInstanceCal
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* String name = "name3373707";
* InputConfig input_config = InputConfig.newBuilder().build();
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's stick with the lowerCamelCase Java conventions.

* Operation response = cloudRedisClient.importInstanceAsync(name, input_config).get();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we please look for applicable examples where the proto would return empty, and handle those as well?

* }
* }</pre>
*
Expand Down Expand Up @@ -847,7 +863,9 @@ public final UnaryCallable<ImportInstanceRequest, Operation> importInstanceCalla
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* String name = "name3373707";
* OutputConfig output_config = OutputConfig.newBuilder().build();
* Operation response = cloudRedisClient.exportInstanceAsync(name, output_config).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -929,7 +947,10 @@ public final UnaryCallable<ExportInstanceRequest, Operation> exportInstanceCalla
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* InstanceName name = InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]");
* FailoverInstanceRequest.DataProtectionMode data_protection_mode =
* FailoverInstanceRequest.DataProtectionMode.forNumber(0);
* Operation response = cloudRedisClient.failoverInstanceAsync(name, data_protection_mode).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -959,7 +980,10 @@ public final OperationFuture<Instance, OperationMetadata> failoverInstanceAsync(
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* String name = "name3373707";
* FailoverInstanceRequest.DataProtectionMode data_protection_mode =
* FailoverInstanceRequest.DataProtectionMode.forNumber(0);
* Operation response = cloudRedisClient.failoverInstanceAsync(name, data_protection_mode).get();
* }
* }</pre>
*
Expand Down Expand Up @@ -1032,7 +1056,8 @@ public final UnaryCallable<FailoverInstanceRequest, Operation> failoverInstanceC
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* InstanceName name = InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]");
* Operation response = cloudRedisClient.deleteInstanceAsync(name).get();
* }
* }</pre>
*
Expand All @@ -1057,7 +1082,8 @@ public final OperationFuture<Empty, OperationMetadata> deleteInstanceAsync(Insta
*
* <pre>{@code
* try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
* // Note: Not implemented yet, placeholder for lro Unary rpc method sample code.
* String name = "name3373707";
* Operation response = cloudRedisClient.deleteInstanceAsync(name).get();
* }
* }</pre>
*
Expand Down