Skip to content

Commit 4f16dfb

Browse files
committed
impl(generator): handle deprecated services
1 parent 000bb23 commit 4f16dfb

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

generator/internal/client_generator.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Status ClientGenerator::GenerateHeader() {
7575

7676
// includes
7777
HeaderPrint("\n");
78+
7879
HeaderLocalIncludes(
7980
{HasGenerateGrpcTransport() ? vars("connection_header_path")
8081
: vars("connection_rest_header_path"),

generator/internal/service_code_generator.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,13 @@ VarsDictionary ServiceCodeGenerator::MergeServiceAndMethodVars(
293293

294294
void ServiceCodeGenerator::HeaderLocalIncludes(
295295
std::vector<std::string> const& local_includes) {
296-
GenerateLocalIncludes(header_, local_includes);
296+
GenerateLocalIncludes(header_, local_includes, FileType::kHeaderFile,
297+
IsDeprecated());
297298
}
298299

299300
void ServiceCodeGenerator::CcLocalIncludes(
300301
std::vector<std::string> const& local_includes) {
301-
GenerateLocalIncludes(cc_, local_includes, FileType::kCcFile);
302+
GenerateLocalIncludes(cc_, local_includes, FileType::kCcFile, IsDeprecated());
302303
}
303304

304305
void ServiceCodeGenerator::HeaderSystemIncludes(
@@ -384,7 +385,14 @@ void ServiceCodeGenerator::CcPrintMethod(
384385
}
385386

386387
void ServiceCodeGenerator::GenerateLocalIncludes(
387-
Printer& p, std::vector<std::string> local_includes, FileType file_type) {
388+
Printer& p, std::vector<std::string> local_includes, FileType file_type,
389+
bool is_deprecated) {
390+
if (is_deprecated) {
391+
p.Print(
392+
"#include "
393+
"\"google/cloud/internal/disable_deprecation_warnings.inc\"\n");
394+
}
395+
388396
if (file_type == FileType::kCcFile) {
389397
std::sort(local_includes.begin() + 1, local_includes.end());
390398
} else {
@@ -507,6 +515,10 @@ std::vector<MixinMethod> const& ServiceCodeGenerator::MixinMethods() const {
507515
return mixin_methods_;
508516
}
509517

518+
bool ServiceCodeGenerator::IsDeprecated() const {
519+
return service_descriptor_->options().deprecated();
520+
}
521+
510522
} // namespace generator_internal
511523
} // namespace cloud
512524
} // namespace google

generator/internal/service_code_generator.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,19 @@ class ServiceCodeGenerator : public GeneratorInterface {
237237

238238
std::vector<MixinMethod> const& MixinMethods() const;
239239

240+
/**
241+
* If the service is defined with `option deprecated = true;`.
242+
* @return
243+
*/
244+
bool IsDeprecated() const;
245+
240246
private:
241247
void SetMethods();
242248

243249
enum class FileType { kHeaderFile, kCcFile };
244250
static void GenerateLocalIncludes(Printer& p,
245251
std::vector<std::string> local_includes,
246-
FileType file_type = FileType::kHeaderFile);
252+
FileType file_type, bool is_deprecated);
247253
static void GenerateSystemIncludes(Printer& p,
248254
std::vector<std::string> system_includes);
249255

generator/internal/service_code_generator_test.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class TestGenerator : public ServiceCodeGenerator {
6363
using ServiceCodeGenerator::HasPaginatedMethod;
6464
using ServiceCodeGenerator::HasStreamingReadMethod;
6565
using ServiceCodeGenerator::HasStreamingWriteMethod;
66+
using ServiceCodeGenerator::IsDeprecated;
6667
using ServiceCodeGenerator::IsDiscoveryDocumentProto;
6768
using ServiceCodeGenerator::IsExperimental;
6869
using ServiceCodeGenerator::MethodSignatureWellKnownProtobufTypeIncludes;
@@ -1011,6 +1012,55 @@ TEST(PredicateUtilsTest, IsDiscoveryDocumentProto) {
10111012
EXPECT_FALSE(g3.IsDiscoveryDocumentProto());
10121013
}
10131014

1015+
TEST(ServiceCodeGeneratorTest, IsDeprecated) {
1016+
auto constexpr kServiceProto = R"""(
1017+
syntax = "proto3";
1018+
1019+
// This service that is deprecated.
1020+
service DeprecatedService {
1021+
option deprecated = true;
1022+
}
1023+
1024+
// This service that is NOT deprecated.
1025+
service NonDeprecatedService {
1026+
}
1027+
1028+
)""";
1029+
1030+
generator_testing::FakeSourceTree source_tree(
1031+
std::map<std::string, std::string>{
1032+
{"google/cloud/foo/service.proto", kServiceProto}});
1033+
google::protobuf::compiler::SourceTreeDescriptorDatabase source_tree_db(
1034+
&source_tree);
1035+
google::protobuf::SimpleDescriptorDatabase simple_db;
1036+
FileDescriptorProto file_proto;
1037+
// we need descriptor.proto to be accessible by the pool
1038+
// since our test file imports it
1039+
FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
1040+
simple_db.Add(file_proto);
1041+
google::protobuf::MergedDescriptorDatabase merged_db(&simple_db,
1042+
&source_tree_db);
1043+
generator_testing::ErrorCollector collector;
1044+
DescriptorPool pool(&merged_db, &collector);
1045+
1046+
FileDescriptor const* service_file_descriptor =
1047+
pool.FindFileByName("google/cloud/foo/service.proto");
1048+
1049+
auto generator_context = std::make_unique<MockGeneratorContext>();
1050+
EXPECT_CALL(*generator_context, Open("header_path"))
1051+
.Times(2)
1052+
.WillRepeatedly(
1053+
[](std::string const&) { return new MockZeroCopyOutputStream(); });
1054+
1055+
TestGenerator deprecated_service(service_file_descriptor->service(0),
1056+
generator_context.get());
1057+
EXPECT_TRUE(deprecated_service.IsDeprecated());
1058+
1059+
TestGenerator non_deprecated_service(service_file_descriptor->service(1),
1060+
generator_context.get());
1061+
EXPECT_FALSE(non_deprecated_service.IsDeprecated());
1062+
}
1063+
10141064
} // namespace
10151065
} // namespace generator_internal
10161066
} // namespace cloud

0 commit comments

Comments
 (0)