From b8a5458a7493de17516bdb172f6a3a208dfc5eed Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 7 Jan 2024 23:57:49 +0100 Subject: tools: Dangling changes --- modules/tools/cli_analyzer.hpp | 103 ++++++++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 17 deletions(-) (limited to 'modules/tools/cli_analyzer.hpp') diff --git a/modules/tools/cli_analyzer.hpp b/modules/tools/cli_analyzer.hpp index 295ddf6..7f3f013 100644 --- a/modules/tools/cli_analyzer.hpp +++ b/modules/tools/cli_analyzer.hpp @@ -21,13 +21,19 @@ struct cli_modifier { codec json; codec encoded; - error_or read(data& enc_data, std::deque& sch_path, std::string& json_data_str){ + error_or read( + std::deque& sch_path, + data& enc_data, + std::string& json_data_str + ){ data native; - { + if constexpr ( std::is_same_v ){ auto eov = encoded.decode(enc_data, native); if(eov.is_error()){ return eov; } + }else{ + native = enc_data; } { data json_data; @@ -37,12 +43,18 @@ struct cli_modifier { } json_data_str = convert_to_string(json_data.get_buffer()); + + std::cout< write(data& enc_data, std::deque& sch_path, std::string& json_data_str){ + error_or write( + std::deque& sch_path, + data& enc_data, + std::string& json_data_str + ){ data native; { /// @todo string to data @@ -52,35 +64,85 @@ struct cli_modifier { return eov; } } - { + if constexpr (std::is_same_v ){ auto eov = encoded.encode(native, enc_data); if(eov.is_error()){ return eov; } - }i + }else{ + enc_data = native; + } return void_t{}; - } }; +template +struct cli_traverser, Encoding> { + using Schema = schema::Tuple; + + template + static error_or traverse_member(std::deque& sch_path, data& enc_data, std::string& json_data){ + using Type = typename parameter_pack_type::type; + + std::string num_str = std::to_string(i); + if( num_str == sch_path.front() ){ + sch_path.pop_front(); + return cli_traverser::traverse(sch_path, enc_data.template get(), json_data); + } + + if ( (i+1) < sizeof...(T)){ + return traverse_member(sch_path, enc_data, json_data); + } + return make_error("Didn't find tuple path element"); + } +}; + +/** + * Traverse the path until we hit the end of the provided path + */ template struct cli_traverser...>, Encoding> { using Schema = schema::Struct...>; + template + static error_or traverse_member(std::deque& sch_path, data& enc_data, std::string& json_data){ + using Type = typename parameter_pack_type::type; + constexpr string_literal Literal = parameter_key_pack_type::literal; + + if ( Literal.view() == sch_path.front() ){ + sch_path.pop_front(); + return cli_traverser::traverse(sch_path, enc_data.template get(), json_data); + } + + if constexpr ( (i+1) < sizeof...(T) ) { + return traverse_member(sch_path, enc_data, json_data); + } + return make_error("Didn't find struct path element"); + } + template - static error_or traverse(std::deque& sch_path, std::string& json_data){ + static error_or traverse(std::deque& sch_path, data& enc_data, std::string& json_data){ + /** + * If our path is empty, then we have reached the desired destination. + */ if(sch_path.empty()){ + /** + * Decide during this step if we are reading or not + */ cli_modifier mod; if constexpr (std::is_same_v){ return mod.read(sch_path, json_data); } else if constexpr (std::is_same_v) { return mod.write(sch_path, json_data); } else { - return make_error(); + return make_error("We only support cli_mode::read and cli_mode::write"); } } else { - + if constexpr ( sizeof...(T) > 0 ){ + return traverse_member(sch_path, json_data); + } + return make_error("No elements in struct while path isn't empty"); } return void_t{}; @@ -88,22 +150,29 @@ struct cli_traverser...>, Encoding> { }; } -template -int modify_data_on_cli(int argc, char** argv){ +struct parsed_args { + bool read_mode = true; + std::string file_path; + std::deque sch_path; +}; - /// @todo parse cli data - bool read_mode = true; +template +int modify_data_on_cli(bool read_mode, const std::string& file_path, std::deque sch_path, std::string& json_data){ + /** + * Read data from file + */ + - std::deque sch_path; - std::string json_data; + data data; + data native_data; if (read_mode) { - auto eov = impl::cli_modifier::traverse(sch_path, json_data); + auto eov = impl::cli_modifier::traverse(sch_path, native_data, json_data); if(eov.is_error()){ return -1; } } else { - auto eov = impl::cli_modifier::traverse(sch_path, json_data); + auto eov = impl::cli_modifier::traverse(sch_path, native_data, json_data); if(eov.is_error()){ return -1; } -- cgit v1.2.3