Skip to content

Commit

Permalink
Получение XML аттрибута без исключений
Browse files Browse the repository at this point in the history
add no exception methods
commit_hash:245a52ca795a16ad57a9ac642b0cd00ca0122a32
  • Loading branch information
artyasen committed Feb 3, 2025
1 parent cafd674 commit a19aaf0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions library/cpp/xml/document/node-attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ namespace NXml {
return t;
}

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())));
Expand Down
18 changes: 18 additions & 0 deletions library/cpp/xml/document/xml-document-decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ namespace NXml {
template <class T>
T Attr(TZtStringBuf name) const;

/**
* 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
Expand Down Expand Up @@ -597,6 +605,16 @@ namespace NXml {
return ActualNode.NextSibling();
}

/**
* 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
Expand Down
22 changes: 22 additions & 0 deletions library/cpp/xml/document/xml-document_ut.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit a19aaf0

Please sign in to comment.