Skip to content

Commit

Permalink
Merge pull request #124 from AO-StreetArt/mongoDates
Browse files Browse the repository at this point in the history
Addition of Mongo Date object to buffer
  • Loading branch information
AO-StreetArt committed Mar 31, 2018
2 parents cfcdc60 + 8620615 commit 4ec1585
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions aossl/mongo/include/mongo_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ class MongoBuffer: public Buffer, public MongoBufferInterface {
bson_append_double(children[child_index], key, -1, value);
array_indices[child_index]++;
}
//! Add a datetime value to the buffer
void add_date(std::string key, int value) {
bson_append_date_time(children[child_index], key.c_str(), -1, value);
}
//! Add an datetime value to the open array in the buffer
void add_date(int value) {
char str[16];
const char *key;
bson_uint32_to_string(array_indices[child_index], &key, str, 16);
bson_append_date_time(children[child_index], key, -1, value);
array_indices[child_index]++;
}
//! Start an array in the buffer
inline void start_array(std::string key) {
bson_t *child = new bson_t;
Expand Down
4 changes: 4 additions & 0 deletions aossl/mongo/include/mongo_buffer_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class MongoBufferInterface {
virtual void add_double(std::string key, double value) = 0;
//! Add a double value to the open array in the buffer
virtual void add_double(double value) = 0;
//! Add a datetime value to the buffer
virtual void add_date(std::string key, int value) = 0;
//! Add an datetime value to the open array in the buffer
virtual void add_date(int value) = 0;
//! Start an array in the buffer
virtual void start_array(std::string key) = 0;
//! End an array value in the buffer
Expand Down
6 changes: 6 additions & 0 deletions aossl/mongo/mongo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void test_mongo_buffer() {
std::string dkey4 = "numbers";
std::string dkey5 = "integers";
std::string dkey6 = "double";
std::string dkey7 = "date";
std::string search_key = "name.last";
std::string dval1 = "Alex";
std::string dval2 = "Barry";
Expand All @@ -77,6 +78,9 @@ void test_mongo_buffer() {
buf->end_array();
assert(buf->has_field(dkey5));
assert(buf->count_keys() == 5);
buf->add_date(dkey7, 1522515115);
assert(buf->has_field(dkey7));
assert(buf->count_keys() == 6);
std::cout << "Test Mongo Buffer:" << std::endl;
std::cout << buf->to_json() << std::endl;
delete buf;
Expand All @@ -98,6 +102,7 @@ void test_bson_api(MongoInterface *mongo, MongoInterface *bad_mongo) {
std::string dkey4 = "numbers";
std::string dkey5 = "integers";
std::string dkey6 = "double";
std::string dkey7 = "date";
std::string dval1 = "Alex";
std::string dval2 = "Barry";
buf->start_object(dkey1);
Expand All @@ -111,6 +116,7 @@ void test_bson_api(MongoInterface *mongo, MongoInterface *bad_mongo) {
buf->end_array();
buf->add_double(dkey6, 1.01);
buf->end_object();
buf->add_date(dkey7, 1522515115);
try {
resp1 = mongo->create_document(buf);
key1 = resp1->get_value();
Expand Down

0 comments on commit 4ec1585

Please sign in to comment.