From e29a213c288e34516a0834209de2bc3d6f215024 Mon Sep 17 00:00:00 2001 From: helintongh Date: Mon, 9 Dec 2024 15:17:46 +0800 Subject: [PATCH] [struct_pb] add namespace and nested message generator (#843) --- src/struct_pb/tools/proto_to_struct.cpp | 19 ++++++++++++++++++- src/struct_pb/tools/struct_code_generator.hpp | 10 ++++++++-- src/struct_pb/tools/struct_token.hpp | 1 + 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/struct_pb/tools/proto_to_struct.cpp b/src/struct_pb/tools/proto_to_struct.cpp index d4028d2ca..aded6e285 100644 --- a/src/struct_pb/tools/proto_to_struct.cpp +++ b/src/struct_pb/tools/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/src/struct_pb/tools/struct_code_generator.hpp b/src/struct_pb/tools/struct_code_generator.hpp index 7648850b5..80cd7305c 100644 --- a/src/struct_pb/tools/struct_code_generator.hpp +++ b/src/struct_pb/tools/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/src/struct_pb/tools/struct_token.hpp b/src/struct_pb/tools/struct_token.hpp index 61bfe4f41..055cafaf9 100644 --- a/src/struct_pb/tools/struct_token.hpp +++ b/src/struct_pb/tools/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();