From 57c8ff05f4b19762b6915aac83c1245a54ee7f42 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 2 Apr 2024 17:48:37 +0200 Subject: tools, codec-json: Initial whitespace is also valid json. Also fixed reading for cli manip as well --- modules/codec-json/c++/json.hpp | 1 + modules/tools/c++/cli_analyzer.hpp | 86 ++++++++++++++++++++++++++++++++------ modules/tools/examples/cli_mod.cpp | 73 ++++++++++++++++++++++++++++++-- modules/tools/examples/foo.json | 1 + 4 files changed, 144 insertions(+), 17 deletions(-) create mode 100644 modules/tools/examples/foo.json (limited to 'modules') diff --git a/modules/codec-json/c++/json.hpp b/modules/codec-json/c++/json.hpp index b77f328..034585c 100644 --- a/modules/codec-json/c++/json.hpp +++ b/modules/codec-json/c++/json.hpp @@ -117,6 +117,7 @@ public: error_or decode(data& from_decode, data& to_decode){ buffer_view buff_v{from_decode.get_buffer()}; + impl::json_helper::skip_whitespace(buff_v); auto eov = impl::json_decode::decode(buff_v, to_decode); if(eov.is_error()){ return std::move(eov.get_error()); diff --git a/modules/tools/c++/cli_analyzer.hpp b/modules/tools/c++/cli_analyzer.hpp index 4aa3d6c..9a0998b 100644 --- a/modules/tools/c++/cli_analyzer.hpp +++ b/modules/tools/c++/cli_analyzer.hpp @@ -6,6 +6,7 @@ #include #include +#include #include namespace saw { @@ -42,8 +43,6 @@ struct cli_modifier { } json_data_str = convert_to_string(json_data.get_buffer()); - - std::cout< +struct cli_traverser { + using Schema = schema::String; + + template + static error_or traverse(std::deque& sch_path, data& enc_data, std::string& json_data){ + if(not sch_path.empty()){ + return make_error("Schema path too long"); + } + + cli_modifier mod; + if constexpr (std::is_same_v){ + return mod.read(sch_path, enc_data, json_data); + } else if constexpr (std::is_same_v) { + return mod.write(sch_path, enc_data, json_data); + } else { + return make_error("We only support cli_mode::read and cli_mode::write"); + } + } + +}; + template struct cli_traverser, Encoding> { using Schema = schema::Tuple; @@ -146,27 +167,66 @@ struct parsed_args { }; template -int modify_data_on_cli(bool read_mode, const std::string_view& file_path, std::deque sch_path, std::string& json_data){ - /** +error_or modify_data_on_cli(bool read_mode, const std::string_view& file_path, std::deque sch_path, std::string& json_data){ + /** * Read data from file */ - + std::string file_data = [&]() -> std::string { + std::fstream fstr{std::string{file_path}, fstr.binary | fstr.in}; + if(!fstr.is_open()){ + return {}; + } + std::stringstream sstr; + sstr << fstr.rdbuf(); + return sstr.str(); + }(); + if(file_data.empty()){ + return make_error("File exists, but is empty."); + } + data enc_data{std::string_view{file_data}}; + codec enc_codec; - data enc_data; data native_data; + { + auto eov = enc_codec.template decode(enc_data, native_data); + if(eov.is_error()){ + return eov; + } + } if (read_mode) { - auto eov = impl::cli_traverser::template traverse(sch_path, native_data, json_data); - if(eov.is_error()){ - return -1; + { + auto eov = impl::cli_traverser::template traverse(sch_path, native_data, json_data); + if(eov.is_error()){ + return eov; + } } } else { - auto eov = impl::cli_traverser::template traverse(sch_path, native_data, json_data); - if(eov.is_error()){ - return -1; + { + auto eov = impl::cli_traverser::template traverse(sch_path, native_data, json_data); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = enc_codec.template encode(native_data, enc_data); + if(eov.is_error()){ + return eov; + } + } + { + std::fstream fstr{std::string{file_path}, fstr.binary | fstr.out | fstr.trunc }; + if(!fstr.is_open()){ + return make_error("Couldn't open file"); + } + std::stringstream sstr; + std::string enc_str = convert_to_string(enc_data.get_buffer()); + + fstr << enc_str; + fstr.close(); } } - return 0; + return void_t{}; } } diff --git a/modules/tools/examples/cli_mod.cpp b/modules/tools/examples/cli_mod.cpp index 42d4370..45114a0 100644 --- a/modules/tools/examples/cli_mod.cpp +++ b/modules/tools/examples/cli_mod.cpp @@ -8,16 +8,59 @@ namespace schema { using namespace saw::schema; +using AnalyzeInner = Struct< + Member, + Member +>; + using AnalyzeTest = Struct< - Member + Member, + Member >; } +std::deque split_schema_path(const std::string_view& sch_arg){ + std::deque split; + + bool escape = false; + uint64_t last_pick = 0; + for(uint64_t i = 0; i < sch_arg.size(); ++i){ + if(escape){ + escape = false; + } else { + if(sch_arg.at(i) == '\\'){ + escape = true; + }else if(sch_arg.at(i) == '.'){ + std::string sub_str{sch_arg.substr(last_pick, i - last_pick)}; + if(sub_str.empty()){ + /// @todo return error + return {}; + } + split.emplace_back(std::move(sub_str)); + last_pick = i+1u; + } + } + } + + if( ( sch_arg.size() == last_pick ) || escape ){ + return {}; + } + + std::string sub_str{sch_arg.substr(last_pick)}; + split.emplace_back(std::move(sub_str)); + + return split; +} + int main(int argc, char** argv){ /** * Basic checking and wrapping args into an array of string_view + * 1. mode + * 2. file_path + * 3. schema_path + * 4. data if it's write */ - int min_amount_args = 2; + int min_amount_args = 3; if( argc < (min_amount_args + 1) ) { std::cerr<<"Not enough arguments"<(argc) ) ){ + std::cerr<<"Invalid amount argument. Need "< bool { if(args_view.at(1) == "w"){ return false; @@ -55,12 +103,29 @@ int main(int argc, char** argv){ }(); std::string json_data; - std::deque sch_path; - int rc = saw::modify_data_on_cli(is_read_mode, args_view.at(2), sch_path, json_data); + if ( not is_read_mode ){ + json_data = std::string{args_view.at(4)}; + } + + std::deque sch_path = split_schema_path(args_view.at(3)); + { + auto eov = saw::modify_data_on_cli(is_read_mode, args_view.at(2), sch_path, json_data); + if(eov.is_error()){ + auto& err = eov.get_error(); + std::cerr<<"Modification failed: "<