From c0c01dde25b4db3b4982a1f5fc14895a6a6dff97 Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Wed, 27 May 2020 11:53:00 +0200 Subject: [PATCH] MongoDB::Array: int --> size_t in get for consistency with size(), new helper functions to add elements to an array. --- MongoDB/include/Poco/MongoDB/Array.h | 33 +++++++++++++++++++++++---- MongoDB/src/Array.cpp | 2 +- MongoDB/testsuite/src/MongoDBTest.cpp | 27 ++++++++++++++++++++++ MongoDB/testsuite/src/MongoDBTest.h | 1 + 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/MongoDB/include/Poco/MongoDB/Array.h b/MongoDB/include/Poco/MongoDB/Array.h index 55e2628406c..4d96592e6a0 100644 --- a/MongoDB/include/Poco/MongoDB/Array.h +++ b/MongoDB/include/Poco/MongoDB/Array.h @@ -40,7 +40,26 @@ class MongoDB_API Array: public Document /// Destroys the Array. template - 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(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 + 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 @@ -50,7 +69,7 @@ class MongoDB_API Array: public Document } template - 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. @@ -58,12 +77,12 @@ class MongoDB_API Array: public Document return Document::get(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 - 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. { @@ -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& to); + + std::size_t _curPos = 0; }; @@ -94,6 +118,7 @@ template<> inline void BSONReader::read(Array::Ptr& to) { to->read(_reader); + to->_curPos = to->size()-1; } diff --git a/MongoDB/src/Array.cpp b/MongoDB/src/Array.cpp index c6d96d1371d..8505f241055 100644 --- a/MongoDB/src/Array.cpp +++ b/MongoDB/src/Array.cpp @@ -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); diff --git a/MongoDB/testsuite/src/MongoDBTest.cpp b/MongoDB/testsuite/src/MongoDBTest.cpp index 9105ea9ddac..707a70d1663 100644 --- a/MongoDB/testsuite/src/MongoDBTest.cpp +++ b/MongoDB/testsuite/src/MongoDBTest.cpp @@ -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" @@ -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(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(0), "First"); + assertEqual(arr->get(1).raw(), birthdate.timestamp().raw()); + assertEqual(arr->get(2), 1993); + assertEqual(arr->get(3), false); +} + void MongoDBTest::testQueryRequest() { @@ -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); diff --git a/MongoDB/testsuite/src/MongoDBTest.h b/MongoDB/testsuite/src/MongoDBTest.h index 4c5954612a4..6ec23d728d8 100644 --- a/MongoDB/testsuite/src/MongoDBTest.h +++ b/MongoDB/testsuite/src/MongoDBTest.h @@ -27,6 +27,7 @@ class MongoDBTest: public CppUnit::TestCase virtual ~MongoDBTest(); void testInsertRequest(); + void testArray(); void testQueryRequest(); void testDBQueryRequest(); void testCountCommand();