From cfb4e481b4ad37b33c2bc7660299e6fac7e8afb6 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 28 Mar 2024 12:49:43 +0100 Subject: [PATCH] Implemented auto documentation --- .../include/net/http_server_handlers_map2.h | 1 + .../serialization/keyvalue_serialization.h | 39 ++++++++++++++----- .../keyvalue_serialization_overloads.h | 23 ++++++++--- src/rpc/core_rpc_server_commands_defs.h | 14 ++++--- 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/contrib/epee/include/net/http_server_handlers_map2.h b/contrib/epee/include/net/http_server_handlers_map2.h index 50ab3e30..b7ad8799 100644 --- a/contrib/epee/include/net/http_server_handlers_map2.h +++ b/contrib/epee/include/net/http_server_handlers_map2.h @@ -40,6 +40,7 @@ typename_t get_documentation_json_struct() return AUTO_VAL_INIT_T(typename_t); } + template bool auto_doc_t(const std::string& prefix_name, std::string& generate_reference) { diff --git a/contrib/epee/include/serialization/keyvalue_serialization.h b/contrib/epee/include/serialization/keyvalue_serialization.h index 856ee830..e3a48a34 100644 --- a/contrib/epee/include/serialization/keyvalue_serialization.h +++ b/contrib/epee/include/serialization/keyvalue_serialization.h @@ -39,9 +39,9 @@ namespace epee #define BEGIN_KV_SERIALIZE_MAP() \ public: \ template \ - bool store(t_storage& st, typename t_storage::hsection hparent_section = nullptr, bool auto_doc_mode = false) const\ + bool store(t_storage& st, typename t_storage::hsection hparent_section = nullptr) const\ {\ - return serialize_map(*this, st, hparent_section, auto_doc_mode); \ + return serialize_map(*this, st, hparent_section); \ }\ template \ bool _load(t_storage& stg, typename t_storage::hsection hparent_section = nullptr)\ @@ -62,7 +62,7 @@ public: \ }\ }\ template \ - static bool serialize_map(this_type& this_ref, t_storage& stg, typename t_storage::hsection hparent_section, bool auto_doc_mode = false) \ + static bool serialize_map(this_type& this_ref, t_storage& stg, typename t_storage::hsection hparent_section) \ { #define KV_SERIALIZE_N(varialble, val_name) \ @@ -72,22 +72,43 @@ public: \ epee::serialization::selector::serialize(this_ref.varialble, stg, hparent_section, val_name, auto_doc_mode, substitute, description); #define KV_SERIALIZE_N_DOC2(varialble, val_name) \ - {using var_type = decltype(this_ref.varialble); \ + { using var_type = decltype(this_ref.varialble); \ epee::serialization::selector::serialize(this_ref.varialble, stg, hparent_section, val_name); \ if constexpr (t_storage::use_descriptions::value) \ { \ - epee::serialization::selector::set_descr(this_ref.varialble, stg, hparent_section, val_name); \ + epee::serialization::selector::serialize_and_doc(stg, hparent_section, val_name + +/* + {using var_type = decltype(this_ref.varialble); \ + epee::serialization::selector::serialize(this_ref.varialble, stg, hparent_section, val_name); \ + if constexpr (t_storage::use_descriptions::value) \ + { \ + epee::serialization::selector::set_descr(stg, hparent_section, val_name, description, default = var_type()); \ } \ } - +*/ +#define DOC_DSCR(description) , description +#define DOC_EXMP(substitute) , substitute +#define DOC_EXMP_AUTO_1(arg_1) , var_type(arg_1) +#define DOC_EXMP_AUTO_2(arg_1, arg_2) , var_type(arg_1, arg_2) +#define DOC_END ); } } +#define DOC_EXMP_AUTO(...) epee::create_t_object(__VA_ARGS__) + + +// Function template to create an object with forwarded constructor arguments + template + T create_t_object(Args&&... args) { + return T(std::forward(args)...); + } + //substitute, description); -#define DOC_EXAMPLE(substitute) substitute, -#define DOC_EX(substitute__) var_type(substitute__), -#define DOC_DSCR(description) description); } +//#define DOC_EXAMPLE(substitute) substitute, +//#define DOC_EX(substitute__) var_type(substitute__), +//#define DOC_COMMAND(command_general_description) static const char* explain_yourseflf = command_general_description; #define KV_SERIALIZE_CUSTOM_N(varialble, stored_type, from_v_to_stored, from_stored_to_v, val_name) \ diff --git a/contrib/epee/include/serialization/keyvalue_serialization_overloads.h b/contrib/epee/include/serialization/keyvalue_serialization_overloads.h index 7ca5dd98..bd4eb2fe 100644 --- a/contrib/epee/include/serialization/keyvalue_serialization_overloads.h +++ b/contrib/epee/include/serialization/keyvalue_serialization_overloads.h @@ -317,15 +317,26 @@ namespace epee template static bool serialize(const t_type& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) { - stg.set_entry_description(hparent_section, pname, description); - return kv_serialize( (doc_mode ? doc_substitute:d), stg, hparent_section, pname); + if constexpr (!t_storage::use_descriptions::value) + { + return kv_serialize(d, stg, hparent_section, pname); + } + else + return false; + } - //bool doc_mode = false, const t_type& doc_substitute = t_type(), const std::string& description = std::string() + //const t_type& doc_substitute = t_type(), const std::string& description = std::string() template - static bool set_descr(t_storage& stg, typename t_storage::hsection hparent_section, const char* pname, const std::string& description = std::string(), const t_type& doc_substitute = t_type()) + static bool serialize_and_doc(t_storage& stg, typename t_storage::hsection hparent_section, const char* pname, const std::string& description = std::string(), const t_type& doc_substitute = t_type()) { - stg.set_entry_description(hparent_section, pname, description); - return kv_serialize((doc_mode ? doc_substitute : d), stg, hparent_section, pname); + if constexpr (t_storage::use_descriptions::value) + { + stg.set_entry_description(hparent_section, pname, description); + return kv_serialize(doc_substitute, stg, hparent_section, pname); + } + else + return false; + } template diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index 66b34896..4ad4d674 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -137,6 +137,9 @@ namespace currency struct COMMAND_RPC_GET_HEIGHT { + DOC_COMMAND("Return current blockchain height"); + + struct request { BEGIN_KV_SERIALIZE_MAP() @@ -149,8 +152,8 @@ namespace currency std::string status; BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE_DOC2(height) DOC_EX(11111) DOC_DSCR("Some height of the block") - KV_SERIALIZE_DOC2(status) DOC_EX("OK") DOC_DSCR("Status of the operation") + KV_SERIALIZE_DOC2(height) DOC_DSCR("Some height of the block") DOC_EXMP(11111) DOC_END + KV_SERIALIZE_DOC2(status) DOC_DSCR("Status of the operation") DOC_EXMP("OK") DOC_END END_KV_SERIALIZE_MAP() }; }; @@ -202,7 +205,6 @@ namespace currency END_KV_SERIALIZE_MAP() }; - struct response { std::list txs_as_hex; //transactions blobs as hex @@ -210,9 +212,9 @@ namespace currency std::string status; BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE_DOC(txs_as_hex, std::list(1, "97d91442f8f3c22683585eaa60b53757d49bf046a96269cef45c1bc9ff7300cc97d914497d91442f8f3c22683585eaa60b53757d49bf046a96269cef45c1bc9ff7300cc2f8f3c22683585eaa60b53757d49bf046a96269cef45c1bc9ff7300cc"), "Transactions stored as blobs") - KV_SERIALIZE_DOC(missed_tx, std::list(1, "97d91442f8f3c22683585eaa60b53757d49bf046a96269cef45c1bc9ff7300cc"), "Missed transactions hashes") - KV_SERIALIZE_DOC(status, std::string("OK"), "Command response status") + KV_SERIALIZE_DOC2(txs_as_hex) DOC_DSCR("Transactions stored as blobs") DOC_EXMP_AUTO(1, "7d914497d91442f8f3c2268397d914497d91442f8f3c22683585eaa60b53757d49bf046a96269cef45c1bc9ff7300cc2f8f3c22683585eaa60b53757d49bf046a96269cef45c1bc9ff7300cc") DOC_END + KV_SERIALIZE_DOC2(missed_tx) DOC_DSCR("Missed transactions hashes") DOC_EXMP_AUTO(1, "97d91442f8f3c22683585eaa60b53757d49bf046a96269cef45c1bc9ff7300cc") DOC_END + KV_SERIALIZE_DOC2(status) DOC_DSCR("Command response status") DOC_EXMP_AUTO("OK") DOC_END END_KV_SERIALIZE_MAP() }; };