diff options
author | grigminakov <grigminakov@yandex-team.com> | 2024-10-11 10:15:40 +0300 |
---|---|---|
committer | grigminakov <grigminakov@yandex-team.com> | 2024-10-11 10:25:25 +0300 |
commit | 3f579e6b3e904b26d224de2f24da6d8226d26530 (patch) | |
tree | 6bf3e1fa2916c56c2155abb1837a6bf4798106a5 | |
parent | e7ae2d617562e731cd6770c5f903adc9a60a7386 (diff) | |
download | ydb-3f579e6b3e904b26d224de2f24da6d8226d26530.tar.gz |
YT: Support DoAttributes in fluent yson builder
commit_hash:bc040fe60da2c2ac38e9fa90f43169a08ccd32db
-rw-r--r-- | yt/yt/core/ytree/fluent.h | 11 | ||||
-rw-r--r-- | yt/yt/core/ytree/unittests/ytree_fluent_ut.cpp | 39 |
2 files changed, 43 insertions, 7 deletions
diff --git a/yt/yt/core/ytree/fluent.h b/yt/yt/core/ytree/fluent.h index ec0c51fd7e..366ca9794d 100644 --- a/yt/yt/core/ytree/fluent.h +++ b/yt/yt/core/ytree/fluent.h @@ -88,7 +88,8 @@ namespace NYT::NYTree { // and close map; // * DoList(TFuncList func) -> TAny, same as DoMap(); // * DoListFor(TCollection collection, TFuncList func) -> TAny; same as DoMapFor(). -// +// * DoAttributes(TFuncAttributes func) -> TAny, open attributes, delegate invocation +// to a separate procedure and close attributes; // // TFluentMap: // * Item(TStringBuf key) -> TAny, open an element keyed with `key`; @@ -407,6 +408,14 @@ public: this->Consumer, TAnyWithoutAttributes<TParent>(this->Consumer, std::move(this->Parent))); } + + TAnyWithoutAttributes<TParent> DoAttributes(auto funcMap) + { + this->Consumer->OnBeginAttributes(); + InvokeFluentFunc<TFluentAttributes<TFluentYsonVoid>>(funcMap, this->Consumer); + this->Consumer->OnEndAttributes(); + return TAnyWithoutAttributes<TParent>(this->Consumer, std::move(this->Parent)); + } }; template <class TParent = TFluentYsonVoid> diff --git a/yt/yt/core/ytree/unittests/ytree_fluent_ut.cpp b/yt/yt/core/ytree/unittests/ytree_fluent_ut.cpp index 51f43f0e07..8b2e9b8970 100644 --- a/yt/yt/core/ytree/unittests/ytree_fluent_ut.cpp +++ b/yt/yt/core/ytree/unittests/ytree_fluent_ut.cpp @@ -15,8 +15,6 @@ using ::testing::StrictMock; //////////////////////////////////////////////////////////////////////////////// -// TODO(sandello): Fix this test under clang. -#ifndef __clang__ // String-like Scalars {{{ //////////////////////////////////////////////////////////////////////////////// @@ -187,7 +185,7 @@ TEST(TYTreeFluentMapTest, Items) StrictMock<TMockYsonConsumer> mock; InSequence dummy; - auto node = ConvertToNode(TYsonString("{bar = 10}")); + auto node = ConvertToNode(TYsonString(TString("{bar = 10}"))); EXPECT_CALL(mock, OnBeginMap()); EXPECT_CALL(mock, OnKeyedItem("bar")); @@ -275,7 +273,7 @@ TEST(TYTreeFluentListTest, Items) StrictMock<TMockYsonConsumer> mock; InSequence dummy; - auto node = ConvertToNode(TYsonString("[10; 20; 30]")); + auto node = ConvertToNode(TYsonString(TString("[10; 20; 30]"))); EXPECT_CALL(mock, OnBeginList()); EXPECT_CALL(mock, OnListItem()); @@ -420,8 +418,37 @@ TEST(TYTreeFluentTest, Complex) .EndList(); } -//////////////////////////////////////////////////////////////////////////////// -#endif +TEST(TYTreeFluentTest, DoMap) +{ + StrictMock<TMockYsonConsumer> mock; + + EXPECT_CALL(mock, OnBeginMap()); + EXPECT_CALL(mock, OnKeyedItem("key")); + EXPECT_CALL(mock, OnEntity); + EXPECT_CALL(mock, OnEndMap()); + + BuildYsonFluently(&mock) + .DoMap([] (TFluentMap map) { + map.Item("key").Entity(); + }); +} + +TEST(TYTreeFluentTest, DoAttributes) +{ + StrictMock<TMockYsonConsumer> mock; + + EXPECT_CALL(mock, OnBeginAttributes()); + EXPECT_CALL(mock, OnKeyedItem("key")); + EXPECT_CALL(mock, OnStringScalar("value")); + EXPECT_CALL(mock, OnEndAttributes()); + EXPECT_CALL(mock, OnEntity()); + + BuildYsonFluently(&mock) + .DoAttributes([] (TFluentAttributes attributes) { + attributes.Item("key").Value("value"); + }) + .Entity(); +} //////////////////////////////////////////////////////////////////////////////// |