Skip to content

Commit

Permalink
AVRO-4035 [C++] Add doc strings to generated classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Aug 30, 2024
1 parent 8fad7dd commit 3c6390d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lang/c++/impl/avrogencpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class CodeGen {
void generateTraits(const NodePtr &n);
void generateRecordTraits(const NodePtr &n);
void generateUnionTraits(const NodePtr &n);
void generateDocComment(const NodePtr &n, const char *indent = "");
void emitCopyright();
void emitGeneratedWarning();

Expand Down Expand Up @@ -253,6 +254,7 @@ string CodeGen::generateRecordType(const NodePtr &n) {
return it->second;
}

generateDocComment(n);
os_ << "struct " << decoratedName << " {\n";
if (!noUnion_) {
for (size_t i = 0; i < c; ++i) {
Expand All @@ -271,6 +273,7 @@ string CodeGen::generateRecordType(const NodePtr &n) {
// the nameAt(i) does not take c++ reserved words into account
// so we need to call decorate on it
std::string decoratedNameAt = decorate(n->nameAt(i));
generateDocComment(n->leafAt(i), " ");
os_ << " " << types[i];
os_ << ' ' << decoratedNameAt << ";\n";
}
Expand Down Expand Up @@ -409,7 +412,7 @@ string CodeGen::generateUnionType(const NodePtr &n) {
for (size_t i = 0; i < c; ++i) {
// escape reserved literals for c++
auto branch_name = decorate(names[i]);
// avoid rare collisions, e.g. somone might name their struct int_
// avoid rare collisions, e.g. someone might name their struct int_
if (used_branch_names.find(branch_name) != used_branch_names.end()) {
size_t postfix = 2;
std::string escaped_name = branch_name + "_" + std::to_string(postfix);
Expand Down Expand Up @@ -739,6 +742,16 @@ void CodeGen::generateTraits(const NodePtr &n) {
}
}

void CodeGen::generateDocComment(const NodePtr &n, const char *indent) {
if (!n->getDoc().empty()) {
std::vector<std::string> lines;
boost::algorithm::split(lines, n->getDoc(), boost::algorithm::is_any_of("\n"));
for (auto &line : lines) {
os_ << indent << "// " << line << "\n";
}
}
}

void CodeGen::emitCopyright() {
os_ << "/**\n"
" * Licensed to the Apache Software Foundation (ASF) under one\n"
Expand Down Expand Up @@ -954,4 +967,4 @@ bool UnionCodeTracker::unionTraitsAlreadyGenerated(const std::string &unionClass

void UnionCodeTracker::setTraitsGenerated(const std::string &unionClassName) {
generatedUnionTraits_.insert(unionClassName);
}
}
2 changes: 1 addition & 1 deletion lang/c++/jsonschemas/bigrecord
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"type": "record",
"doc": "Top level Doc.",
"doc": "Top level Doc.\nWith multiple lines",
"name": "RootRecord",
"fields": [
{
Expand Down

0 comments on commit 3c6390d

Please sign in to comment.