improve deserialization and provide message comp deserl

This commit is contained in:
Green Sky 2024-02-15 18:34:52 +01:00
parent 0b0245d844
commit f6e55851cc
No known key found for this signature in database
2 changed files with 50 additions and 28 deletions

View File

@ -41,6 +41,11 @@ namespace Message::Components {
} // Message::Components } // Message::Components
namespace Fragment::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MessagesTSRange, begin, end)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MessagesContact, id)
} // Fragment::Components
template<typename T> template<typename T>
static bool serl_json_default(void* comp, nlohmann::json& out) { static bool serl_json_default(void* comp, nlohmann::json& out) {
if constexpr (!std::is_empty_v<T>) { if constexpr (!std::is_empty_v<T>) {
@ -64,19 +69,6 @@ static bool serl_json_msg_ts_range(void* comp, nlohmann::json& out) {
return true; return true;
} }
static bool deserl_json_msg_ts_range(FragmentHandle fh, const nlohmann::json& in) {
// TODO: this is ugly in multiple places
try {
fh.emplace_or_replace<FragComp::MessagesTSRange>(FragComp::MessagesTSRange{
in["begin"],
in["end"]
});
} catch(...) {
return false;
}
return true;
}
static bool serl_json_msg_c_id(void* comp, nlohmann::json& out) { static bool serl_json_msg_c_id(void* comp, nlohmann::json& out) {
if (comp == nullptr) { if (comp == nullptr) {
return false; return false;
@ -91,18 +83,6 @@ static bool serl_json_msg_c_id(void* comp, nlohmann::json& out) {
return true; return true;
} }
static bool deserl_json_msg_c_id(FragmentHandle fh, const nlohmann::json& in) {
// TODO: this is ugly in multiple places
try {
fh.emplace_or_replace<FragComp::MessagesContact>(FragComp::MessagesContact{
in["id"]
});
} catch(...) {
return false;
}
return true;
}
void MessageFragmentStore::handleMessage(const Message3Handle& m) { void MessageFragmentStore::handleMessage(const Message3Handle& m) {
if (!static_cast<bool>(m)) { if (!static_cast<bool>(m)) {
return; // huh? return; // huh?
@ -235,25 +215,37 @@ MessageFragmentStore::MessageFragmentStore(
_rmm.subscribe(this, RegistryMessageModel_Event::message_destroy); _rmm.subscribe(this, RegistryMessageModel_Event::message_destroy);
_fs._sc.registerSerializerJson<FragComp::MessagesTSRange>(serl_json_msg_ts_range); _fs._sc.registerSerializerJson<FragComp::MessagesTSRange>(serl_json_msg_ts_range);
_fs._sc.registerDeSerializerJson<FragComp::MessagesTSRange>(deserl_json_msg_ts_range); _fs._sc.registerDeSerializerJson<FragComp::MessagesTSRange>();
_fs._sc.registerSerializerJson<FragComp::MessagesContact>(serl_json_msg_c_id); _fs._sc.registerSerializerJson<FragComp::MessagesContact>(serl_json_msg_c_id);
_fs._sc.registerDeSerializerJson<FragComp::MessagesContact>(deserl_json_msg_c_id); _fs._sc.registerDeSerializerJson<FragComp::MessagesContact>();
_sc.registerSerializerJson<Message::Components::Timestamp>(serl_json_default<Message::Components::Timestamp>); _sc.registerSerializerJson<Message::Components::Timestamp>(serl_json_default<Message::Components::Timestamp>);
_sc.registerDeSerializerJson<Message::Components::Timestamp>();
_sc.registerSerializerJson<Message::Components::TimestampProcessed>(serl_json_default<Message::Components::TimestampProcessed>); _sc.registerSerializerJson<Message::Components::TimestampProcessed>(serl_json_default<Message::Components::TimestampProcessed>);
_sc.registerDeSerializerJson<Message::Components::TimestampProcessed>();
_sc.registerSerializerJson<Message::Components::TimestampWritten>(serl_json_default<Message::Components::TimestampWritten>); _sc.registerSerializerJson<Message::Components::TimestampWritten>(serl_json_default<Message::Components::TimestampWritten>);
_sc.registerDeSerializerJson<Message::Components::TimestampWritten>();
_sc.registerSerializerJson<Message::Components::ContactFrom>(serl_json_default<Message::Components::ContactFrom>); _sc.registerSerializerJson<Message::Components::ContactFrom>(serl_json_default<Message::Components::ContactFrom>);
_sc.registerDeSerializerJson<Message::Components::ContactFrom>();
_sc.registerSerializerJson<Message::Components::ContactTo>(serl_json_default<Message::Components::ContactTo>); _sc.registerSerializerJson<Message::Components::ContactTo>(serl_json_default<Message::Components::ContactTo>);
_sc.registerDeSerializerJson<Message::Components::ContactTo>();
_sc.registerSerializerJson<Message::Components::TagUnread>(serl_json_default<Message::Components::TagUnread>); _sc.registerSerializerJson<Message::Components::TagUnread>(serl_json_default<Message::Components::TagUnread>);
_sc.registerDeSerializerJson<Message::Components::TagUnread>();
_sc.registerSerializerJson<Message::Components::Read>(serl_json_default<Message::Components::Read>); _sc.registerSerializerJson<Message::Components::Read>(serl_json_default<Message::Components::Read>);
_sc.registerDeSerializerJson<Message::Components::Read>();
_sc.registerSerializerJson<Message::Components::MessageText>(serl_json_default<Message::Components::MessageText>); _sc.registerSerializerJson<Message::Components::MessageText>(serl_json_default<Message::Components::MessageText>);
_sc.registerDeSerializerJson<Message::Components::MessageText>();
_sc.registerSerializerJson<Message::Components::TagMessageIsAction>(serl_json_default<Message::Components::TagMessageIsAction>); _sc.registerSerializerJson<Message::Components::TagMessageIsAction>(serl_json_default<Message::Components::TagMessageIsAction>);
_sc.registerDeSerializerJson<Message::Components::TagMessageIsAction>();
// files // files
//_sc.registerSerializerJson<Message::Components::Transfer::FileID>() //_sc.registerSerializerJson<Message::Components::Transfer::FileID>()
_sc.registerSerializerJson<Message::Components::Transfer::FileInfo>(serl_json_default<Message::Components::Transfer::FileInfo>); _sc.registerSerializerJson<Message::Components::Transfer::FileInfo>(serl_json_default<Message::Components::Transfer::FileInfo>);
_sc.registerDeSerializerJson<Message::Components::Transfer::FileInfo>();
_sc.registerSerializerJson<Message::Components::Transfer::FileInfoLocal>(serl_json_default<Message::Components::Transfer::FileInfoLocal>); _sc.registerSerializerJson<Message::Components::Transfer::FileInfoLocal>(serl_json_default<Message::Components::Transfer::FileInfoLocal>);
_sc.registerDeSerializerJson<Message::Components::Transfer::FileInfoLocal>();
_sc.registerSerializerJson<Message::Components::Transfer::TagHaveAll>(serl_json_default<Message::Components::Transfer::TagHaveAll>); _sc.registerSerializerJson<Message::Components::Transfer::TagHaveAll>(serl_json_default<Message::Components::Transfer::TagHaveAll>);
_sc.registerDeSerializerJson<Message::Components::Transfer::TagHaveAll>();
_fs.scanStoragePath("test_message_store/"); _fs.scanStoragePath("test_message_store/");
} }

View File

@ -2,6 +2,7 @@
#include <entt/core/type_info.hpp> #include <entt/core/type_info.hpp>
#include <entt/container/dense_map.hpp> #include <entt/container/dense_map.hpp>
#include <entt/entity/handle.hpp>
#include <nlohmann/json_fwd.hpp> #include <nlohmann/json_fwd.hpp>
@ -16,12 +17,41 @@ struct SerializerCallbacks {
using deserialize_json_fn = bool(*)(FragmentHandle fh, const nlohmann::json& in); using deserialize_json_fn = bool(*)(FragmentHandle fh, const nlohmann::json& in);
entt::dense_map<entt::id_type, deserialize_json_fn> _deserl_json; entt::dense_map<entt::id_type, deserialize_json_fn> _deserl_json;
template<typename T>
static bool component_emplace_or_replace_json(FragmentHandle fh, const nlohmann::json& j) {
if constexpr (std::is_empty_v<T>) {
fh.emplace_or_replace<T>(); // assert empty json?
} else {
fh.emplace_or_replace<T>(static_cast<T>(j));
}
return true;
}
template<typename T>
static bool component_get_json(const FragmentHandle fh, nlohmann::json& j) {
if (fh.all_of<T>()) {
if constexpr (!std::is_empty_v<T>) {
j = fh.get<T>();
}
return true;
}
return false;
}
void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info); void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info);
template<typename CompType> template<typename CompType>
void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info = entt::type_id<CompType>()) { registerSerializerJson(fn, type_info); } void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info = entt::type_id<CompType>()) { registerSerializerJson(fn, type_info); }
void registerDeSerializerJson(deserialize_json_fn fn, const entt::type_info& type_info); void registerDeSerializerJson(deserialize_json_fn fn, const entt::type_info& type_info);
template<typename CompType> template<typename CompType>
void registerDeSerializerJson(deserialize_json_fn fn, const entt::type_info& type_info = entt::type_id<CompType>()) { registerDeSerializerJson(fn, type_info); } void registerDeSerializerJson(
deserialize_json_fn fn = component_emplace_or_replace_json<CompType>,
const entt::type_info& type_info = entt::type_id<CompType>()
) {
registerDeSerializerJson(fn, type_info);
}
}; };