Skip to content

Commit

Permalink
Merge pull request #4 from oatpp/implement_datetime_datatype
Browse files Browse the repository at this point in the history
Types: Add DateTime support.
  • Loading branch information
lganzzzo authored Oct 30, 2020
2 parents ce4634f + 2ad0e01 commit ebfad69
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/oatpp-mongo/bson/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace __class {
const ClassId InlineDocument::CLASS_ID("oatpp::mongo::InlineDocument");
const ClassId InlineArray::CLASS_ID("oatpp::mongo::InlineArray");
const ClassId ObjectId::CLASS_ID("oatpp::mongo::ObjectId");
const ClassId DateTime::CLASS_ID("oatpp::mongo::DateTime");

}

Expand Down
16 changes: 16 additions & 0 deletions src/oatpp-mongo/bson/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ namespace __class {

};

class DateTime {
public:
static const ClassId CLASS_ID;

static Type *getType() {
static Type type(CLASS_ID, nullptr);
return &type;
}

};

}

/**
Expand All @@ -206,6 +217,11 @@ typedef oatpp::data::mapping::type::ObjectWrapper<oatpp::base::StrBuffer, __clas
*/
typedef oatpp::data::mapping::type::Primitive<type::ObjectId, __class::ObjectId> ObjectId;

/**
* DateTime is an ObjectWrapper over `v_int64` and __class::DateTime.
*/
typedef oatpp::data::mapping::type::Primitive<v_int64, __class::DateTime> DateTime;

}}}

#endif // oatpp_mongo_bson_Types_hpp
25 changes: 25 additions & 0 deletions src/oatpp-mongo/bson/mapping/Deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Deserializer::Deserializer(const std::shared_ptr<Config>& config)

setDeserializerMethod(oatpp::mongo::bson::__class::ObjectId::CLASS_ID, &Deserializer::deserializeObjectId);

setDeserializerMethod(oatpp::mongo::bson::__class::DateTime::CLASS_ID, &Deserializer::deserializeDateTime);

}

void Deserializer::setDeserializerMethod(const data::mapping::type::ClassId& classId, DeserializerMethod method) {
Expand Down Expand Up @@ -199,6 +201,29 @@ oatpp::Void Deserializer::deserializeBoolean(Deserializer* deserializer,

}

oatpp::Void Deserializer::deserializeDateTime(Deserializer* deserializer,
parser::Caret& caret,
const Type* const type,
v_char8 bsonTypeCode)
{
(void) deserializer;
(void) type;

switch(bsonTypeCode) {

case TypeCode::NULL_VALUE:
return oatpp::Void(DateTime::Class::getType());

case TypeCode::DATE_TIME:
return DateTime(Utils::readInt64(caret));

default:
caret.setError("[oatpp::mongo::bson::mapping::Deserializer::deserializeDateTime()]: Error. Type-code doesn't match DateTime.");
return oatpp::Void(DateTime::Class::getType());

}
}

oatpp::Void Deserializer::deserializeString(Deserializer* deserializer,
parser::Caret& caret,
const Type* const type,
Expand Down
1 change: 1 addition & 0 deletions src/oatpp-mongo/bson/mapping/Deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ class Deserializer {
}

static oatpp::Void deserializeBoolean(Deserializer* deserializer, parser::Caret& caret, const Type* const type, v_char8 bsonTypeCode);
static oatpp::Void deserializeDateTime(Deserializer* deserializer, parser::Caret& caret, const Type* const type, v_char8 bsonTypeCode);
static oatpp::Void deserializeString(Deserializer* deserializer, parser::Caret& caret, const Type* const type, v_char8 bsonTypeCode);

static oatpp::Void deserializeInlineDocs(Deserializer* deserializer, parser::Caret& caret, const Type* const type, v_char8 bsonTypeCode);
Expand Down
21 changes: 21 additions & 0 deletions src/oatpp-mongo/bson/mapping/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Serializer::Serializer(const std::shared_ptr<Config>& config)

setSerializerMethod(oatpp::mongo::bson::__class::ObjectId::CLASS_ID, &Serializer::serializeObjectId);

setSerializerMethod(oatpp::mongo::bson::__class::DateTime::CLASS_ID, &Serializer::serializeDateTime);

}

void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId, SerializerMethod method) {
Expand All @@ -86,6 +88,25 @@ void Serializer::setSerializerMethod(const data::mapping::type::ClassId& classId
}
}

void Serializer::serializeDateTime(Serializer* serializer,
data::stream::ConsistentOutputStream* stream,
const data::share::StringKeyLabel& key,
const oatpp::Void& polymorph)
{
(void) serializer;

if(!key) {
throw std::runtime_error("[oatpp::mongo::bson::mapping::Serializer::serializeDateTime()]: Error. The key can't be null.");
}

if(polymorph) {
bson::Utils::writeKey(stream, TypeCode::DATE_TIME, key);
bson::Utils::writeInt64(stream, *static_cast<v_int64*>(polymorph.get()));
} else {
bson::Utils::writeKey(stream, TypeCode::NULL_VALUE, key);
}
}

void Serializer::serializeString(Serializer* serializer,
data::stream::ConsistentOutputStream* stream,
const data::share::StringKeyLabel& key,
Expand Down
6 changes: 5 additions & 1 deletion src/oatpp-mongo/bson/mapping/Serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class Serializer {
}

if(polymorph) {
auto str = static_cast<oatpp::base::StrBuffer *>(polymorph.get());
bson::Utils::writePrimitive(stream, key, * static_cast<typename T::ObjectType*>(polymorph.get()));
} else {
bson::Utils::writeKey(stream, TypeCode::NULL_VALUE, key);
Expand Down Expand Up @@ -181,6 +180,11 @@ class Serializer {

}

static void serializeDateTime(Serializer* serializer,
data::stream::ConsistentOutputStream* stream,
const data::share::StringKeyLabel& key,
const oatpp::Void& polymorph);

static void serializeString(Serializer* serializer,
data::stream::ConsistentOutputStream* stream,
const data::share::StringKeyLabel& key,
Expand Down

0 comments on commit ebfad69

Please sign in to comment.