oatpp / oatpp-mongo

Oat++ native BSON + MongoDB driver implementation based on Oat++ object-mapping sub-framework.
https://oatpp.io/
Apache License 2.0
6 stars 4 forks source link

[TODO] add ObjectId constructor from String #6

Open lganzzzo opened 3 years ago

lganzzzo commented 3 years ago

Until this implemented users can use the following method:

#include "oatpp-mongo/bson/Types.hpp"
#include "oatpp/encoding/Hex.hpp"
#include "oatpp/core/data/stream/BufferStream.hpp"

oatpp::mongo::bson::ObjectId objectIdFromString(const oatpp::String& hexText) {
  oatpp::data::stream::BufferOutputStream stream(12);
  oatpp::encoding::Hex::decode(&stream, hexText->getData(), hexText->getSize(), false);
  if(stream.getCurrentPosition() != 12) {
    throw std::runtime_error("Error. Invalid string.");
  }
  return oatpp::mongo::bson::ObjectId(stream.getData());
}

auto objId = objectIdFromString("5f9b6ecb21a7da049662e7d2");
OATPP_LOGD("AAA", "objId='%s'", objId->toString()->c_str());