From c5ac5b79af1a34b914ac67de302f86744e23856b Mon Sep 17 00:00:00 2001 From: eshcherbin Date: Mon, 3 Feb 2025 10:23:16 +0300 Subject: Make TCompactFlatMap formattable commit_hash:e5c092a9d0375881554f1d3bc905c662070956bf --- library/cpp/yt/compact_containers/compact_flat_map.h | 2 +- library/cpp/yt/string/format-inl.h | 5 +++++ library/cpp/yt/string/unittests/format_ut.cpp | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) (limited to 'library/cpp') diff --git a/library/cpp/yt/compact_containers/compact_flat_map.h b/library/cpp/yt/compact_containers/compact_flat_map.h index d4e93668430..49c8628c46c 100644 --- a/library/cpp/yt/compact_containers/compact_flat_map.h +++ b/library/cpp/yt/compact_containers/compact_flat_map.h @@ -20,7 +20,7 @@ concept CComparisonAllowed = std::same_as || CHasIsTransparentFlag #include +#include #include @@ -163,6 +164,10 @@ template constexpr bool CKnownKVRange> = true; template constexpr bool CKnownKVRange> = true; +template +constexpr bool CKnownKVRange> = true; +template +constexpr bool CKnownKVRange> = true; // TODO(arkady-e1ppa): Uncomment me when // https://github.com/llvm/llvm-project/issues/58534 is shipped. diff --git a/library/cpp/yt/string/unittests/format_ut.cpp b/library/cpp/yt/string/unittests/format_ut.cpp index f783efcf11a..e2e23c737c2 100644 --- a/library/cpp/yt/string/unittests/format_ut.cpp +++ b/library/cpp/yt/string/unittests/format_ut.cpp @@ -70,6 +70,7 @@ static_assert(CFormattable>); static_assert(CFormattable>); static_assert(CFormattable>); static_assert(CFormattable>); +static_assert(CFormattable>); static_assert(CFormattable>); static_assert(CFormattable>); static_assert(CFormattable); -- cgit v1.3 From a19aaf0e98329e8fff4e16626c2f8212f7d87789 Mon Sep 17 00:00:00 2001 From: artyasen Date: Mon, 3 Feb 2025 15:57:35 +0300 Subject: Получение XML аттрибута без исключений MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add no exception methods commit_hash:245a52ca795a16ad57a9ac642b0cd00ca0122a32 --- library/cpp/xml/document/node-attr.h | 12 ++++++++++++ library/cpp/xml/document/xml-document-decl.h | 18 ++++++++++++++++++ library/cpp/xml/document/xml-document_ut.cpp | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+) (limited to 'library/cpp') diff --git a/library/cpp/xml/document/node-attr.h b/library/cpp/xml/document/node-attr.h index 1378ffdfbff..6f5445d4c2d 100644 --- a/library/cpp/xml/document/node-attr.h +++ b/library/cpp/xml/document/node-attr.h @@ -44,6 +44,18 @@ namespace NXml { return t; } + template + TMaybe TNode::TryAttr(TZtStringBuf name) const { + TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + if (!value) { + return Nothing(); + } + + T t; + AttrInternal(value, t, name); + return t; + } + template T TNode::Attr(TZtStringBuf name, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); diff --git a/library/cpp/xml/document/xml-document-decl.h b/library/cpp/xml/document/xml-document-decl.h index bfda1fb7e6e..643ba664680 100644 --- a/library/cpp/xml/document/xml-document-decl.h +++ b/library/cpp/xml/document/xml-document-decl.h @@ -325,6 +325,14 @@ namespace NXml { template T Attr(TZtStringBuf name) const; + /** + * get node attribute + * @param name: attribute name + * returns value if exists + */ + template + TMaybe TryAttr(TZtStringBuf name) const; + /** * get node attribute * @param name: attribute name @@ -597,6 +605,16 @@ namespace NXml { return ActualNode.NextSibling(); } + /** + * get node attribute + * @param name: attribute name + * returns value if exists + */ + template + TMaybe TryAttr(TZtStringBuf name) const { + return ActualNode.TryAttr(name); + } + /** * get node attribute * @param name: attribute name diff --git a/library/cpp/xml/document/xml-document_ut.cpp b/library/cpp/xml/document/xml-document_ut.cpp index 9f537b75c4c..0ec1fc60838 100644 --- a/library/cpp/xml/document/xml-document_ut.cpp +++ b/library/cpp/xml/document/xml-document_ut.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "xml-document.h" @@ -35,6 +36,27 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { NXml::TConstNode text = root.Node("text"); UNIT_ASSERT_EQUAL(text.Value(), "Некоторый текст"); } + Y_UNIT_TEST(GetAttributes) { + NXml::TDocument xml(R"( + + hello world + Некоторый текст + + )", + NXml::TDocument::String); + + NXml::TConstNode root = xml.Root(); + NXml::TConstNode b = root.Node("a/b"); + + UNIT_ASSERT_EXCEPTION_CONTAINS(b.Attr("unknown attrib"), yexception, "@unknown attrib"); + + const auto unknownAttr = b.TryAttr("unknown attrib"); + UNIT_ASSERT(unknownAttr.Empty()); + + const auto knownAttr = b.TryAttr("len"); + UNIT_ASSERT(knownAttr.Defined()); + UNIT_ASSERT_EQUAL(knownAttr.GetRef(), 15); + } Y_UNIT_TEST(SerializeString) { NXml::TDocument xml("frob", NXml::TDocument::RootName); xml.Root().SetAttr("xyzzy", "Frobozz"); -- cgit v1.3