diff options
author | Ted Tuckman <ted.tuckman@mongodb.com> | 2020-07-10 10:19:26 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-10-02 14:00:45 +0000 |
commit | 0348620630f3dd86214676e7e9566a6aa6e6be5c (patch) | |
tree | a1b84365ffc80bb73c2df02c18029d6f147e147b /src/mongo/db/pipeline | |
parent | 45637f4d481c8badd6d5a2d95dcb8ae947c78c92 (diff) | |
download | mongo-0348620630f3dd86214676e7e9566a6aa6e6be5c.tar.gz |
SERVER-25893 Make it easier to construct a Value from a Vector
Diffstat (limited to 'src/mongo/db/pipeline')
7 files changed, 44 insertions, 64 deletions
diff --git a/src/mongo/db/pipeline/aggregation_request_test.cpp b/src/mongo/db/pipeline/aggregation_request_test.cpp index 8642432720f..225be1a950e 100644 --- a/src/mongo/db/pipeline/aggregation_request_test.cpp +++ b/src/mongo/db/pipeline/aggregation_request_test.cpp @@ -158,7 +158,7 @@ TEST(AggregationRequestTest, ShouldOnlySerializeRequiredFieldsIfNoOptionalFields auto expectedSerialization = Document{{AggregationRequest::kCommandName, nss.coll()}, - {AggregationRequest::kPipelineName, Value(std::vector<Value>{})}, + {AggregationRequest::kPipelineName, std::vector<Value>{}}, {AggregationRequest::kCursorName, Value(kDefaultCursorOptionDocument)}}; ASSERT_DOCUMENT_EQ(request.serializeToCommandObj(), expectedSerialization); } @@ -181,7 +181,7 @@ TEST(AggregationRequestTest, ShouldNotSerializeOptionalValuesIfEquivalentToDefau auto expectedSerialization = Document{{AggregationRequest::kCommandName, nss.coll()}, - {AggregationRequest::kPipelineName, Value(std::vector<Value>{})}, + {AggregationRequest::kPipelineName, std::vector<Value>{}}, {AggregationRequest::kCursorName, Value(kDefaultCursorOptionDocument)}}; ASSERT_DOCUMENT_EQ(request.serializeToCommandObj(), expectedSerialization); } @@ -216,7 +216,7 @@ TEST(AggregationRequestTest, ShouldSerializeOptionalValuesIfSet) { auto expectedSerialization = Document{{AggregationRequest::kCommandName, nss.coll()}, - {AggregationRequest::kPipelineName, Value(std::vector<Value>{})}, + {AggregationRequest::kPipelineName, std::vector<Value>{}}, {AggregationRequest::kAllowDiskUseName, true}, {AggregationRequest::kFromMongosName, true}, {AggregationRequest::kNeedsMergeName, true}, @@ -242,7 +242,7 @@ TEST(AggregationRequestTest, ShouldSerializeBatchSizeIfSetAndExplainFalse) { auto expectedSerialization = Document{{AggregationRequest::kCommandName, nss.coll()}, - {AggregationRequest::kPipelineName, Value(std::vector<Value>{})}, + {AggregationRequest::kPipelineName, std::vector<Value>{}}, {AggregationRequest::kCursorName, Value(Document({{AggregationRequest::kBatchSizeName, 10}}))}}; ASSERT_DOCUMENT_EQ(request.serializeToCommandObj(), expectedSerialization); @@ -254,7 +254,7 @@ TEST(AggregationRequestTest, ShouldSerialiseAggregateFieldToOneIfCollectionIsAgg auto expectedSerialization = Document{{AggregationRequest::kCommandName, 1}, - {AggregationRequest::kPipelineName, Value(std::vector<Value>{})}, + {AggregationRequest::kPipelineName, std::vector<Value>{}}, {AggregationRequest::kCursorName, Value(Document({{AggregationRequest::kBatchSizeName, AggregationRequest::kDefaultBatchSize}}))}}; @@ -287,10 +287,9 @@ TEST(AggregationRequestTest, ShouldNotSerializeBatchSizeWhenExplainSet) { request.setBatchSize(10); request.setExplain(ExplainOptions::Verbosity::kQueryPlanner); - auto expectedSerialization = - Document{{AggregationRequest::kCommandName, nss.coll()}, - {AggregationRequest::kPipelineName, Value(std::vector<Value>{})}, - {AggregationRequest::kCursorName, Value(Document())}}; + auto expectedSerialization = Document{{AggregationRequest::kCommandName, nss.coll()}, + {AggregationRequest::kPipelineName, std::vector<Value>{}}, + {AggregationRequest::kCursorName, Value(Document())}}; ASSERT_DOCUMENT_EQ(request.serializeToCommandObj(), expectedSerialization); } diff --git a/src/mongo/db/pipeline/document_path_support_test.cpp b/src/mongo/db/pipeline/document_path_support_test.cpp index f870a317f22..5e966e08a75 100644 --- a/src/mongo/db/pipeline/document_path_support_test.cpp +++ b/src/mongo/db/pipeline/document_path_support_test.cpp @@ -70,7 +70,7 @@ TEST(VisitAllValuesAtPathTest, NestedObjectWithEmptyArrayValue) { TEST(VisitAllValuesAtPathTest, NestedObjectWithSingletonArrayValue) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{{"a", Document{{"b", vector<Value>{Value(1)}}}}}; + Document doc{{"a", Document{{"b", vector{1}}}}}; visitAllValuesAtPath(doc, FieldPath("a.b"), callback); ASSERT_EQ(values.size(), 1UL); ASSERT_EQ(values.count(Value(1)), 1UL); @@ -79,7 +79,7 @@ TEST(VisitAllValuesAtPathTest, NestedObjectWithSingletonArrayValue) { TEST(VisitAllValuesAtPathTest, NestedObjectWithArrayValue) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{{"a", Document{{"b", vector<Value>{Value(1), Value(2), Value(3)}}}}}; + Document doc{{"a", Document{{"b", vector{1, 2, 3}}}}}; visitAllValuesAtPath(doc, FieldPath("a.b"), callback); ASSERT_EQ(values.size(), 3UL); ASSERT_EQ(values.count(Value(1)), 1UL); @@ -103,9 +103,9 @@ TEST(VisitAllValuesAtPathTest, ObjectWithArrayOfSubobjectsWithArrayValues) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; Document doc{{"a", - vector<Document>{Document{{"b", vector<Value>{Value(1), Value(2)}}}, - Document{{"b", vector<Value>{Value(2), Value(3)}}}, - Document{{"b", vector<Value>{Value(3), Value(1)}}}}}}; + vector<Document>{Document{{"b", vector{1, 2}}}, + Document{{"b", vector{2, 3}}}, + Document{{"b", vector{3, 1}}}}}}; visitAllValuesAtPath(doc, FieldPath("a.b"), callback); ASSERT_EQ(values.size(), 3UL); ASSERT_EQ(values.count(Value(1)), 1UL); @@ -151,7 +151,7 @@ TEST(VisitAllValuesAtPathTest, AcceptsNumericFieldNames) { TEST(VisitAllValuesAtPathTest, UsesNumericFieldNameToExtractElementFromArray) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{{"a", vector<Value>{Value(1), Value(Document{{"0", 1}})}}}; + Document doc{{"a", {1, Document{{"0", 1}}}}}; visitAllValuesAtPath(doc, FieldPath("a.0"), callback); ASSERT_EQ(values.size(), 1UL); ASSERT_EQ(values.count(Value(1)), 1UL); @@ -160,10 +160,7 @@ TEST(VisitAllValuesAtPathTest, UsesNumericFieldNameToExtractElementFromArray) { TEST(VisitAllValuesAtPathTest, TreatsNegativeIndexAsFieldName) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{ - {"a", - vector<Value>{ - Value(0), Value(1), Value(Document{{"-1", "target"_sd}}), Value(Document{{"b", 3}})}}}; + Document doc{{"a", {0, 1, Document{{"-1", "target"_sd}}, Document{{"b", 3}}}}}; visitAllValuesAtPath(doc, FieldPath("a.-1"), callback); ASSERT_EQ(values.size(), 1UL); ASSERT_EQ(values.count(Value("target"_sd)), 1UL); @@ -172,8 +169,7 @@ TEST(VisitAllValuesAtPathTest, TreatsNegativeIndexAsFieldName) { TEST(VisitAllValuesAtPathTest, ExtractsNoValuesFromOutOfBoundsIndex) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{ - {"a", vector<Value>{Value(1), Value(Document{{"b", 2}}), Value(Document{{"10", 3}})}}}; + Document doc{{"a", {1, Document{{"b", 2}}, Document{{"10", 3}}}}}; visitAllValuesAtPath(doc, FieldPath("a.10"), callback); ASSERT_EQ(values.size(), 0UL); } @@ -181,10 +177,7 @@ TEST(VisitAllValuesAtPathTest, ExtractsNoValuesFromOutOfBoundsIndex) { TEST(VisitAllValuesAtPathTest, DoesNotTreatHexStringAsIndexSpecification) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{{"a", - vector<Value>{Value(1), - Value(Document{{"0x2", 2}}), - Value(Document{{"NOT THIS ONE", 3}})}}}; + Document doc{{"a", {1, Document{{"0x2", 2}}, Document{{"NOT THIS ONE", 3}}}}}; visitAllValuesAtPath(doc, FieldPath("a.0x2"), callback); ASSERT_EQ(values.size(), 1UL); ASSERT_EQ(values.count(Value(2)), 1UL); @@ -193,9 +186,7 @@ TEST(VisitAllValuesAtPathTest, DoesNotTreatHexStringAsIndexSpecification) { TEST(VisitAllValuesAtPathTest, DoesNotAcceptLeadingPlusAsArrayIndex) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{{"a", - vector<Value>{ - Value(1), Value(Document{{"+2", 2}}), Value(Document{{"NOT THIS ONE", 3}})}}}; + Document doc{{"a", {1, Document{{"+2", 2}}, Document{{"NOT THIS ONE", 3}}}}}; visitAllValuesAtPath(doc, FieldPath("a.+2"), callback); ASSERT_EQ(values.size(), 1UL); ASSERT_EQ(values.count(Value(2)), 1UL); @@ -204,10 +195,7 @@ TEST(VisitAllValuesAtPathTest, DoesNotAcceptLeadingPlusAsArrayIndex) { TEST(VisitAllValuesAtPathTest, DoesNotAcceptTrailingCharactersForArrayIndex) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{{"a", - vector<Value>{Value(1), - Value(Document{{"2xyz", 2}}), - Value(Document{{"NOT THIS ONE", 3}})}}}; + Document doc{{"a", {1, Document{{"2xyz", 2}}, Document{{"NOT THIS ONE", 3}}}}}; visitAllValuesAtPath(doc, FieldPath("a.2xyz"), callback); ASSERT_EQ(values.size(), 1UL); ASSERT_EQ(values.count(Value(2)), 1UL); @@ -216,10 +204,7 @@ TEST(VisitAllValuesAtPathTest, DoesNotAcceptTrailingCharactersForArrayIndex) { TEST(VisitAllValuesAtPathTest, DoesNotAcceptNonDigitsForArrayIndex) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{{"a", - vector<Value>{Value(1), - Value(Document{{"2x4", 2}}), - Value(Document{{"NOT THIS ONE", 3}})}}}; + Document doc{{"a", {1, Document{{"2x4", 2}}, Document{{"NOT THIS ONE", 3}}}}}; visitAllValuesAtPath(doc, FieldPath("a.2x4"), callback); ASSERT_EQ(values.size(), 1UL); ASSERT_EQ(values.count(Value(2)), 1UL); @@ -229,8 +214,7 @@ TEST(VisitAllValuesAtPathTest, DoesExtractNestedValuesFromWithinArraysTraversedWithPositionalPaths) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{ - {"a", vector<Value>{Value(1), Value(Document{{"2", 2}}), Value(Document{{"target", 3}})}}}; + Document doc{{"a", {1, Document{{"2", 2}}, Document{{"target", 3}}}}}; visitAllValuesAtPath(doc, FieldPath("a.2.target"), callback); ASSERT_EQ(values.size(), 1UL); ASSERT_EQ(values.count(Value(3)), 1UL); @@ -275,7 +259,7 @@ TEST(VisitAllValuesAtPathTest, DoesNotAddMissingValueToResults) { TEST(VisitAllValuesAtPathTest, DoesNotAddMissingValueWithinArrayToResults) { auto values = kDefaultValueComparator.makeUnorderedValueSet(); auto callback = [&values](const Value& val) { values.insert(val); }; - Document doc{{"a", vector<Value>{Value(1), Value(), Value(2)}}}; + Document doc{{"a", {1, Value(), 2}}}; visitAllValuesAtPath(doc, FieldPath("a"), callback); ASSERT_EQ(values.size(), 2UL); ASSERT_EQ(values.count(Value(1)), 1UL); diff --git a/src/mongo/db/pipeline/document_source_change_stream_test.cpp b/src/mongo/db/pipeline/document_source_change_stream_test.cpp index 678ef85676f..c4ddc661be0 100644 --- a/src/mongo/db/pipeline/document_source_change_stream_test.cpp +++ b/src/mongo/db/pipeline/document_source_change_stream_test.cpp @@ -865,13 +865,13 @@ TEST_F(ChangeStreamStageTest, TransformRemoveFields) { {DSChangeStream::kOperationTypeField, DSChangeStream::kUpdateOpType}, {DSChangeStream::kClusterTimeField, kDefaultTs}, {DSChangeStream::kNamespaceField, D{{"db", nss.db()}, {"coll", nss.coll()}}}, - {DSChangeStream::kDocumentKeyField, D{{{"_id", 1}, {"x", 2}}}}, + {DSChangeStream::kDocumentKeyField, Document{{"_id", 1}, {"x", 2}}}, { "updateDescription", - D{{"updatedFields", D{}}, {"removedFields", vector<V>{V("y"_sd)}}}, + D{{"updatedFields", D{}}, {"removedFields", {"y"_sd}}}, }}; checkTransformation(removeField, expectedRemoveField); -} +} // namespace TEST_F(ChangeStreamStageTest, TransformReplace) { BSONObj o = BSON("_id" << 1 << "x" << 2 << "y" << 1); @@ -2014,10 +2014,10 @@ TEST_F(ChangeStreamStageDBTest, TransformRemoveFields) { {DSChangeStream::kOperationTypeField, DSChangeStream::kUpdateOpType}, {DSChangeStream::kClusterTimeField, kDefaultTs}, {DSChangeStream::kNamespaceField, D{{"db", nss.db()}, {"coll", nss.coll()}}}, - {DSChangeStream::kDocumentKeyField, D{{{"_id", 1}, {"x", 2}}}}, + {DSChangeStream::kDocumentKeyField, D{{"_id", 1}, {"x", 2}}}, { "updateDescription", - D{{"updatedFields", D{}}, {"removedFields", vector<V>{V("y"_sd)}}}, + D{{"updatedFields", D{}}, {"removedFields", {"y"_sd}}}, }}; checkTransformation(removeField, expectedRemoveField); } diff --git a/src/mongo/db/pipeline/document_source_graph_lookup_test.cpp b/src/mongo/db/pipeline/document_source_graph_lookup_test.cpp index c59ed9c2ea4..3b1299ec9be 100644 --- a/src/mongo/db/pipeline/document_source_graph_lookup_test.cpp +++ b/src/mongo/db/pipeline/document_source_graph_lookup_test.cpp @@ -491,8 +491,7 @@ TEST_F(DocumentSourceGraphLookUpTest, ShouldExpandArraysAtEndOfConnectFromField) * \ / * `> 3 ' */ - Document startDoc{{"_id", 0}, - {"to", std::vector<Value>{Value(1), Value(2), Value(3)}}}; // Note the array. + Document startDoc{{"_id", 0}, {"to", std::vector{1, 2, 3}}}; Document middle1{{"_id", 1}, {"to", 4}}; Document middle2{{"_id", 2}, {"to", 4}}; Document middle3{{"_id", 3}, {"to", 4}}; diff --git a/src/mongo/db/pipeline/document_source_lookup.cpp b/src/mongo/db/pipeline/document_source_lookup.cpp index 94a4071d4ad..78e4d59d077 100644 --- a/src/mongo/db/pipeline/document_source_lookup.cpp +++ b/src/mongo/db/pipeline/document_source_lookup.cpp @@ -732,10 +732,10 @@ void DocumentSourceLookUp::serializeToArray( {"pipeline", pipeline}}}}; } else { doc = Document{{getSourceName(), - {Document{{"from", fromValue}, - {"as", _as.fullPath()}, - {"localField", _localField->fullPath()}, - {"foreignField", _foreignField->fullPath()}}}}}; + Document{{"from", fromValue}, + {"as", _as.fullPath()}, + {"localField", _localField->fullPath()}, + {"foreignField", _foreignField->fullPath()}}}}; } MutableDocument output(doc); diff --git a/src/mongo/db/pipeline/document_source_lookup_test.cpp b/src/mongo/db/pipeline/document_source_lookup_test.cpp index baa1fd032d0..5d8bedf72b4 100644 --- a/src/mongo/db/pipeline/document_source_lookup_test.cpp +++ b/src/mongo/db/pipeline/document_source_lookup_test.cpp @@ -726,17 +726,15 @@ TEST_F(DocumentSourceLookUpTest, ShouldPropagatePauses) { auto next = lookup->getNext(); ASSERT_TRUE(next.isAdvanced()); - ASSERT_DOCUMENT_EQ( - next.releaseDocument(), - (Document{{"foreignId", 0}, {"foreignDocs", vector<Value>{Value(Document{{"_id", 0}})}}})); + ASSERT_DOCUMENT_EQ(next.releaseDocument(), + (Document{{"foreignId", 0}, {"foreignDocs", {Document{{"_id", 0}}}}})); ASSERT_TRUE(lookup->getNext().isPaused()); next = lookup->getNext(); ASSERT_TRUE(next.isAdvanced()); - ASSERT_DOCUMENT_EQ( - next.releaseDocument(), - (Document{{"foreignId", 1}, {"foreignDocs", vector<Value>{Value(Document{{"_id", 1}})}}})); + ASSERT_DOCUMENT_EQ(next.releaseDocument(), + (Document{{"foreignId", 1}, {"foreignDocs", {Document{{"_id", 1}}}}})); ASSERT_TRUE(lookup->getNext().isPaused()); diff --git a/src/mongo/db/pipeline/expression_test.cpp b/src/mongo/db/pipeline/expression_test.cpp index e1eeb44d3d2..c5deffd22e8 100644 --- a/src/mongo/db/pipeline/expression_test.cpp +++ b/src/mongo/db/pipeline/expression_test.cpp @@ -67,7 +67,7 @@ static Value evaluateExpression(const string& expressionName, const vector<ImplicitValue>& operands) { auto expCtx = ExpressionContextForTest{}; VariablesParseState vps = expCtx.variablesParseState; - const BSONObj obj = BSON(expressionName << ImplicitValue::convertToValue(operands)); + const BSONObj obj = BSON(expressionName << Value(ImplicitValue::convertToValues(operands))); auto expression = Expression::parseExpression(&expCtx, obj, vps); Value result = expression->evaluate({}, &expCtx.variables); return result; @@ -80,14 +80,14 @@ static Value evaluateExpression(const string& expressionName, */ static void assertExpectedResults( const string& expression, - initializer_list<pair<vector<ImplicitValue>, ImplicitValue>> operations) { + initializer_list<pair<initializer_list<ImplicitValue>, ImplicitValue>> operations) { for (auto&& op : operations) { try { Value result = evaluateExpression(expression, op.first); ASSERT_VALUE_EQ(op.second, result); ASSERT_EQUALS(op.second.getType(), result.getType()); } catch (...) { - LOGV2(24188, "failed", "argument"_attr = ImplicitValue::convertToValue(op.first)); + LOGV2(24188, "failed", "argument"_attr = ImplicitValue::convertToValues(op.first)); throw; } } @@ -152,7 +152,7 @@ TEST(ExpressionArrayToObjectTest, KVFormatSimple) { << BSON("k" << "key2" << "v" << 3)))}, - {Value(BSON("key1" << 2 << "key2" << 3))}}}); + Value(BSON("key1" << 2 << "key2" << 3))}}); } TEST(ExpressionArrayToObjectTest, KVFormatWithDuplicates) { @@ -163,19 +163,19 @@ TEST(ExpressionArrayToObjectTest, KVFormatWithDuplicates) { << BSON("k" << "hi" << "v" << 3)))}, - {Value(BSON("hi" << 3))}}}); + Value(BSON("hi" << 3))}}); } TEST(ExpressionArrayToObjectTest, ListFormatSimple) { assertExpectedResults("$arrayToObject", {{{Value(BSON_ARRAY(BSON_ARRAY("key1" << 2) << BSON_ARRAY("key2" << 3)))}, - {Value(BSON("key1" << 2 << "key2" << 3))}}}); + Value(BSON("key1" << 2 << "key2" << 3))}}); } TEST(ExpressionArrayToObjectTest, ListFormWithDuplicates) { assertExpectedResults("$arrayToObject", {{{Value(BSON_ARRAY(BSON_ARRAY("key1" << 2) << BSON_ARRAY("key1" << 3)))}, - {Value(BSON("key1" << 3))}}}); + Value(BSON("key1" << 3))}}); } /* ------------------------ ExpressionRange --------------------------- */ @@ -2103,7 +2103,7 @@ TEST(BuiltinRemoveVariableTest, RemoveSerializesCorrectlyAfterOptimization) { namespace ExpressionMergeObjects { TEST(ExpressionMergeObjects, MergingWithSingleObjectShouldLeaveUnchanged) { - assertExpectedResults("$mergeObjects", {{{}, {Document({})}}}); + assertExpectedResults("$mergeObjects", {{{}, Document({})}}); auto doc = Document({{"a", 1}, {"b", 1}}); assertExpectedResults("$mergeObjects", {{{doc}, doc}}); |