From 5d8aa8aef9aab3a1508feae9c37b4e8c3fb7f0c7 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 27 Oct 2024 14:51:14 +0100 Subject: [PATCH] add message file object --- .../message3/message_serializer.cpp | 49 +++++++++++++++++++ .../message3/message_serializer.hpp | 9 ++++ .../message3/nj/message_components.hpp | 8 +-- .../nj/message_components_serializer.cpp | 10 +--- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/solanaceae/message3/message_serializer.cpp b/src/solanaceae/message3/message_serializer.cpp index 5d2baa7..d2cf197 100644 --- a/src/solanaceae/message3/message_serializer.cpp +++ b/src/solanaceae/message3/message_serializer.cpp @@ -2,6 +2,7 @@ #include #include +#include #include @@ -52,6 +53,37 @@ Contact3 MessageSerializerNJ::deserlContactByID(const nlohmann::json& j) { return other_c; } +nlohmann::json MessageSerializerNJ::serlFileObjByID(ObjectHandle o) const { + if (!o) { + std::cerr << "MSC warning: encountered invalid file object\n"; + return nullptr; + } + + auto* id_comp = o.try_get(); + if (id_comp == nullptr || id_comp->v.empty()) { + std::cerr << "MSC warning: encountered file object without ID\n"; + return nullptr; // throw instead?? + } + + return nlohmann::json::binary(id_comp->v); +} + +ObjectHandle MessageSerializerNJ::deserlFileObjByID(const nlohmann::json& j) { + std::vector id; + if (j.is_binary()) { + id = j.get_binary(); + } else { + j["bytes"].get_to(id); + } + + auto o = os.getOneObjectByID(ByteSpan{id}); + if (!o) { + // TODO: create empty objet with id, others need to merge if it exist? special tag to prevent arb merging?? + } + + return o; +} + template<> bool MessageSerializerNJ::component_get_json(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) { const Contact3 c = h.get().c; @@ -154,3 +186,20 @@ bool MessageSerializerNJ::component_emplace_or_replace_json +bool MessageSerializerNJ::component_get_json(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) { + const auto& comp = h.get(); + + return false; +} + +template<> +bool MessageSerializerNJ::component_emplace_or_replace_json(MessageSerializerNJ& msc, Handle h, const nlohmann::json& j) { + if (j.is_null()) { + std::cerr << "MSC warning: encountered null MessageFileObject\n"; + h.emplace_or_replace(); + return true; + } + + return false; +} diff --git a/src/solanaceae/message3/message_serializer.hpp b/src/solanaceae/message3/message_serializer.hpp index b9b80ac..9d96cc4 100644 --- a/src/solanaceae/message3/message_serializer.hpp +++ b/src/solanaceae/message3/message_serializer.hpp @@ -4,6 +4,7 @@ #include #include +#include #include @@ -14,6 +15,7 @@ struct MessageSerializerNJ { static constexpr const char* version {"1"}; Contact3Registry& cr; + ObjectStore2& os; // nlohmann // json/msgpack @@ -76,6 +78,8 @@ struct MessageSerializerNJ { // helper nlohmann::json serlContactByID(Contact3 c) const; Contact3 deserlContactByID(const nlohmann::json& j); + nlohmann::json serlFileObjByID(ObjectHandle o) const; + ObjectHandle deserlFileObjByID(const nlohmann::json& j); }; // fwd @@ -83,6 +87,7 @@ namespace Message::Components { struct ContactFrom; struct ContactTo; struct ReceivedBy; +struct MessageFileObject; } // make specializations known @@ -98,3 +103,7 @@ template<> bool MessageSerializerNJ::component_get_json(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j); template<> bool MessageSerializerNJ::component_emplace_or_replace_json(MessageSerializerNJ& msc, Handle h, const nlohmann::json& j); +template<> +bool MessageSerializerNJ::component_get_json(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j); +template<> +bool MessageSerializerNJ::component_emplace_or_replace_json(MessageSerializerNJ& msc, Handle h, const nlohmann::json& j); diff --git a/src/solanaceae/message3/nj/message_components.hpp b/src/solanaceae/message3/nj/message_components.hpp index a9db939..c30872c 100644 --- a/src/solanaceae/message3/nj/message_components.hpp +++ b/src/solanaceae/message3/nj/message_components.hpp @@ -21,13 +21,7 @@ namespace Message::Components { // SyncedBy NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MessageText, text) - - // TODO: remove - //namespace Transfer { - //NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FileInfo::FileDirEntry, file_name, file_size) - //NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FileInfo, file_list, total_size) - //NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FileInfoLocal, file_list) - //} // Transfer + // file is special } // Message::Components diff --git a/src/solanaceae/message3/nj/message_components_serializer.cpp b/src/solanaceae/message3/nj/message_components_serializer.cpp index 8d17269..fbdd4b0 100644 --- a/src/solanaceae/message3/nj/message_components_serializer.cpp +++ b/src/solanaceae/message3/nj/message_components_serializer.cpp @@ -27,13 +27,7 @@ void registerMessageComponents(MessageSerializerNJ& msnj) { msnj.registerSerializer(); msnj.registerDeserializer(); - // files - //_sc.registerSerializerJson() - //_sc.registerSerializerJson(); - //_sc.registerDeSerializerJson(); - //_sc.registerSerializerJson(); - //_sc.registerDeSerializerJson(); - //_sc.registerSerializerJson(); - //_sc.registerDeSerializerJson(); + msnj.registerSerializer(); + msnj.registerDeserializer(); }