Skip to content

Commit

Permalink
[struct_pb] add namespace and nested message generator (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh authored Dec 9, 2024
1 parent 73577fe commit e29a213
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/struct_pb/tools/proto_to_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class struct_code_generator : public google::protobuf::compiler::CodeGenerator {

auto output = context->Open(filename);

std::string package_name = file->package();

bool enable_optional = false;
bool enable_inherit = false;

Expand All @@ -65,10 +67,21 @@ class struct_code_generator : public google::protobuf::compiler::CodeGenerator {

std::vector<struct_tokenizer> proto_module_info;
std::vector<struct_enum> proto_enum_info;

for (int i = 0; i < file->message_type_count(); ++i) {
// struct name
const google::protobuf::Descriptor* descriptor = file->message_type(i);

for (int k = 0; k < descriptor->nested_type_count(); ++k) {
struct_tokenizer tokenizer_nested;
tokenizer_nested.clear();

const google::protobuf::Descriptor* nested_type =
descriptor->nested_type(k);
tokenizer_nested.tokenizer(nested_type);
proto_module_info.emplace_back(tokenizer_nested);
}

struct_enum enum_token;
enum_token.clear();
enum_token.get_enum_fields(descriptor);
Expand All @@ -80,7 +93,7 @@ class struct_code_generator : public google::protobuf::compiler::CodeGenerator {
proto_module_info.emplace_back(tokenizer);
}

std::string struct_header = code_generate_header();
std::string struct_header = code_generate_header(package_name);
write_to_output(zero_copy_output, (const void*)struct_header.c_str(),
struct_header.size());

Expand Down Expand Up @@ -122,6 +135,10 @@ class struct_code_generator : public google::protobuf::compiler::CodeGenerator {
write_to_output(zero_copy_output, (const void*)struct_macro_str.c_str(),
struct_macro_str.size());
}
std::string namespace_last_part = "";
namespace_last_part = code_generate_last_part();
write_to_output(zero_copy_output, (const void*)namespace_last_part.c_str(),
namespace_last_part.size());

delete zero_copy_output;
return true;
Expand Down
10 changes: 8 additions & 2 deletions src/struct_pb/tools/struct_code_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

char parameter_value[27] = "abcdefghijklmnopqrstuvwxyz";

std::string code_generate_header() {
std::string code_generate_header(const std::string &package_name) {
std::string result = "#pragma once\n#include <ylt/struct_pb.hpp>\n\n";

result.append("namespace ");
result.append(package_name);
result.append(" {\n\n");
return result;
}

Expand Down Expand Up @@ -263,4 +267,6 @@ std::string code_generate_enum(const struct_enum &enum_inst) {
result.append("};\n\n");
}
return result;
}
}

std::string code_generate_last_part() { return "}"; }
1 change: 1 addition & 0 deletions src/struct_pb/tools/struct_token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class struct_tokenizer {
for (int j = 0; j < descriptor->field_count(); ++j) {
struct_token token = {};
const google::protobuf::FieldDescriptor* field = descriptor->field(j);

token.var_name = field->name();
if (field->type() == google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
token.type_name = field->message_type()->name();
Expand Down

0 comments on commit e29a213

Please sign in to comment.