add json comparator
This commit is contained in:
parent
5bec01b9df
commit
924f6521eb
@ -31,7 +31,7 @@ namespace trs
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename Key, typename Function>
|
template<typename Key, typename Function>
|
||||||
void register_function(const Key k, const Function f) noexcept
|
void registration(const Key k, const Function f) noexcept
|
||||||
{
|
{
|
||||||
m_functions[k] = f;
|
m_functions[k] = f;
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
#include "hack/exception/exception.hpp"
|
#include "hack/exception/exception.hpp"
|
||||||
|
|
||||||
namespace trs
|
namespace trs
|
||||||
@ -19,7 +16,7 @@ namespace trs
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename Key, typename Function>
|
template<typename Key, typename Function>
|
||||||
void register_function(const Key k, const Function f) noexcept
|
void registration(const Key k, const Function f) noexcept
|
||||||
{
|
{
|
||||||
m_functions[k] = f;
|
m_functions[k] = f;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
|
|
||||||
#include "httplib.h"
|
#include "httplib.h"
|
||||||
#include "hack/logger/logger.hpp"
|
#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/utils/var.hpp"
|
||||||
#include "trs/libs/transaction.hpp"
|
#include "trs/libs/transaction.hpp"
|
||||||
@ -36,8 +40,8 @@ namespace trs
|
|||||||
template<typename Function>
|
template<typename Function>
|
||||||
void registration(std::string func_name, Function insp, Function func)
|
void registration(std::string func_name, Function insp, Function func)
|
||||||
{
|
{
|
||||||
m_inspector.register_function(func_name, insp);
|
m_inspector.registration(func_name, insp);
|
||||||
m_function_manager.register_function(func_name, func);
|
m_function_manager.registration(func_name, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
void run()
|
void run()
|
||||||
@ -93,8 +97,8 @@ namespace trs
|
|||||||
catch(hack::exception& ex)
|
catch(hack::exception& ex)
|
||||||
{
|
{
|
||||||
ex.transaction(tr);
|
ex.transaction(tr);
|
||||||
|
ex.log();
|
||||||
ex.commit<database>();
|
ex.commit<database>();
|
||||||
ex.commit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.set_content(nlohmann::to_string(tr.m_data.m_result), var::HEADER_FLAG_JSON);
|
res.set_content(nlohmann::to_string(tr.m_data.m_result), var::HEADER_FLAG_JSON);
|
||||||
|
49
src/trs/utils/helpers.hpp
Normal file
49
src/trs/utils/helpers.hpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "hack/exception/exception.hpp"
|
||||||
|
#include "hack/string/string_concat_helper.hpp"
|
||||||
|
|
||||||
|
#include "using.hpp"
|
||||||
|
|
||||||
|
namespace trs::helpers
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
inline std::pair<bool, std::string> 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<typename Transaction>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,20 +1,19 @@
|
|||||||
#include "hack/exception/exception.hpp"
|
|
||||||
|
|
||||||
#include "trs/trs.hpp"
|
#include "trs/trs.hpp"
|
||||||
#include "trs/utils/var.hpp"
|
|
||||||
|
|
||||||
namespace worckspaces
|
namespace worckspaces
|
||||||
{
|
{
|
||||||
|
namespace json_data
|
||||||
|
{
|
||||||
|
inline auto healthcheck = R"(
|
||||||
|
{
|
||||||
|
"key": "value"
|
||||||
|
}
|
||||||
|
)"_json;
|
||||||
|
}
|
||||||
|
|
||||||
inline void inspector(trs::transaction& tr)
|
inline void inspector(trs::transaction& tr)
|
||||||
{
|
{
|
||||||
if (tr.m_data.m_payload.size() != 0)
|
trs::helpers::json_compare(tr, json_data::healthcheck);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void provider(trs::transaction& tr)
|
inline void provider(trs::transaction& tr)
|
||||||
@ -29,7 +28,6 @@ auto main(int argc, char* args[]) -> int
|
|||||||
{
|
{
|
||||||
trs::http app;
|
trs::http app;
|
||||||
app.init("app_connection", "log_connection");
|
app.init("app_connection", "log_connection");
|
||||||
|
|
||||||
app.registration("healthcheck", worckspaces::inspector, worckspaces::provider);
|
app.registration("healthcheck", worckspaces::inspector, worckspaces::provider);
|
||||||
app.run();
|
app.run();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user