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. (#3016)
  • Loading branch information
matejk committed May 29, 2022
1 parent 0f9a876 commit 9740190
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
34 changes: 30 additions & 4 deletions MongoDB/include/Poco/MongoDB/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,31 @@ class MongoDB_API Array: public Document
virtual ~Array();
/// Destroys the Array.

// Document template functions available for backward compatibility
using Document::add;
using Document::get;

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(size()), 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(size()), 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 +73,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 +95,9 @@ 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);
};


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
37 changes: 37 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,41 @@ 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<Poco::Int32>(1993));
arr->add(false);

// Document-style interface
arr->add("4", "12.4");

assertEqual(arr->size(), 5);
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<Poco::Int32>(2), 1993);
assertEqual(arr->get<bool>(3), false);
assertEqual(arr->get<std::string>(4), "12.4");

// Document-style interface
assertEqual(arr->get<Poco::Int32>("2"), 1993);
assertEqual(arr->get<std::string>("4"), "12.4");

}


void MongoDBTest::testQueryRequest()
{
Expand Down Expand Up @@ -472,6 +508,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 9740190

Please sign in to comment.