From 55672732cb0c05d47ab9ea129713f8350e6abcd5 Mon Sep 17 00:00:00 2001 From: helintong Date: Thu, 5 Dec 2024 19:52:16 +0800 Subject: [PATCH] feat: add namespace and nested message generator --- tool/proto_to_struct.cpp | 19 ++++++++++++++++++- tool/struct_code_generator.hpp | 10 ++++++++-- tool/struct_token.hpp | 1 + 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/tool/proto_to_struct.cpp b/tool/proto_to_struct.cpp index d4028d2c..aded6e28 100644 --- a/tool/proto_to_struct.cpp +++ b/tool/proto_to_struct.cpp @@ -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; @@ -65,10 +67,21 @@ class struct_code_generator : public google::protobuf::compiler::CodeGenerator { std::vector proto_module_info; std::vector 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); @@ -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()); @@ -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; diff --git a/tool/struct_code_generator.hpp b/tool/struct_code_generator.hpp index 7648850b..80cd7305 100644 --- a/tool/struct_code_generator.hpp +++ b/tool/struct_code_generator.hpp @@ -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 \n\n"; + + result.append("namespace "); + result.append(package_name); + result.append(" {\n\n"); return result; } @@ -263,4 +267,6 @@ std::string code_generate_enum(const struct_enum &enum_inst) { result.append("};\n\n"); } return result; -} \ No newline at end of file +} + +std::string code_generate_last_part() { return "}"; } \ No newline at end of file diff --git a/tool/struct_token.hpp b/tool/struct_token.hpp index 61bfe4f4..055cafaf 100644 --- a/tool/struct_token.hpp +++ b/tool/struct_token.hpp @@ -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();