diff options
author | Mikhail Borisov <borisov.mikhail@gmail.com> | 2022-02-10 16:45:40 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:40 +0300 |
commit | 5d50718e66d9c037dc587a0211110b7d25a66185 (patch) | |
tree | e98df59de24d2ef7c77baed9f41e4875a2fef972 /library/cpp/yson/node | |
parent | a6a92afe03e02795227d2641b49819b687f088f8 (diff) | |
download | ydb-5d50718e66d9c037dc587a0211110b7d25a66185.tar.gz |
Restoring authorship annotation for Mikhail Borisov <borisov.mikhail@gmail.com>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/yson/node')
-rw-r--r-- | library/cpp/yson/node/node.cpp | 8 | ||||
-rw-r--r-- | library/cpp/yson/node/node.h | 104 | ||||
-rw-r--r-- | library/cpp/yson/node/node_ut.cpp | 52 |
3 files changed, 82 insertions, 82 deletions
diff --git a/library/cpp/yson/node/node.cpp b/library/cpp/yson/node/node.cpp index 5917d73fbc..b39e070718 100644 --- a/library/cpp/yson/node/node.cpp +++ b/library/cpp/yson/node/node.cpp @@ -442,7 +442,7 @@ const TNode& TNode::At(size_t index) const { CheckType(List); const auto& list = std::get<TListType>(Value_); if (index >= list.size()) { - ythrow TLookupError() << "List out-of-range: requested index=" << index << ", but size=" << list.size(); + ythrow TLookupError() << "List out-of-range: requested index=" << index << ", but size=" << list.size(); } return list[index]; } @@ -451,7 +451,7 @@ TNode& TNode::At(size_t index) { CheckType(List); auto& list = std::get<TListType>(Value_); if (index >= list.size()) { - ythrow TLookupError() << "List out-of-range: requested index=" << index << ", but size=" << list.size(); + ythrow TLookupError() << "List out-of-range: requested index=" << index << ", but size=" << list.size(); } return list[index]; } @@ -545,7 +545,7 @@ const TNode& TNode::At(const TStringBuf key) const { const auto& map = std::get<TMapType>(Value_); TMapType::const_iterator i = map.find(key); if (i == map.end()) { - ythrow TLookupError() << "Cannot find key " << key; + ythrow TLookupError() << "Cannot find key " << key; } else { return i->second; } @@ -556,7 +556,7 @@ TNode& TNode::At(const TStringBuf key) { auto& map = std::get<TMapType>(Value_); TMapType::iterator i = map.find(key); if (i == map.end()) { - ythrow TLookupError() << "Cannot find key " << key; + ythrow TLookupError() << "Cannot find key " << key; } else { return i->second; } diff --git a/library/cpp/yson/node/node.h b/library/cpp/yson/node/node.h index 2873eea4df..5f90f95df0 100644 --- a/library/cpp/yson/node/node.h +++ b/library/cpp/yson/node/node.h @@ -22,10 +22,10 @@ namespace NYT { class TNode { public: - class TLookupError - : public TWithBackTrace<yexception> - { }; - + class TLookupError + : public TWithBackTrace<yexception> + { }; + class TTypeError : public TWithBackTrace<yexception> { }; @@ -223,12 +223,12 @@ public: template<typename T> T ChildConvertTo(const TStringBuf key) const; - template<typename T> - const T& ChildAs(const TStringBuf key) const; - - template<typename T> - T& ChildAs(const TStringBuf key); - + template<typename T> + const T& ChildAs(const TStringBuf key) const; + + template<typename T> + T& ChildAs(const TStringBuf key); + // list getters // works the same way like simple getters const TString& ChildAsString(size_t index) const; @@ -247,13 +247,13 @@ public: template<typename T> T ChildConvertTo(size_t index) const; - template<typename T> - const T& ChildAs(size_t index) const; + template<typename T> + const T& ChildAs(size_t index) const; + + template<typename T> + T& ChildAs(size_t index); + - template<typename T> - T& ChildAs(size_t index); - - // attributes bool HasAttributes() const; void ClearAttributes(); @@ -458,42 +458,42 @@ inline T TNode::ChildConvertTo(size_t index) const { } template<typename T> -inline const T& TNode::ChildAs(const TStringBuf key) const { - const auto& node = At(key); - try { - return node.As<T>(); - } catch (TTypeError& e) { - e << ", during getting key=" << key; - throw e; - } catch (...) { - ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key; - } -} - -template<typename T> -inline const T& TNode::ChildAs(size_t index) const { - const auto& node = At(index); - try { - return node.As<T>(); - } catch (TTypeError& e) { - e << ", during getting index=" << index; - throw e; - } catch (...) { - ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index; - } -} - -template<typename T> -inline T& TNode::ChildAs(const TStringBuf key) { - return const_cast<T&>(static_cast<const TNode*>(this)->ChildAs<T>(key)); -} - -template<typename T> -inline T& TNode::ChildAs(size_t index) { - return const_cast<T&>(static_cast<const TNode*>(this)->ChildAs<T>(index)); -} - -template<typename T> +inline const T& TNode::ChildAs(const TStringBuf key) const { + const auto& node = At(key); + try { + return node.As<T>(); + } catch (TTypeError& e) { + e << ", during getting key=" << key; + throw e; + } catch (...) { + ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key; + } +} + +template<typename T> +inline const T& TNode::ChildAs(size_t index) const { + const auto& node = At(index); + try { + return node.As<T>(); + } catch (TTypeError& e) { + e << ", during getting index=" << index; + throw e; + } catch (...) { + ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index; + } +} + +template<typename T> +inline T& TNode::ChildAs(const TStringBuf key) { + return const_cast<T&>(static_cast<const TNode*>(this)->ChildAs<T>(key)); +} + +template<typename T> +inline T& TNode::ChildAs(size_t index) { + return const_cast<T&>(static_cast<const TNode*>(this)->ChildAs<T>(index)); +} + +template<typename T> inline bool TNode::IsOfType() const noexcept { return std::holds_alternative<T>(Value_); } diff --git a/library/cpp/yson/node/node_ut.cpp b/library/cpp/yson/node/node_ut.cpp index 7e081e1afb..448e99f575 100644 --- a/library/cpp/yson/node/node_ut.cpp +++ b/library/cpp/yson/node/node_ut.cpp @@ -414,34 +414,34 @@ Y_UNIT_TEST_SUITE(YtNodeTest) { ("double", -3.5) ("list", TNode::CreateList().Add(5)) ("map", TNode::CreateMap()("key", "value")); - - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TString>("string"), "7"); + + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TString>("string"), "7"); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsString("string"), "7"); UNIT_ASSERT_VALUES_EQUAL(node.ChildConvertTo<i64>("string"), 7); - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<i64>("int64"), 3); + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<i64>("int64"), 3); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsInt64("int64"), 3); UNIT_ASSERT_VALUES_EQUAL(node.ChildIntCast<ui64>("int64"), 3u); - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<ui64>("uint64"), 5u); + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<ui64>("uint64"), 5u); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsUint64("uint64"), 5u); UNIT_ASSERT_VALUES_EQUAL(node.ChildIntCast<i64>("uint64"), 5); UNIT_ASSERT_VALUES_EQUAL(node.ChildConvertTo<TString>("uint64"), "5"); - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<double>("double"), -3.5); + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<double>("double"), -3.5); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsDouble("double"), -3.5); UNIT_ASSERT_VALUES_EQUAL(node.ChildConvertTo<TString>("double"), "-3.5"); - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TNode::TListType>("list")[0].AsInt64(), 5); + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TNode::TListType>("list")[0].AsInt64(), 5); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsList("list")[0].AsInt64(), 5); - - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TNode::TMapType>("map")["key"].AsString(), "value"); + + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TNode::TMapType>("map")["key"].AsString(), "value"); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsMap("map")["key"].AsString(), "value"); - - // mutable accessor - auto& childString = node.ChildAs<TString>("string"); - childString = "yaddayadda"; - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TString>("string"), "yaddayadda"); + + // mutable accessor + auto& childString = node.ChildAs<TString>("string"); + childString = "yaddayadda"; + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TString>("string"), "yaddayadda"); } Y_UNIT_TEST(TestListGetters) { @@ -452,33 +452,33 @@ Y_UNIT_TEST_SUITE(YtNodeTest) { .Add(-3.5) .Add(TNode::CreateList().Add(5)) .Add(TNode::CreateMap()("key", "value")); - - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TString>(0), "7"); + + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TString>(0), "7"); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsString(0), "7"); UNIT_ASSERT_VALUES_EQUAL(node.ChildConvertTo<i64>(0), 7); - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<i64>(1), 3); + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<i64>(1), 3); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsInt64(1), 3); UNIT_ASSERT_VALUES_EQUAL(node.ChildIntCast<ui64>(1), 3u); - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<ui64>(2), 5u); + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<ui64>(2), 5u); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsUint64(2), 5u); UNIT_ASSERT_VALUES_EQUAL(node.ChildIntCast<i64>(2), 5); UNIT_ASSERT_VALUES_EQUAL(node.ChildConvertTo<TString>(2), "5"); - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<double>(3), -3.5); + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<double>(3), -3.5); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsDouble(3), -3.5); UNIT_ASSERT_VALUES_EQUAL(node.ChildConvertTo<TString>(3), "-3.5"); - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TNode::TListType>(4)[0].AsInt64(), 5); + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TNode::TListType>(4)[0].AsInt64(), 5); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsList(4)[0].AsInt64(), 5); - - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TNode::TMapType>(5)["key"].AsString(), "value"); + + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TNode::TMapType>(5)["key"].AsString(), "value"); UNIT_ASSERT_VALUES_EQUAL(node.ChildAsMap(5)["key"].AsString(), "value"); - - // mutable accessor - auto& childString = node.ChildAs<TString>(0); - childString = "yaddayadda"; - UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TString>(0), "yaddayadda"); + + // mutable accessor + auto& childString = node.ChildAs<TString>(0); + childString = "yaddayadda"; + UNIT_ASSERT_VALUES_EQUAL(node.ChildAs<TString>(0), "yaddayadda"); } } |