diff options
author | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-09-02 13:47:05 +0300 |
---|---|---|
committer | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-09-02 14:10:50 +0300 |
commit | a10178d5e96298b82a130fce1560b3b1914d928d (patch) | |
tree | 3ac685e3d39fd804e9c0781caef5bf5f5e75efa5 | |
parent | 07991279b3ddf89ef2a7baec78a8ad93e5bc7fbb (diff) | |
download | ydb-a10178d5e96298b82a130fce1560b3b1914d928d.tar.gz |
YT-22617: Support YsonString fields
bbfefec4e98650992f244261e26626c3c28ce1f2
-rw-r--r-- | yt/yt/core/ytree/unittests/yson_struct_ut.cpp | 80 | ||||
-rw-r--r-- | yt/yt/core/ytree/yson_struct_detail-inl.h | 20 |
2 files changed, 100 insertions, 0 deletions
diff --git a/yt/yt/core/ytree/unittests/yson_struct_ut.cpp b/yt/yt/core/ytree/unittests/yson_struct_ut.cpp index dd8b43d002..59d1bd7c10 100644 --- a/yt/yt/core/ytree/unittests/yson_struct_ut.cpp +++ b/yt/yt/core/ytree/unittests/yson_struct_ut.cpp @@ -2574,6 +2574,86 @@ TEST(TYsonStructTest, OuterYsonStructWithValidation) EXPECT_EQ(deserialized->Inner->MyInt, 42); } +struct TWithYsonString + : public TYsonStructLite +{ + NYson::TYsonString MyString; + + REGISTER_YSON_STRUCT_LITE(TWithYsonString); + + static void Register(TRegistrar registrar) + { + registrar.Parameter("my_string", &TThis::MyString) + .Default(); + } +}; + +TEST(TYsonStructTest, TYsonStringFieldSimple) +{ + TWithYsonString value; + + { + auto node = BuildYsonNodeFluently() + .BeginMap() + .Item("my_string").Value(ConvertToYsonString(42)) + .EndMap(); + + Deserialize(value, node->AsMap()); + EXPECT_TRUE(value.MyString); + EXPECT_EQ(ConvertTo<i32>(value.MyString), 42); + } + + { + std::string message{"Hi mom!"}; + + auto node = BuildYsonNodeFluently() + .BeginMap() + .Item("my_string").Value(ConvertToYsonString(message)) + .EndMap(); + + Deserialize(value, node->AsMap()); + EXPECT_TRUE(value.MyString); + EXPECT_EQ(ConvertTo<std::string>(value.MyString), message); + } + + { + auto config = New<TTestConfig>(); + config->MyString = "Hello, world!"; + + auto node = BuildYsonNodeFluently() + .BeginMap() + .Item("my_string").Value(ConvertToYsonString(config)) + .EndMap(); + + Deserialize(value, node->AsMap()); + EXPECT_TRUE(value.MyString); + + auto extracted = ConvertTo<TTestConfigPtr>(value.MyString); + EXPECT_EQ(extracted->NullableInt, config->NullableInt); + EXPECT_EQ(extracted->MyString, extracted->MyString); + } +} + +TEST(TYsonStructTest, TYsonStringFieldCompound) +{ + TWithYsonString value; + + auto config = New<TTestConfig>(); + config->MyString = "Hello, world!"; + + auto node = BuildYsonNodeFluently() + .BeginMap() + .Item("my_string").Value(ConvertToYsonString(config)) + .EndMap(); + + Deserialize(value, node->AsMap()); + EXPECT_TRUE(value.MyString); + + auto extracted = ConvertTo<TTestConfigPtr>(value.MyString); + EXPECT_EQ(extracted->NullableInt, config->NullableInt); + EXPECT_EQ(extracted->MyString, extracted->MyString); +} + //////////////////////////////////////////////////////////////////////////////// struct TPolyBase diff --git a/yt/yt/core/ytree/yson_struct_detail-inl.h b/yt/yt/core/ytree/yson_struct_detail-inl.h index 1deebab6ca..c355e34e13 100644 --- a/yt/yt/core/ytree/yson_struct_detail-inl.h +++ b/yt/yt/core/ytree/yson_struct_detail-inl.h @@ -10,6 +10,8 @@ #include <yt/yt/core/yson/token_writer.h> +#include <library/cpp/yt/yson_string/string.h> + #include <library/cpp/yt/misc/wrapper_traits.h> namespace NYT::NYTree { @@ -225,6 +227,24 @@ void LoadFromSource( } } +// TYsonString +template <CYsonStructSource TSource> +void LoadFromSource( + ::NYT::NYson::TYsonString& parameter, + TSource source, + const NYPath::TYPath& path, + std::optional<EUnrecognizedStrategy> /*ignored*/) +{ + using TTraits = TYsonSourceTraits<TSource>; + + try { + parameter = NYson::ConvertToYsonString(TTraits::AsNode(source)); + } catch (const std::exception& ex) { + THROW_ERROR_EXCEPTION("Error loading parameter %v", path) + << ex; + } +} + // INodePtr template <CYsonStructSource TSource> void LoadFromSource( |