diff options
| author | artyasen <[email protected]> | 2025-02-03 15:57:35 +0300 | 
|---|---|---|
| committer | artyasen <[email protected]> | 2025-02-03 16:35:09 +0300 | 
| commit | a19aaf0e98329e8fff4e16626c2f8212f7d87789 (patch) | |
| tree | b262bcd535c5f29e36b701c95fa91c8beb32fcb6 /library/cpp/xml/document | |
| parent | cafd6740c55721b602590b6371e271bb41355e36 (diff) | |
Получение XML аттрибута без исключений
add no exception methods
commit_hash:245a52ca795a16ad57a9ac642b0cd00ca0122a32
Diffstat (limited to 'library/cpp/xml/document')
| -rw-r--r-- | library/cpp/xml/document/node-attr.h | 12 | ||||
| -rw-r--r-- | library/cpp/xml/document/xml-document-decl.h | 18 | ||||
| -rw-r--r-- | library/cpp/xml/document/xml-document_ut.cpp | 22 | 
3 files changed, 52 insertions, 0 deletions
| 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 @@ -45,6 +45,18 @@ namespace NXml {      }      template <class T> +    TMaybe<T> 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 <class T>      T TNode::Attr(TZtStringBuf name, const T& defvalue) const {          TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str())));          if (!attr) { 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 @@ -328,6 +328,14 @@ namespace NXml {          /**          * get node attribute          * @param name: attribute name +        * returns value if exists +        */ +        template <class T> +        TMaybe<T> TryAttr(TZtStringBuf name) const; + +        /** +        * get node attribute +        * @param name: attribute name          * returns default value if attribute not found          */          template <class T> @@ -600,6 +608,16 @@ namespace NXml {          /**          * get node attribute          * @param name: attribute name +        * returns value if exists +        */ +        template <class T> +        TMaybe<T> TryAttr(TZtStringBuf name) const { +            return ActualNode.TryAttr<T>(name); +        } + +        /** +        * get node attribute +        * @param name: attribute name          * throws exception if attribute not found          */          template <class T> 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 <library/cpp/testing/unittest/registar.h>  #include <util/generic/map.h> +#include <util/generic/yexception.h>  #include "xml-document.h" @@ -35,6 +36,27 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) {          NXml::TConstNode text = root.Node("text");          UNIT_ASSERT_EQUAL(text.Value<TString>(), "Некоторый текст");      } +    Y_UNIT_TEST(GetAttributes) { +        NXml::TDocument xml(R"(<?xml version="1.0"?> +                            <root> +                                <a><b len="15" correct="1">hello world</b></a> +                                <text>Некоторый текст</text> +                            </root> +                            )", +                            NXml::TDocument::String); + +        NXml::TConstNode root = xml.Root(); +        NXml::TConstNode b = root.Node("a/b"); + +        UNIT_ASSERT_EXCEPTION_CONTAINS(b.Attr<int>("unknown attrib"), yexception, "@unknown attrib"); + +        const auto unknownAttr = b.TryAttr<int>("unknown attrib"); +        UNIT_ASSERT(unknownAttr.Empty()); + +        const auto knownAttr = b.TryAttr<int>("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"); | 
