diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2021-08-05 18:46:09 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-08-23 18:41:54 +0000 |
commit | 5f0ea16eed2bf1292077baa654f80a0acefeed8b (patch) | |
tree | b02ed69c288c5b01f95489a62f8a95ac6bf9e431 /src/mongo/idl | |
parent | 7bada458c6185d97803390e9850590aa799b37b0 (diff) | |
download | mongo-5f0ea16eed2bf1292077baa654f80a0acefeed8b.tar.gz |
SERVER-59453 Add support for generic BSONArray to IDL
Diffstat (limited to 'src/mongo/idl')
-rw-r--r-- | src/mongo/idl/basic_types.idl | 11 | ||||
-rw-r--r-- | src/mongo/idl/idl_test.cpp | 68 | ||||
-rw-r--r-- | src/mongo/idl/unittest.idl | 5 |
3 files changed, 38 insertions, 46 deletions
diff --git a/src/mongo/idl/basic_types.idl b/src/mongo/idl/basic_types.idl index 37a7f86a0d0..c0037bbb446 100644 --- a/src/mongo/idl/basic_types.idl +++ b/src/mongo/idl/basic_types.idl @@ -187,6 +187,17 @@ types: cpp_type: "mongo::BSONObj" deserializer: "BSONObj::getOwned" + array: + bson_serialization_type: array + description: "An unowned BSONArray without custom deserialization or seialization" + cpp_type: "mongo::BSONArray" + + array_owned: + bson_serialization_type: array + description: "An owned BSONArray" + cpp_type: "mongo::BSONArray" + deserializer: "BSONObj::getOwned" + date: bson_serialization_type: date description: "A BSON UTC DateTime" diff --git a/src/mongo/idl/idl_test.cpp b/src/mongo/idl/idl_test.cpp index b09aa091a71..daab2d425ee 100644 --- a/src/mongo/idl/idl_test.cpp +++ b/src/mongo/idl/idl_test.cpp @@ -152,17 +152,21 @@ void assert_same_types() { template <typename ParserT, typename TestT, BSONType Test_bson_type> void TestLoopback(TestT test_value) { - IDLParserErrorContext ctxt("root"); - auto testDoc = BSON("value" << test_value); - auto element = testDoc.firstElement(); ASSERT_EQUALS(element.type(), Test_bson_type); - auto testStruct = ParserT::parse(ctxt, testDoc); + auto testStruct = ParserT::parse({"test"}, testDoc); assert_same_types<decltype(testStruct.getValue()), TestT>(); - ASSERT_EQUALS(testStruct.getValue(), test_value); + // We need to use a different unittest macro for comparing obj/array. + constexpr bool isObjectTest = std::is_same_v<TestT, const BSONObj&>; + constexpr bool isArrayTest = std::is_same_v<TestT, const BSONArray&>; + if constexpr (isObjectTest || isArrayTest) { + ASSERT_BSONOBJ_EQ(testStruct.getValue(), test_value); + } else { + ASSERT_EQUALS(testStruct.getValue(), test_value); + } // Positive: Test we can roundtrip from the just parsed document { @@ -192,7 +196,15 @@ void TestLoopback(TestT test_value) { // Validate the operator == works // Use ASSERT instead of ASSERT_EQ to avoid operator<< - ASSERT(one_new == testStruct); + if constexpr (!isArrayTest) { + // BSONArray comparison not currently implemented. + ASSERT_TRUE(one_new == testStruct); + } + + if constexpr (isObjectTest) { + // Only One_plain_object implements comparison ops + ASSERT_FALSE(one_new < testStruct); + } } } @@ -207,46 +219,10 @@ TEST(IDLOneTypeTests, TestLoopbackTest) { TestLoopback<One_objectid, const OID&, jstOID>(OID::max()); TestLoopback<One_date, const Date_t&, Date>(Date_t::now()); TestLoopback<One_timestamp, const Timestamp&, bsonTimestamp>(Timestamp::max()); -} - -// Test a BSONObj can be passed through an IDL type -TEST(IDLOneTypeTests, TestObjectLoopbackTest) { - IDLParserErrorContext ctxt("root"); - - auto testValue = BSON("Hello" - << "World"); - auto testDoc = BSON("value" << testValue); - - auto element = testDoc.firstElement(); - ASSERT_EQUALS(element.type(), Object); - - auto testStruct = One_plain_object::parse(ctxt, testDoc); - assert_same_types<decltype(testStruct.getValue()), const BSONObj&>(); - - ASSERT_BSONOBJ_EQ(testStruct.getValue(), testValue); - - // Positive: Test we can roundtrip from the just parsed document - { - BSONObjBuilder builder; - testStruct.serialize(&builder); - auto loopbackDoc = builder.obj(); - - ASSERT_BSONOBJ_EQ(testDoc, loopbackDoc); - } - - // Positive: Test we can serialize from nothing the same document - { - BSONObjBuilder builder; - One_plain_object one_new; - one_new.setValue(testValue); - one_new.serialize(&builder); - - auto serializedDoc = builder.obj(); - ASSERT_BSONOBJ_EQ(testDoc, serializedDoc); - - ASSERT_TRUE(one_new == testStruct); - ASSERT_FALSE(one_new < testStruct); - } + TestLoopback<One_plain_object, const BSONObj&, Object>(BSON("Hello" + << "World")); + TestLoopback<One_plain_array, const BSONArray&, Array>(BSON_ARRAY("Hello" + << "World")); } // Test we compare an object with optional BSONObjs correctly diff --git a/src/mongo/idl/unittest.idl b/src/mongo/idl/unittest.idl index da3741f21cc..712a04ce4c2 100644 --- a/src/mongo/idl/unittest.idl +++ b/src/mongo/idl/unittest.idl @@ -126,6 +126,11 @@ structs: fields: value: object + one_plain_array: + description: UnitTest for a single BSONArray + fields: + value: array + one_plain_optional_object: description: UnitTest for optional BSONObj generate_comparison_operators: true |