Skip to content

Commit

Permalink
Merge pull request #3 from oatpp/v_1.2.0
Browse files Browse the repository at this point in the history
V 1.2.0
  • Loading branch information
lganzzzo authored Sep 29, 2020
2 parents 01f78dd + 3d2a2af commit ce4634f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/oatpp-mongo/bson/mapping/Deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ oatpp::Void Deserializer::deserializeEnum(Deserializer* deserializer,
v_char8 bsonTypeCode)
{

auto polymorphicDispatcher = static_cast<const data::mapping::type::__class::AbstractEnum::AbstractPolymorphicDispatcher*>(
auto polymorphicDispatcher = static_cast<const data::mapping::type::__class::AbstractEnum::PolymorphicDispatcher*>(
type->polymorphicDispatcher
);

Expand Down Expand Up @@ -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<const oatpp::data::mapping::type::__class::AbstractObject::PolymorphicDispatcher*>(type->polymorphicDispatcher);
auto object = dispatcher->createObject();
const auto& fieldsMap = dispatcher->getProperties()->getMap();

while(innerCaret.canContinue() && innerCaret.getPosition() < innerCaret.getDataSize() - 1) {

Expand All @@ -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<oatpp::BaseObject*>(object.get()), deserializer->deserialize(innerCaret, field->type, valueType));

} else if (deserializer->getConfig()->allowUnknownFields) {
skipElement(innerCaret, valueType);
Expand Down Expand Up @@ -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) + "'");

}
}

Expand Down
24 changes: 14 additions & 10 deletions src/oatpp-mongo/bson/mapping/Deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

/**
Expand Down Expand Up @@ -71,6 +70,11 @@ class Deserializer {
*/
bool allowUnknownFields = true;

/**
* Enable type interpretations.
*/
std::vector<std::string> enableInterpretations = {};

};

public:
Expand Down Expand Up @@ -123,11 +127,11 @@ class Deserializer {

parser::Caret innerCaret(caret.getCurrData(), docSize - 4);

auto listWrapper = type->creator();
auto polymorphicDispatcher = static_cast<const typename Collection::Class::AbstractPolymorphicDispatcher*>(type->polymorphicDispatcher);
auto polymorphicDispatcher = static_cast<const typename Collection::Class::PolymorphicDispatcher*>(type->polymorphicDispatcher);
auto listWrapper = polymorphicDispatcher->createObject();
const auto& list = listWrapper.template staticCast<Collection>();

Type* itemType = *type->params.begin();
const Type* itemType = *type->params.begin();
v_int32 expectedIndex = 0;
while(innerCaret.canContinue() && innerCaret.getPosition() < innerCaret.getDataSize() - 1) {

Expand Down Expand Up @@ -211,17 +215,17 @@ class Deserializer {

parser::Caret innerCaret(caret.getCurrData(), docSize - 4);

auto mapWrapper = type->creator();
auto polymorphicDispatcher = static_cast<const typename Collection::Class::AbstractPolymorphicDispatcher*>(type->polymorphicDispatcher);
auto polymorphicDispatcher = static_cast<const typename Collection::Class::PolymorphicDispatcher*>(type->polymorphicDispatcher);
auto mapWrapper = polymorphicDispatcher->createObject();
const auto& map = mapWrapper.template staticCast<Collection>();

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;
Expand Down
18 changes: 13 additions & 5 deletions src/oatpp-mongo/bson/mapping/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void Serializer::serializeEnum(Serializer* serializer,
const oatpp::Void& polymorph)
{

auto polymorphicDispatcher = static_cast<const data::mapping::type::__class::AbstractEnum::AbstractPolymorphicDispatcher*>(
auto polymorphicDispatcher = static_cast<const data::mapping::type::__class::AbstractEnum::PolymorphicDispatcher*>(
polymorph.valueType->polymorphicDispatcher
);

Expand Down Expand Up @@ -242,8 +242,9 @@ void Serializer::serializeObject(Serializer* serializer,

data::stream::BufferOutputStream innerStream;

auto fields = polymorph.valueType->propertiesGetter()->getList();
oatpp::DTO* object = static_cast<oatpp::DTO*>(polymorph.get());
auto dispatcher = static_cast<const oatpp::data::mapping::type::__class::AbstractObject::PolymorphicDispatcher*>(polymorph.valueType->polymorphicDispatcher);
auto fields = dispatcher->getProperties()->getList();
auto object = static_cast<oatpp::BaseObject*>(polymorph.get());

for (auto const &field : fields) {

Expand Down Expand Up @@ -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) + "'");
}

}
}

Expand Down
10 changes: 7 additions & 3 deletions src/oatpp-mongo/bson/mapping/Serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -79,6 +78,11 @@ class Serializer {
*/
bool throwOnUnknownTypes = true;

/**
* Enable type interpretations.
*/
std::vector<std::string> enableInterpretations = {};

};
public:
typedef void (*SerializerMethod)(Serializer*,
Expand Down
6 changes: 3 additions & 3 deletions test/oatpp-mongo/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit ce4634f

Please sign in to comment.