Skip to content

Commit ca97e68

Browse files
authored
cleanup(generator): check extension existence in IsGRPCLongrunningOperation (#14675)
1 parent 0db8bbf commit ca97e68

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

generator/internal/longrunning.cc

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,8 @@ DeduceLongrunningOperationResponseType(
7575
} // namespace
7676

7777
bool IsLongrunningOperation(MethodDescriptor const& method) {
78-
bool grpc_lro =
79-
method.output_type()->full_name() == "google.longrunning.Operation";
80-
auto operation_service_extension =
81-
method.options().GetExtension(google::cloud::operation_service);
82-
bool http_lro = !operation_service_extension.empty();
83-
return grpc_lro || http_lro;
78+
return IsGRPCLongrunningOperation(method) ||
79+
IsHttpLongrunningOperation(method);
8480
}
8581

8682
bool IsLongrunningMetadataTypeUsedAsResponse(MethodDescriptor const& method) {
@@ -95,10 +91,11 @@ bool IsLongrunningMetadataTypeUsedAsResponse(MethodDescriptor const& method) {
9591
void SetLongrunningOperationMethodVars(
9692
google::protobuf::MethodDescriptor const& method,
9793
VarsDictionary& method_vars) {
94+
if (!IsLongrunningOperation(method)) return;
9895
method_vars["longrunning_operation_type"] =
9996
ProtoNameToCppName(method.output_type()->full_name());
10097

101-
if (method.output_type()->full_name() == "google.longrunning.Operation") {
98+
if (IsGRPCLongrunningOperation(method)) {
10299
auto operation_info =
103100
method.options().GetExtension(google::longrunning::operation_info);
104101
method_vars["longrunning_metadata_type"] = ProtoNameToCppName(absl::visit(
@@ -118,9 +115,7 @@ void SetLongrunningOperationMethodVars(
118115
return;
119116
}
120117

121-
auto operation_service_extension =
122-
method.options().GetExtension(google::cloud::operation_service);
123-
if (!operation_service_extension.empty()) {
118+
if (IsHttpLongrunningOperation(method)) {
124119
method_vars["longrunning_response_type"] = ProtoNameToCppName(absl::visit(
125120
FullyQualifiedMessageTypeVisitor(),
126121
FullyQualifyMessageType(method, method.output_type()->full_name())));
@@ -136,7 +131,8 @@ void SetLongrunningOperationMethodVars(
136131
}
137132

138133
bool IsGRPCLongrunningOperation(MethodDescriptor const& method) {
139-
return method.output_type()->full_name() == "google.longrunning.Operation";
134+
return method.output_type()->full_name() == "google.longrunning.Operation" &&
135+
method.options().HasExtension(google::longrunning::operation_info);
140136
}
141137

142138
bool IsHttpLongrunningOperation(MethodDescriptor const& method) {
@@ -150,7 +146,7 @@ void SetLongrunningOperationServiceVars(
150146
VarsDictionary& service_vars) {
151147
for (int i = 0; i != service.method_count(); ++i) {
152148
auto const* method = service.method(i);
153-
if (method->output_type()->full_name() == "google.longrunning.Operation") {
149+
if (IsGRPCLongrunningOperation(*method)) {
154150
service_vars["longrunning_operation_include_header"] =
155151
"google/longrunning/operations.pb.h";
156152
service_vars["longrunning_response_type"] =
@@ -169,9 +165,7 @@ void SetLongrunningOperationServiceVars(
169165
R"""(absl::StrCat("/", rest_internal::DetermineApiVersion("v1", *options) ,"/", request.name(), ":cancel"))""";
170166
return;
171167
}
172-
if (!method->options()
173-
.GetExtension(google::cloud::operation_service)
174-
.empty()) {
168+
if (IsHttpLongrunningOperation(*method)) {
175169
service_vars["longrunning_response_type"] = ProtoNameToCppName(
176170
absl::visit(FullyQualifiedMessageTypeVisitor(),
177171
FullyQualifyMessageType(

generator/internal/longrunning_test.cc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,20 @@ TEST(LongrunningTest, IsGRPCLongrunningOperation) {
4646
service {
4747
name: "Service"
4848
method {
49-
name: "Lro"
49+
name: "GrpcLro"
50+
input_type: "google.longrunning.Bar"
51+
output_type: "google.longrunning.Operation"
52+
options {
53+
[google.longrunning.operation_info] {}
54+
}
55+
}
56+
method {
57+
name: "NonLro1"
5058
input_type: "google.longrunning.Bar"
5159
output_type: "google.longrunning.Operation"
5260
}
5361
method {
54-
name: "NonLro"
62+
name: "NonLro2"
5563
input_type: "google.longrunning.Bar"
5664
output_type: "google.longrunning.Bar"
5765
}
@@ -61,14 +69,20 @@ TEST(LongrunningTest, IsGRPCLongrunningOperation) {
6169
ASSERT_TRUE(TextFormat::ParseFromString(kServiceText, &service_file));
6270
DescriptorPool pool;
6371
FileDescriptor const* service_file_descriptor = pool.BuildFile(service_file);
64-
EXPECT_TRUE(
65-
IsLongrunningOperation(*service_file_descriptor->service(0)->method(0)));
6672
EXPECT_TRUE(IsGRPCLongrunningOperation(
6773
*service_file_descriptor->service(0)->method(0)));
6874
EXPECT_FALSE(IsHttpLongrunningOperation(
6975
*service_file_descriptor->service(0)->method(0)));
76+
EXPECT_FALSE(IsGRPCLongrunningOperation(
77+
*service_file_descriptor->service(0)->method(1)));
78+
EXPECT_FALSE(IsGRPCLongrunningOperation(
79+
*service_file_descriptor->service(0)->method(2)));
80+
EXPECT_TRUE(
81+
IsLongrunningOperation(*service_file_descriptor->service(0)->method(0)));
7082
EXPECT_FALSE(
7183
IsLongrunningOperation(*service_file_descriptor->service(0)->method(1)));
84+
EXPECT_FALSE(
85+
IsLongrunningOperation(*service_file_descriptor->service(0)->method(2)));
7286
}
7387

7488
TEST(LongrunningTest, IsLongrunningMetadataTypeUsedAsResponseEmptyResponse) {

generator/internal/service_code_generator_test.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ TEST(PredicateUtilsTest, HasLongRunningMethodOne) {
171171
name: "One"
172172
input_type: "google.protobuf.Bar"
173173
output_type: "google.longrunning.Operation"
174+
options {
175+
[google.longrunning.operation_info] {}
176+
}
174177
}
175178
method {
176179
name: "Two"
@@ -224,11 +227,17 @@ TEST(PredicateUtilsTest, HasLongRunningMethodMoreThanOne) {
224227
name: "Two"
225228
input_type: "google.protobuf.Bar"
226229
output_type: "google.longrunning.Operation"
230+
options {
231+
[google.longrunning.operation_info] {}
232+
}
227233
}
228234
method {
229235
name: "Three"
230236
input_type: "google.protobuf.Bar"
231237
output_type: "google.longrunning.Operation"
238+
options {
239+
[google.longrunning.operation_info] {}
240+
}
232241
}
233242
}
234243
)pb";

0 commit comments

Comments
 (0)