From a81d70f5a4a73b1562da99d0fb4837f312a98305 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Fri, 25 Sep 2020 03:45:16 +0300 Subject: [PATCH 1/3] Update to the latest oatpp API --- CMakeLists.txt | 2 +- src/oatpp-mongo/bson/mapping/Deserializer.cpp | 9 +++++---- src/oatpp-mongo/bson/mapping/Deserializer.hpp | 19 +++++++++---------- src/oatpp-mongo/bson/mapping/Serializer.cpp | 7 ++++--- src/oatpp-mongo/bson/mapping/Serializer.hpp | 5 ++--- test/oatpp-mongo/tests.cpp | 2 +- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a110be6..bd6265b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR) ## use these variables to configure module installation set(OATPP_THIS_MODULE_NAME oatpp-mongo) ## name of the module (also name of folders in installation dirs) -set(OATPP_THIS_MODULE_VERSION "1.1.0") ## version of the module (also sufix of folders in installation dirs) +set(OATPP_THIS_MODULE_VERSION "1.2.0") ## version of the module (also sufix of folders in installation dirs) set(OATPP_THIS_MODULE_LIBRARIES oatpp-mongo) ## list of libraries to find when find_package is called set(OATPP_THIS_MODULE_TARGETS oatpp-mongo) ## list of targets to install set(OATPP_THIS_MODULE_DIRECTORIES oatpp-mongo) ## list of directories to install diff --git a/src/oatpp-mongo/bson/mapping/Deserializer.cpp b/src/oatpp-mongo/bson/mapping/Deserializer.cpp index ad08dc5..f50d0df 100644 --- a/src/oatpp-mongo/bson/mapping/Deserializer.cpp +++ b/src/oatpp-mongo/bson/mapping/Deserializer.cpp @@ -334,7 +334,7 @@ oatpp::Void Deserializer::deserializeEnum(Deserializer* deserializer, v_char8 bsonTypeCode) { - auto polymorphicDispatcher = static_cast( + auto polymorphicDispatcher = static_cast( type->polymorphicDispatcher ); @@ -385,8 +385,9 @@ oatpp::Void Deserializer::deserializeObject(Deserializer* deserializer, parser::Caret innerCaret(caret.getCurrData(), docSize - 4); - auto object = type->creator(); - const auto& fieldsMap = type->propertiesGetter()->getMap(); + auto dispatcher = static_cast(type->polymorphicDispatcher); + auto object = dispatcher->createObject(); + const auto& fieldsMap = dispatcher->getProperties()->getMap(); while(innerCaret.canContinue() && innerCaret.getPosition() < innerCaret.getDataSize() - 1) { @@ -402,7 +403,7 @@ oatpp::Void Deserializer::deserializeObject(Deserializer* deserializer, if(fieldIterator != fieldsMap.end()){ auto field = fieldIterator->second; - field->set(object.get(), deserializer->deserialize(innerCaret, field->type, valueType)); + field->set(static_cast(object.get()), deserializer->deserialize(innerCaret, field->type, valueType)); } else if (deserializer->getConfig()->allowUnknownFields) { skipElement(innerCaret, valueType); diff --git a/src/oatpp-mongo/bson/mapping/Deserializer.hpp b/src/oatpp-mongo/bson/mapping/Deserializer.hpp index e3d272b..6568e53 100644 --- a/src/oatpp-mongo/bson/mapping/Deserializer.hpp +++ b/src/oatpp-mongo/bson/mapping/Deserializer.hpp @@ -40,9 +40,8 @@ namespace oatpp { namespace mongo { namespace bson { namespace mapping { */ class Deserializer { public: - typedef oatpp::data::mapping::type::Type Type; - typedef oatpp::data::mapping::type::Type::Property Property; - typedef oatpp::data::mapping::type::Type::Properties Properties; + typedef oatpp::BaseObject::Property Property; + typedef oatpp::BaseObject::Properties Properties; public: /** @@ -123,11 +122,11 @@ class Deserializer { parser::Caret innerCaret(caret.getCurrData(), docSize - 4); - auto listWrapper = type->creator(); - auto polymorphicDispatcher = static_cast(type->polymorphicDispatcher); + auto polymorphicDispatcher = static_cast(type->polymorphicDispatcher); + auto listWrapper = polymorphicDispatcher->createObject(); const auto& list = listWrapper.template staticCast(); - Type* itemType = *type->params.begin(); + const Type* itemType = *type->params.begin(); v_int32 expectedIndex = 0; while(innerCaret.canContinue() && innerCaret.getPosition() < innerCaret.getDataSize() - 1) { @@ -211,17 +210,17 @@ class Deserializer { parser::Caret innerCaret(caret.getCurrData(), docSize - 4); - auto mapWrapper = type->creator(); - auto polymorphicDispatcher = static_cast(type->polymorphicDispatcher); + auto polymorphicDispatcher = static_cast(type->polymorphicDispatcher); + auto mapWrapper = polymorphicDispatcher->createObject(); const auto& map = mapWrapper.template staticCast(); auto it = type->params.begin(); - Type* keyType = *it ++; + const Type* keyType = *it ++; if(keyType->classId.id != oatpp::data::mapping::type::__class::String::CLASS_ID.id){ throw std::runtime_error("[oatpp::mongo::bson::mapping::Deserializer::deserializeFieldsMap()]: Invalid bson map key. Key should be String"); } - Type* valueType = *it; + const Type* valueType = *it; while(innerCaret.canContinue() && innerCaret.getPosition() < innerCaret.getDataSize() - 1) { v_char8 valueTypeCode; diff --git a/src/oatpp-mongo/bson/mapping/Serializer.cpp b/src/oatpp-mongo/bson/mapping/Serializer.cpp index c7be7cf..fadd356 100644 --- a/src/oatpp-mongo/bson/mapping/Serializer.cpp +++ b/src/oatpp-mongo/bson/mapping/Serializer.cpp @@ -210,7 +210,7 @@ void Serializer::serializeEnum(Serializer* serializer, const oatpp::Void& polymorph) { - auto polymorphicDispatcher = static_cast( + auto polymorphicDispatcher = static_cast( polymorph.valueType->polymorphicDispatcher ); @@ -242,8 +242,9 @@ void Serializer::serializeObject(Serializer* serializer, data::stream::BufferOutputStream innerStream; - auto fields = polymorph.valueType->propertiesGetter()->getList(); - oatpp::DTO* object = static_cast(polymorph.get()); + auto dispatcher = static_cast(polymorph.valueType->polymorphicDispatcher); + auto fields = dispatcher->getProperties()->getList(); + auto object = static_cast(polymorph.get()); for (auto const &field : fields) { diff --git a/src/oatpp-mongo/bson/mapping/Serializer.hpp b/src/oatpp-mongo/bson/mapping/Serializer.hpp index 14ffa94..c91dd90 100644 --- a/src/oatpp-mongo/bson/mapping/Serializer.hpp +++ b/src/oatpp-mongo/bson/mapping/Serializer.hpp @@ -42,9 +42,8 @@ namespace oatpp { namespace mongo { namespace bson { namespace mapping { */ class Serializer { public: - typedef oatpp::data::mapping::type::Type Type; - typedef oatpp::data::mapping::type::Type::Property Property; - typedef oatpp::data::mapping::type::Type::Properties Properties; + typedef oatpp::BaseObject::Property Property; + typedef oatpp::BaseObject::Properties Properties; public: /** * Serializer config. diff --git a/test/oatpp-mongo/tests.cpp b/test/oatpp-mongo/tests.cpp index 2abdb19..932e665 100644 --- a/test/oatpp-mongo/tests.cpp +++ b/test/oatpp-mongo/tests.cpp @@ -158,7 +158,7 @@ void testMsg() { oatpp::mongo::bson::mapping::ObjectMapper objectMapper; auto connectionProvider = oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", 27017); - auto connection = connectionProvider->getConnection(); + auto connection = connectionProvider->get(); oatpp::mongo::driver::wire::Connection dbConnection(connection); From 386fe985a4c9a4f62ffb1634ad6f7b1575dec43a Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Fri, 25 Sep 2020 03:54:34 +0300 Subject: [PATCH 2/3] Support type interpretations. --- src/oatpp-mongo/bson/mapping/Deserializer.cpp | 7 +++++++ src/oatpp-mongo/bson/mapping/Deserializer.hpp | 5 +++++ src/oatpp-mongo/bson/mapping/Serializer.cpp | 11 +++++++++-- src/oatpp-mongo/bson/mapping/Serializer.hpp | 5 +++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/oatpp-mongo/bson/mapping/Deserializer.cpp b/src/oatpp-mongo/bson/mapping/Deserializer.cpp index f50d0df..1251a36 100644 --- a/src/oatpp-mongo/bson/mapping/Deserializer.cpp +++ b/src/oatpp-mongo/bson/mapping/Deserializer.cpp @@ -453,8 +453,15 @@ oatpp::Void Deserializer::deserialize(parser::Caret& caret, const Type* const ty if(method) { return (*method)(this, caret, type, bsonTypeCode); } else { + + auto* interpretation = type->findInterpretation(m_config->enableInterpretations); + if(interpretation) { + return interpretation->fromInterpretation(deserialize(caret, interpretation->getInterpretationType(), bsonTypeCode)); + } + throw std::runtime_error("[oatpp::mongo::bson::mapping::Deserializer::deserialize()]: " "Error. No deserialize method for type '" + std::string(type->classId.name) + "'"); + } } diff --git a/src/oatpp-mongo/bson/mapping/Deserializer.hpp b/src/oatpp-mongo/bson/mapping/Deserializer.hpp index 6568e53..e97e270 100644 --- a/src/oatpp-mongo/bson/mapping/Deserializer.hpp +++ b/src/oatpp-mongo/bson/mapping/Deserializer.hpp @@ -70,6 +70,11 @@ class Deserializer { */ bool allowUnknownFields = true; + /** + * Enable type interpretations. + */ + std::vector enableInterpretations = {}; + }; public: diff --git a/src/oatpp-mongo/bson/mapping/Serializer.cpp b/src/oatpp-mongo/bson/mapping/Serializer.cpp index fadd356..fe55ba2 100644 --- a/src/oatpp-mongo/bson/mapping/Serializer.cpp +++ b/src/oatpp-mongo/bson/mapping/Serializer.cpp @@ -276,8 +276,15 @@ void Serializer::serialize(data::stream::ConsistentOutputStream* stream, if(method) { (*method)(this, stream, key, polymorph); } else { - throw std::runtime_error("[oatpp::mongo::bson::mapping::Serializer::serialize()]: " - "Error. No serialize method for type '" + std::string(polymorph.valueType->classId.name) + "'"); + + auto* interpretation = polymorph.valueType->findInterpretation(m_config->enableInterpretations); + if(interpretation) { + serialize(stream, key, interpretation->toInterpretation(polymorph)); + } else { + throw std::runtime_error("[oatpp::mongo::bson::mapping::Serializer::serialize()]: " + "Error. No serialize method for type '" + std::string(polymorph.valueType->classId.name) + "'"); + } + } } diff --git a/src/oatpp-mongo/bson/mapping/Serializer.hpp b/src/oatpp-mongo/bson/mapping/Serializer.hpp index c91dd90..0827ae4 100644 --- a/src/oatpp-mongo/bson/mapping/Serializer.hpp +++ b/src/oatpp-mongo/bson/mapping/Serializer.hpp @@ -78,6 +78,11 @@ class Serializer { */ bool throwOnUnknownTypes = true; + /** + * Enable type interpretations. + */ + std::vector enableInterpretations = {}; + }; public: typedef void (*SerializerMethod)(Serializer*, From 3d2a2affb23ca311c55b736b9b977e2d83d186d1 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Wed, 30 Sep 2020 02:23:20 +0300 Subject: [PATCH 3/3] Update to the latest oatpp API. --- test/oatpp-mongo/tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/oatpp-mongo/tests.cpp b/test/oatpp-mongo/tests.cpp index 932e665..7bfb107 100644 --- a/test/oatpp-mongo/tests.cpp +++ b/test/oatpp-mongo/tests.cpp @@ -60,7 +60,7 @@ #include "oatpp-mongo/bson/Utils.hpp" -#include "oatpp/network/client/SimpleTCPConnectionProvider.hpp" +#include "oatpp/network/tcp/client/ConnectionProvider.hpp" #include "oatpp/core/data/stream/BufferStream.hpp" namespace { @@ -157,7 +157,7 @@ void testMsg() { oatpp::mongo::bson::mapping::ObjectMapper objectMapper; - auto connectionProvider = oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", 27017); + auto connectionProvider = oatpp::network::tcp::client::ConnectionProvider::createShared({"localhost", 27017}); auto connection = connectionProvider->get(); oatpp::mongo::driver::wire::Connection dbConnection(connection);