Skip to content

Commit

Permalink
Add template comment and package to previously empty generated kotlin…
Browse files Browse the repository at this point in the history
… proto files.

We generate a Kotlin file to correspond to each message, which left the per-file generated file blank. We would still like to generate a file to deal with edge cases where there are no messages defined in a proto file. To fix, we print the default comment about the file being generated as well as a package statement.

PiperOrigin-RevId: 486969373
  • Loading branch information
deannagarcia authored and copybara-github committed Nov 8, 2022
1 parent 35993fe commit 9dfe7e9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/google/protobuf/compiler/java/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,23 @@ std::string FileGenerator::GetKotlinClassname() {
return name_resolver_->GetFileClassName(file_, immutable_api_, true);
}

void FileGenerator::GenerateKotlin(io::Printer* printer) {
printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"// source: $filename$\n"
"\n",
"filename", file_->name());
printer->Print(
"// Generated files should ignore deprecation warnings\n"
"@file:Suppress(\"DEPRECATION\")");
if (!java_package_.empty()) {
printer->Print(
"package $package$;\n"
"\n",
"package", EscapeKotlinKeywords(java_package_));
}
}

void FileGenerator::GenerateKotlinSiblings(
const std::string& package_dir, GeneratorContext* context,
std::vector<std::string>* file_list,
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/java/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class FileGenerator {
void Generate(io::Printer* printer);

std::string GetKotlinClassname();
void GenerateKotlin(io::Printer* printer);
void GenerateKotlinSiblings(const std::string& package_dir,
GeneratorContext* generator_context,
std::vector<std::string>* file_list,
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/compiler/java/kotlin_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ bool KotlinGenerator::Generate(const FileDescriptor* file,
output.get(), '$',
file_options.annotate_code ? &annotation_collector : nullptr);

file_generator->GenerateKotlin(&printer);

file_generator->GenerateKotlinSiblings(package_dir, context, &all_files,
&all_annotations);

Expand Down

0 comments on commit 9dfe7e9

Please sign in to comment.