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..1251a36 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); @@ -452,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 e3d272b..e97e270 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: /** @@ -71,6 +70,11 @@ class Deserializer { */ bool allowUnknownFields = true; + /** + * Enable type interpretations. + */ + std::vector enableInterpretations = {}; + }; public: @@ -123,11 +127,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 +215,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..fe55ba2 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) { @@ -275,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 14ffa94..0827ae4 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. @@ -79,6 +78,11 @@ class Serializer { */ bool throwOnUnknownTypes = true; + /** + * Enable type interpretations. + */ + std::vector enableInterpretations = {}; + }; public: typedef void (*SerializerMethod)(Serializer*, diff --git a/test/oatpp-mongo/tests.cpp b/test/oatpp-mongo/tests.cpp index 2abdb19..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,8 +157,8 @@ void testMsg() { oatpp::mongo::bson::mapping::ObjectMapper objectMapper; - auto connectionProvider = oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", 27017); - auto connection = connectionProvider->getConnection(); + auto connectionProvider = oatpp::network::tcp::client::ConnectionProvider::createShared({"localhost", 27017}); + auto connection = connectionProvider->get(); oatpp::mongo::driver::wire::Connection dbConnection(connection);