From 924f6521eb6d31513e02448205eb84f744c1e636 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Wed, 10 Jul 2024 11:05:21 +0300 Subject: [PATCH] add json comparator --- src/trs/libs/function_manager.hpp | 2 +- src/trs/libs/inspector.hpp | 5 +--- src/trs/trs.hpp | 10 +++++-- src/trs/utils/helpers.hpp | 49 +++++++++++++++++++++++++++++++ tests/main.cpp | 22 +++++++------- 5 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 src/trs/utils/helpers.hpp diff --git a/src/trs/libs/function_manager.hpp b/src/trs/libs/function_manager.hpp index e5bae1b..51dd71b 100644 --- a/src/trs/libs/function_manager.hpp +++ b/src/trs/libs/function_manager.hpp @@ -31,7 +31,7 @@ namespace trs } template - void register_function(const Key k, const Function f) noexcept + void registration(const Key k, const Function f) noexcept { m_functions[k] = f; } diff --git a/src/trs/libs/inspector.hpp b/src/trs/libs/inspector.hpp index 23ac23e..d5a4905 100644 --- a/src/trs/libs/inspector.hpp +++ b/src/trs/libs/inspector.hpp @@ -1,9 +1,6 @@ #pragma once -#include #include -#include - #include "hack/exception/exception.hpp" namespace trs @@ -19,7 +16,7 @@ namespace trs public: template - void register_function(const Key k, const Function f) noexcept + void registration(const Key k, const Function f) noexcept { m_functions[k] = f; } diff --git a/src/trs/trs.hpp b/src/trs/trs.hpp index 42c1e81..166c55f 100644 --- a/src/trs/trs.hpp +++ b/src/trs/trs.hpp @@ -4,6 +4,10 @@ #include "httplib.h" #include "hack/logger/logger.hpp" +#include "hack/exception/exception.hpp" + +#include "trs/utils/define.hpp" +#include "trs/utils/helpers.hpp" #include "trs/utils/var.hpp" #include "trs/libs/transaction.hpp" @@ -36,8 +40,8 @@ namespace trs template void registration(std::string func_name, Function insp, Function func) { - m_inspector.register_function(func_name, insp); - m_function_manager.register_function(func_name, func); + m_inspector.registration(func_name, insp); + m_function_manager.registration(func_name, func); } void run() @@ -93,8 +97,8 @@ namespace trs catch(hack::exception& ex) { ex.transaction(tr); + ex.log(); ex.commit(); - ex.commit(); } res.set_content(nlohmann::to_string(tr.m_data.m_result), var::HEADER_FLAG_JSON); diff --git a/src/trs/utils/helpers.hpp b/src/trs/utils/helpers.hpp new file mode 100644 index 0000000..c62a015 --- /dev/null +++ b/src/trs/utils/helpers.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include +#include + +#include "hack/exception/exception.hpp" +#include "hack/string/string_concat_helper.hpp" + +#include "using.hpp" + +namespace trs::helpers +{ + namespace + { + inline std::pair json_compare_impl(JSON& target, JSON& comp) + { + if (target.empty()) + return { false, "payload is empty" }; + + for (auto&& [key, comp_v] : comp.items()) + { + if (comp_v.is_object()) + { + auto [ok, k] = json_compare_impl(target[key], comp_v); + if (!ok) return { ok, k }; + } + + if (target[key].type_name() != comp_v.type_name()) + return { false, hack::string::str_concat + "field - [" + key + "] is invalid."}; + } + + if (target.size() != comp.size()) return { false, "payload not competed" }; + + return { true, "" }; + } + } + + template + inline void json_compare(Transaction& base, JSON& comp) + { + auto [ok, msg] = json_compare_impl(base.m_data.m_payload, comp); + if (!ok) + { + hack::exception ex; + ex.description(msg); + throw ex; + } + } +} diff --git a/tests/main.cpp b/tests/main.cpp index ea52e70..7d80ed9 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,20 +1,19 @@ -#include "hack/exception/exception.hpp" - #include "trs/trs.hpp" -#include "trs/utils/var.hpp" namespace worckspaces { + namespace json_data + { + inline auto healthcheck = R"( + { + "key": "value" + } + )"_json; + } + inline void inspector(trs::transaction& tr) { - if (tr.m_data.m_payload.size() != 0) - { - hack::exception ex; - ex.description("transaction payload size > 0 but payload must be empty"); - ex.message(trs::var::NO_VALID_DATA); - ex.commit(); - throw ex; - } + trs::helpers::json_compare(tr, json_data::healthcheck); } inline void provider(trs::transaction& tr) @@ -29,7 +28,6 @@ auto main(int argc, char* args[]) -> int { trs::http app; app.init("app_connection", "log_connection"); - app.registration("healthcheck", worckspaces::inspector, worckspaces::provider); app.run(); }