summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Morrow <acm@10gen.com>2013-07-09 18:56:30 -0400
committerAndrew Morrow <acm@10gen.com>2013-07-09 20:02:51 -0400
commit2dd8b16ebbafd564fcd2967ec390da222dedc3c5 (patch)
tree329ad85493342d1e080c137bf8a9e2c44313933a /src
parent5623bbd54da40c28f876a516760836b29ab2441f (diff)
downloadmongo-2dd8b16ebbafd564fcd2967ec390da222dedc3c5.tar.gz
SERVER-8046 Cleanup Element::writeArrayTo and add a test for the new implementation
Diffstat (limited to 'src')
-rw-r--r--src/mongo/bson/mutable/document.cpp2
-rw-r--r--src/mongo/bson/mutable/element.cpp10
-rw-r--r--src/mongo/bson/mutable/mutable_bson_test.cpp17
3 files changed, 21 insertions, 8 deletions
diff --git a/src/mongo/bson/mutable/document.cpp b/src/mongo/bson/mutable/document.cpp
index f8cee4b8913..27c1411240d 100644
--- a/src/mongo/bson/mutable/document.cpp
+++ b/src/mongo/bson/mutable/document.cpp
@@ -1459,7 +1459,7 @@ namespace mutablebson {
const Document::Impl& impl = getDocument().getImpl();
const ElementRep& thisRep = impl.getElementRep(_repIdx);
verify(impl.getType(thisRep) == mongo::Array);
- return writeElement(builder);
+ return writeChildren(builder);
}
Status Element::setValueDouble(const double value) {
diff --git a/src/mongo/bson/mutable/element.cpp b/src/mongo/bson/mutable/element.cpp
index 79de5b073ec..746c4c43dbe 100644
--- a/src/mongo/bson/mutable/element.cpp
+++ b/src/mongo/bson/mutable/element.cpp
@@ -158,10 +158,12 @@ namespace mutablebson {
BSONObj obj = builder.obj();
return obj.firstElement().toString();
} else if (isType(mongo::Array)) {
- BSONArrayBuilder builder;
- writeArrayTo(&builder);
- BSONArray arr = builder.arr();
- return arr.firstElement().toString();
+ BSONObjBuilder builder;
+ BSONArrayBuilder arrayBuilder(builder.subarrayStart(getFieldName()));
+ writeArrayTo(&arrayBuilder);
+ arrayBuilder.done();
+ BSONObj obj = builder.obj();
+ return obj.firstElement().toString();
} else {
return "corrupted element";
}
diff --git a/src/mongo/bson/mutable/mutable_bson_test.cpp b/src/mongo/bson/mutable/mutable_bson_test.cpp
index 57a8eeb64ce..af8f3224fc1 100644
--- a/src/mongo/bson/mutable/mutable_bson_test.cpp
+++ b/src/mongo/bson/mutable/mutable_bson_test.cpp
@@ -1407,14 +1407,25 @@ namespace {
TEST(Document, toStringEphemeralObject) {
mmb::Document doc;
mmb::Element e = doc.makeElementObject("foo");
- doc.root().pushBack(e);
- e.appendDouble("d", 1.0);
- e.appendString("s", "str");
+ ASSERT_OK(doc.root().pushBack(e));
+ ASSERT_OK(e.appendDouble("d", 1.0));
+ ASSERT_OK(e.appendString("s", "str"));
ASSERT_EQUALS(
mongo::fromjson("{ foo: { d : 1.0, s : 'str' } }").firstElement().toString(),
e.toString());
}
+ TEST(Document, toStringEphemeralArray) {
+ mmb::Document doc;
+ mmb::Element e = doc.makeElementArray("foo");
+ ASSERT_OK(doc.root().pushBack(e));
+ ASSERT_OK(e.appendDouble(mongo::StringData(), 1.0));
+ ASSERT_OK(e.appendString(mongo::StringData(), "str"));
+ ASSERT_EQUALS(
+ mongo::fromjson("{ foo: [ 1.0, 'str' ] }").firstElement().toString(),
+ e.toString());
+ }
+
TEST(Document, ElementCloningToDifferentDocument) {
const char initial[] = "{ a : 1, b : [ 1, 2, 3 ], c : { 'c' : 'c' }, d : [ 4, 5, 6 ] }";