Skip to content

Commit

Permalink
MongoDB::Array: int --> size_t in get for consistency with size(), ne…
Browse files Browse the repository at this point in the history
…w helper functions to add elements to an array.
  • Loading branch information
matejk committed Jun 28, 2021
1 parent 05660c7 commit c0c01dd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
33 changes: 29 additions & 4 deletions MongoDB/include/Poco/MongoDB/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,26 @@ class MongoDB_API Array: public Document
/// Destroys the Array.

template<typename T>
T get(int pos) const
Document& add(T value)
/// Creates an element with the name from the current pos and value and
/// adds it to the array document.
///
/// The active document is returned to allow chaining of the add methods.
{
return Document::add<T>(Poco::NumberFormatter::format(_curPos++), value);
}

Document& add(const char* value)
/// Creates an element with a name from the current pos and value and
/// adds it to the array document.
///
/// The active document is returned to allow chaining of the add methods.
{
return Document::add(Poco::NumberFormatter::format(_curPos++), value);
}

template<typename T>
T get(std::size_t pos) const
/// Returns the element at the given index and tries to convert
/// it to the template type. If the element is not found, a
/// Poco::NotFoundException will be thrown. If the element cannot be
Expand All @@ -50,20 +69,20 @@ class MongoDB_API Array: public Document
}

template<typename T>
T get(int pos, const T& deflt) const
T get(std::size_t pos, const T& deflt) const
/// Returns the element at the given index and tries to convert
/// it to the template type. If the element is not found, or
/// has the wrong type, the deflt argument will be returned.
{
return Document::get<T>(Poco::NumberFormatter::format(pos), deflt);
}

Element::Ptr get(int pos) const;
Element::Ptr get(std::size_t pos) const;
/// Returns the element at the given index.
/// An empty element will be returned if the element is not found.

template<typename T>
bool isType(int pos) const
bool isType(std::size_t pos) const
/// Returns true if the type of the element equals the TypeId of ElementTrait,
/// otherwise false.
{
Expand All @@ -72,6 +91,11 @@ class MongoDB_API Array: public Document

std::string toString(int indent = 0) const;
/// Returns a string representation of the Array.

private:
friend void BSONReader::read<Array::Ptr>(Array::Ptr& to);

std::size_t _curPos = 0;
};


Expand All @@ -94,6 +118,7 @@ template<>
inline void BSONReader::read<Array::Ptr>(Array::Ptr& to)
{
to->read(_reader);
to->_curPos = to->size()-1;
}


Expand Down
2 changes: 1 addition & 1 deletion MongoDB/src/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Array::~Array()
}


Element::Ptr Array::get(int pos) const
Element::Ptr Array::get(std::size_t pos) const
{
std::string name = Poco::NumberFormatter::format(pos);
return Document::get(name);
Expand Down
27 changes: 27 additions & 0 deletions MongoDB/testsuite/src/MongoDBTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Poco/DateTime.h"
#include "Poco/ObjectPool.h"
#include "Poco/MongoDB/Array.h"
#include "Poco/MongoDB/InsertRequest.h"
#include "Poco/MongoDB/QueryRequest.h"
#include "Poco/MongoDB/DeleteRequest.h"
Expand Down Expand Up @@ -77,6 +78,31 @@ void MongoDBTest::testInsertRequest()
_mongo->sendRequest(request);
}

void MongoDBTest::testArray()
{
Poco::MongoDB::Array::Ptr arr = new Poco::MongoDB::Array();
arr->add(std::string("First"));

Poco::DateTime birthdate;
birthdate.assign(1969, 3, 9);
arr->add(birthdate.timestamp());

arr->add(static_cast<int>(1993));
arr->add(false);

assertEqual(arr->size(), 4);
assertTrue(arr->exists("0"));
assertTrue(arr->exists("1"));
assertTrue(arr->exists("2"));
assertTrue(arr->exists("3"));
assertFalse(arr->exists("4"));

assertEqual(arr->get<std::string>(0), "First");
assertEqual(arr->get<Poco::Timestamp>(1).raw(), birthdate.timestamp().raw());
assertEqual(arr->get<int>(2), 1993);
assertEqual(arr->get<bool>(3), false);
}


void MongoDBTest::testQueryRequest()
{
Expand Down Expand Up @@ -472,6 +498,7 @@ CppUnit::Test* MongoDBTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo);
CppUnit_addTest(pSuite, MongoDBTest, testInsertRequest);
CppUnit_addTest(pSuite, MongoDBTest, testArray);
CppUnit_addTest(pSuite, MongoDBTest, testQueryRequest);
CppUnit_addTest(pSuite, MongoDBTest, testDBQueryRequest);
CppUnit_addTest(pSuite, MongoDBTest, testCountCommand);
Expand Down
1 change: 1 addition & 0 deletions MongoDB/testsuite/src/MongoDBTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MongoDBTest: public CppUnit::TestCase
virtual ~MongoDBTest();

void testInsertRequest();
void testArray();
void testQueryRequest();
void testDBQueryRequest();
void testCountCommand();
Expand Down

0 comments on commit c0c01dd

Please sign in to comment.