From 6bcd9416555b0506fb9227138c539317fb1b8fae Mon Sep 17 00:00:00 2001 From: Wei-Cheng Lin Date: Wed, 11 Dec 2024 18:46:08 -0800 Subject: [PATCH] PathVisitor: add hybrid test for access/set/get for thrift container key under hybrid node Summary: # Overview Using new defined test struct, adding test case for accessing with path visitor with the case of thrift path terminating at hybrid node Of the overall test cases listed below, this covers the combo of 2c x 3c,3d,3e D66525279 Update test struct definitions for comprehensive coverage of hybrid node access patterns for all visitors: a. Thrift path terminating at HybridNode (annotated member hybridMapOfI32ToStruct) b. Thrift path terminating at Thrift container (list, map, set, struct field in ChildStruct) under HybridNode c. Thrift path terminating at leaf primitive under HybridNode (primitive leaves under ChildStruct: leafEnum, optionalI32, childMap/, structMap//, listOfStruct/) d. [Existing UTs already cover] paths that terminate in COW leaf, COW nodes of various TCs Path Visitor UTs to cover following operations on access patterns in #2 above: a. modifyPath b. removePath c. Set d. Get e. Access (folly::dynamic) ref: https://fburl.com/gdoc/46krq4xp Differential Revision: D67049121 fbshipit-source-id: 3fa5d99845426f689853f5f915e55bac20b2bb9c --- .../visitors/tests/PathVisitorTests.cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/fboss/thrift_cow/visitors/tests/PathVisitorTests.cpp b/fboss/thrift_cow/visitors/tests/PathVisitorTests.cpp index c7f3af8de70de..382e990dfc5f8 100644 --- a/fboss/thrift_cow/visitors/tests/PathVisitorTests.cpp +++ b/fboss/thrift_cow/visitors/tests/PathVisitorTests.cpp @@ -266,6 +266,69 @@ TYPED_TEST(PathVisitorTests, AccessAtHybridThriftContainerTest) { EXPECT_EQ(getOp.val, newVal); } +TYPED_TEST(PathVisitorTests, AccessAtHybridThriftContainerKeyTest) { + RootTestStruct root; + ParentTestStruct parent; + auto testStruct = createSimpleTestStruct(); + parent.mapOfI32ToMapOfStruct() = {{3, {{"4", std::move(testStruct)}}}}; + root.mapOfI32ToMapOfStruct() = {{1, {{"2", std::move(parent)}}}}; + + auto nodeA = this->initNode(root); + folly::dynamic dyn; + auto processPath = pvlambda([&dyn](auto& node, auto begin, auto end) { + EXPECT_EQ(begin, end); + if constexpr (std::is_base_of_v< + Serializable, + std::remove_cvref_t>) { + dyn = node.toFollyDynamic(); + } else { + facebook::thrift::to_dynamic( + dyn, node, facebook::thrift::dynamic_format::JSON_1); + } + }); + + // Thrift path at thrift container key under HybridNode - Access + std::vector path{ + "mapOfI32ToMapOfStruct", + "1", + "2", + "mapOfI32ToMapOfStruct", + "3", + "4", + "hybridMapOfI32ToStruct", + "20", + "structMap", + "30"}; + auto result = RootPathVisitor::visit( + *nodeA, path.begin(), path.end(), PathVisitMode::LEAF, processPath); + EXPECT_EQ(result, ThriftTraverseResult::OK); + cfg::L4PortRange got = facebook::thrift::from_dynamic( + dyn, facebook::thrift::dynamic_format::JSON_1); + EXPECT_EQ(*got.min(), 100); + EXPECT_EQ(*got.max(), 200); + + // Thrift path at thrift container key under HybridNode - Set + using TC = apache::thrift::type_class::structure; + + cfg::L4PortRange newRange; + newRange.min() = 3000; + newRange.max() = 4000; + folly::fbstring newVal = + serialize(fsdb::OperProtocol::SIMPLE_JSON, newRange); + SetEncodedPathVisitorOperator setOp(fsdb::OperProtocol::SIMPLE_JSON, newVal); + + result = RootPathVisitor::visit( + *nodeA, path.begin(), path.end(), PathVisitMode::LEAF, setOp); + EXPECT_EQ(result, ThriftTraverseResult::OK); + + // Thrift path at thrift container key under HybridNode - Get + GetEncodedPathVisitorOperator getOp(fsdb::OperProtocol::SIMPLE_JSON); + result = RootPathVisitor::visit( + *nodeA, path.begin(), path.end(), PathVisitMode::LEAF, getOp); + EXPECT_EQ(result, ThriftTraverseResult::OK); + EXPECT_EQ(getOp.val, newVal); +} + TYPED_TEST(PathVisitorTests, AccessFieldInContainer) { auto structA = createSimpleTestStruct(); auto nodeA = std::make_shared