Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode XML path and line number in Element and add XML path setter Error. #548

Merged
merged 32 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2044e47
Changing Error API to work with generic path instead of file path
aaronchongth Apr 4, 2021
2912649
Added Xml path into API of Error
aaronchongth Apr 6, 2021
437f571
Adding XmlPath to errors up till before includes are handled
aaronchongth Apr 6, 2021
734cdcb
Added tests for new API and error messages
aaronchongth Apr 6, 2021
a6a3d0b
Fixed FilePath argument name in Error
aaronchongth Apr 6, 2021
d6fc814
Added XML path information related API to Element
aaronchongth Apr 22, 2021
3812d54
Reverting API changes to readXml, XML paths are now embedded in Element
aaronchongth Apr 22, 2021
0c3d985
Cleaned up useless changes
aaronchongth Apr 22, 2021
c535022
Lint
aaronchongth Apr 22, 2021
e59c9fb
Revert pimpl-izing Element
aaronchongth Apr 27, 2021
805a96a
Clean up missed out reversions
aaronchongth Apr 27, 2021
261c792
Refactored error output stream implementation
aaronchongth Apr 27, 2021
a7dab14
Added example in brief for an XML path in Element API
aaronchongth Apr 30, 2021
3783d75
Added braces for if else cases
aaronchongth Apr 30, 2021
8d0b3b1
Revert construction, used setters
aaronchongth May 1, 2021
49bdfce
Added line number support to Element
aaronchongth May 3, 2021
6ea91a9
Fixed code_check error
aaronchongth May 3, 2021
3fccb8c
Fixed broken XML path in tests
aaronchongth May 11, 2021
1d46a84
Added integration test for file path, line number and xml path, fixed…
aaronchongth May 14, 2021
1481fd8
Linting element_tracing
aaronchongth May 14, 2021
ae3f175
Elaborating on XML path examples with sample SDF document, linking fr…
aaronchongth May 14, 2021
9881cca
Changed post-increment to pre-increment
aaronchongth May 14, 2021
98a2b6c
Changed lincense year of element_tracing tests, removed unused includes
aaronchongth May 14, 2021
688f425
Reducing number of lines in readXml function
aaronchongth May 14, 2021
9490ed9
Removing prefixed / for windows CI
aaronchongth May 15, 2021
ee7f583
Removed helper function for clearer failure debugging
aaronchongth May 15, 2021
bb37c53
Checking against different file path, as it gets loaded as test_model…
aaronchongth May 17, 2021
7aa8519
Removed lines unrelated to test case
aaronchongth May 18, 2021
bd721e9
Adding minimal path sanitization into findFileCb, reverting to use se…
aaronchongth May 18, 2021
feea2f5
Fix linting errors
aaronchongth May 18, 2021
f90ad09
Removed unused vector include
aaronchongth May 18, 2021
1fde2ad
Merge branch 'sdf11' into aaron/xpath-like-trace
aaronchongth May 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions include/sdf/Element.hh
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,35 @@ namespace sdf
/// \return Full path to SDF document.
public: const std::string &FilePath() const;

/// \brief Set the line number of this element within the SDF document.
/// \param[in] _lineNumber Line number of element.
public: void SetLineNumber(int _lineNumber);

/// \brief Get the line number of this element within the SDF document.
/// \return Line number of this element if it has been set, nullopt
/// otherwise.
public: std::optional<int> LineNumber() const;

/// \brief Set the XML path of this element.
/// \param[in] _path Full XML path (i.e., XPath) to the SDF element.
/// E.g., a SDF document:
/// <sdf>
/// <world name="default">
/// <model name="robot1">
/// <link name="link">
/// ...
/// </link>
/// </model>
/// </world>
/// </sdf>
/// The full XML path to the SDF link element would be:
/// /sdf/world[@name="default"]/model[@name="robot1"]/link[@name="link"])
public: void SetXmlPath(const std::string &_path);
aaronchongth marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Get the XML path of this element.
/// \return Full XML path to the SDF element.
public: const std::string &XmlPath() const;

/// \brief Set the spec version that this was originally parsed from.
/// \param[in] _version Spec version string.
public: void SetOriginalVersion(const std::string &_version);
Expand Down Expand Up @@ -531,6 +560,12 @@ namespace sdf
/// \brief Path to file where this element came from
public: std::string path;

/// \brief Line number in file where this element came from
public: std::optional<int> lineNumber;

/// \brief XML path of this element.
public: std::string xmlPath;

/// \brief Spec version that this was originally parsed from.
public: std::string originalVersion;
};
Expand Down
54 changes: 30 additions & 24 deletions include/sdf/Error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,25 @@ namespace sdf
public: std::string Message() const;

/// \brief Get the file path associated with this error.
/// \return Returns the path of the file that this error is related to.
/// \return Returns the path of the file that this error is related to,
/// nullopt otherwise.
public: std::optional<std::string> FilePath() const;

/// \brief Get the line number associated with this error.
/// \return Returns the line number. nullopt otherwise.
public: std::optional<int> LineNumber() const;

/// \brief Get the XPath-like trace that is associated with this error.
/// \return Returns the XPath-like trace that this error is related to,
/// nullopt otherwise.
public: std::optional<std::string> XmlPath() const;

/// \brief Sets the XML path that is associated with this error.
/// \param[in] _xmlPath The XML path that is related to this error. (e.g.
/// /sdf/world[@name="default"]/model[@name="robot1"]/link[@name="link"])
aaronchongth marked this conversation as resolved.
Show resolved Hide resolved
/// \sa Element::SetXmlPath
public: void SetXmlPath(const std::string &_xmlPath);

/// \brief Safe bool conversion.
/// \return True if this Error's Code() != NONE. In otherwords, this is
/// true when there is an error.
Expand All @@ -213,30 +224,25 @@ namespace sdf
public: friend std::ostream &operator<<(std::ostream &_out,
const sdf::Error &_err)
{
if (!_err.FilePath().has_value())
{
_out << "Error Code "
<< static_cast<std::underlying_type<sdf::ErrorCode>::type>(
_err.Code())
<< " Msg: " << _err.Message();
}
else if (!_err.LineNumber().has_value())
{
_out << "Error Code "
<< static_cast<std::underlying_type<sdf::ErrorCode>::type>(
_err.Code())
<< ": [" << _err.FilePath().value() << "]: "
<< " Msg: " << _err.Message();
}
else
{
_out << "Error Code "
std::string pathInfo = "";

if (_err.XmlPath().has_value())
pathInfo += _err.XmlPath().value();

if (_err.FilePath().has_value())
pathInfo += ":" + _err.FilePath().value();

if (_err.LineNumber().has_value())
pathInfo += ":L" + std::to_string(_err.LineNumber().value());

if (!pathInfo.empty())
pathInfo = "[" + pathInfo + "]: ";

_out << "Error Code "
<< static_cast<std::underlying_type<sdf::ErrorCode>::type>(
_err.Code())
<< ": [" << _err.FilePath().value() << ":L"
<< std::to_string(_err.LineNumber().value()) << "]: "
<< " Msg: " << _err.Message();
}
_err.Code()) << ": "
<< pathInfo
<< "Msg: " << _err.Message();
return _out;
}

Expand Down
30 changes: 30 additions & 0 deletions src/Element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ ElementPtr Element::Clone() const
clone->dataPtr->includeFilename = this->dataPtr->includeFilename;
clone->dataPtr->referenceSDF = this->dataPtr->referenceSDF;
clone->dataPtr->path = this->dataPtr->path;
clone->dataPtr->lineNumber = this->dataPtr->lineNumber;
clone->dataPtr->xmlPath = this->dataPtr->xmlPath;
clone->dataPtr->originalVersion = this->dataPtr->originalVersion;

Param_V::const_iterator aiter;
Expand Down Expand Up @@ -214,6 +216,8 @@ void Element::Copy(const ElementPtr _elem)
this->dataPtr->referenceSDF = _elem->ReferenceSDF();
this->dataPtr->originalVersion = _elem->OriginalVersion();
this->dataPtr->path = _elem->FilePath();
this->dataPtr->lineNumber = _elem->LineNumber();
this->dataPtr->xmlPath = _elem->XmlPath();

for (Param_V::iterator iter = _elem->dataPtr->attributes.begin();
iter != _elem->dataPtr->attributes.end(); ++iter)
Expand Down Expand Up @@ -867,6 +871,8 @@ void Element::Clear()
this->ClearElements();
this->dataPtr->originalVersion.clear();
this->dataPtr->path.clear();
this->dataPtr->lineNumber = std::nullopt;
this->dataPtr->xmlPath.clear();
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -974,6 +980,30 @@ const std::string &Element::FilePath() const
return this->dataPtr->path;
}

/////////////////////////////////////////////////
void Element::SetLineNumber(int _lineNumber)
{
this->dataPtr->lineNumber = _lineNumber;
}

/////////////////////////////////////////////////
std::optional<int> Element::LineNumber() const
{
return this->dataPtr->lineNumber;
}

/////////////////////////////////////////////////
void Element::SetXmlPath(const std::string &_path)
{
this->dataPtr->xmlPath = _path;
}

/////////////////////////////////////////////////
const std::string &Element::XmlPath() const
{
return this->dataPtr->xmlPath;
}

/////////////////////////////////////////////////
void Element::SetOriginalVersion(const std::string &_version)
{
Expand Down
26 changes: 26 additions & 0 deletions src/Element_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ TEST(Element, Child)
sdf::Element child;
sdf::ElementPtr parent = std::make_shared<sdf::Element>();
parent->SetFilePath("/parent/path/model.sdf");
parent->SetLineNumber(12);
parent->SetXmlPath("/sdf/world[@name=\"default\"]");
jennuine marked this conversation as resolved.
Show resolved Hide resolved
parent->SetOriginalVersion("1.5");

ASSERT_EQ(child.GetParent(), nullptr);
Expand Down Expand Up @@ -325,6 +327,8 @@ TEST(Element, Clone)
parent->AddValue("string", "foo", false, "foo description");

parent->SetFilePath("/path/to/file.sdf");
parent->SetLineNumber(12);
parent->SetXmlPath("/sdf/world[@name=\"default\"]");
parent->SetOriginalVersion("1.5");

auto includeElemToStore = std::make_shared<sdf::Element>();
Expand All @@ -334,6 +338,9 @@ TEST(Element, Clone)
sdf::ElementPtr newelem = parent->Clone();

EXPECT_EQ("/path/to/file.sdf", newelem->FilePath());
ASSERT_TRUE(newelem->LineNumber().has_value());
EXPECT_EQ(12, newelem->LineNumber().value());
EXPECT_EQ("/sdf/world[@name=\"default\"]", newelem->XmlPath());
EXPECT_EQ("1.5", newelem->OriginalVersion());
ASSERT_NE(newelem->GetFirstElement(), nullptr);
ASSERT_EQ(newelem->GetElementDescriptionCount(), 1UL);
Expand All @@ -349,11 +356,16 @@ TEST(Element, ClearElements)
sdf::ElementPtr child = std::make_shared<sdf::Element>();

parent->SetFilePath("/path/to/file.sdf");
parent->SetLineNumber(12);
parent->SetXmlPath("/sdf/world[@name=\"default\"]");
parent->SetOriginalVersion("1.5");
child->SetParent(parent);
parent->InsertElement(child);

EXPECT_EQ("/path/to/file.sdf", parent->FilePath());
ASSERT_TRUE(parent->LineNumber().has_value());
EXPECT_EQ(12, parent->LineNumber().value());
EXPECT_EQ("/sdf/world[@name=\"default\"]", parent->XmlPath());
EXPECT_EQ("1.5", parent->OriginalVersion());
ASSERT_NE(parent->GetFirstElement(), nullptr);
EXPECT_EQ("/path/to/file.sdf", parent->GetFirstElement()->FilePath());
Expand All @@ -363,6 +375,8 @@ TEST(Element, ClearElements)

ASSERT_EQ(parent->GetFirstElement(), nullptr);
EXPECT_EQ("/path/to/file.sdf", parent->FilePath());
ASSERT_TRUE(parent->LineNumber().has_value());
EXPECT_EQ(12, parent->LineNumber().value());
EXPECT_EQ("1.5", parent->OriginalVersion());
}

Expand All @@ -373,11 +387,16 @@ TEST(Element, Clear)
sdf::ElementPtr child = std::make_shared<sdf::Element>();

parent->SetFilePath("/path/to/file.sdf");
parent->SetLineNumber(12);
parent->SetXmlPath("/sdf/world[@name=\"default\"]");
parent->SetOriginalVersion("1.5");
child->SetParent(parent);
parent->InsertElement(child);

EXPECT_EQ("/path/to/file.sdf", parent->FilePath());
ASSERT_TRUE(parent->LineNumber().has_value());
EXPECT_EQ(12, parent->LineNumber().value());
EXPECT_EQ("/sdf/world[@name=\"default\"]", parent->XmlPath());
EXPECT_EQ("1.5", parent->OriginalVersion());
ASSERT_NE(parent->GetFirstElement(), nullptr);
EXPECT_EQ("/path/to/file.sdf", parent->GetFirstElement()->FilePath());
Expand All @@ -387,6 +406,8 @@ TEST(Element, Clear)

ASSERT_EQ(parent->GetFirstElement(), nullptr);
EXPECT_TRUE(parent->FilePath().empty());
EXPECT_FALSE(parent->LineNumber().has_value());
EXPECT_TRUE(parent->XmlPath().empty());
EXPECT_TRUE(parent->OriginalVersion().empty());
}

Expand Down Expand Up @@ -601,6 +622,8 @@ TEST(Element, Copy)

src->SetName("test");
src->SetFilePath("/path/to/file.sdf");
src->SetLineNumber(12);
src->SetXmlPath("/sdf/world[@name=\"default\"]");
src->SetOriginalVersion("1.5");
src->AddValue("string", "val", false, "val description");
src->AddAttribute("test", "string", "foo", false, "foo description");
Expand All @@ -613,6 +636,9 @@ TEST(Element, Copy)
dest->Copy(src);

EXPECT_EQ("/path/to/file.sdf", dest->FilePath());
ASSERT_TRUE(dest->LineNumber().has_value());
EXPECT_EQ(12, dest->LineNumber().value());
EXPECT_EQ("/sdf/world[@name=\"default\"]", dest->XmlPath());
EXPECT_EQ("1.5", dest->OriginalVersion());

sdf::ParamPtr param = dest->GetValue();
Expand Down
15 changes: 15 additions & 0 deletions src/Error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class sdf::Error::Implementation
/// \brief Description of the error.
public: std::string message = "";

/// \brief Xml path where the error was raised.
public: std::optional<std::string> xmlPath = std::nullopt;

/// \brief File path where the error was raised.
public: std::optional<std::string> filePath = std::nullopt;

Expand Down Expand Up @@ -92,6 +95,18 @@ std::optional<int> Error::LineNumber() const
return this->dataPtr->lineNumber;
}

/////////////////////////////////////////////////
std::optional<std::string> Error::XmlPath() const
{
return this->dataPtr->xmlPath;
}

/////////////////////////////////////////////////
void Error::SetXmlPath(const std::string &_xmlPath)
{
this->dataPtr->xmlPath = _xmlPath;
}

/////////////////////////////////////////////////
// cppcheck-suppress unusedFunction
Error::operator bool() const
Expand Down
Loading