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

Add EnforcementPolicy in ParserConfig to enable configurable parsing strictness #481

Merged
merged 7 commits into from
Feb 5, 2021

Conversation

brawner
Copy link
Collaborator

@brawner brawner commented Jan 30, 2021

This PR depends on #439, but the PR branch is in a different fork so I can't target it directly.

The added commit is 166b995

This PR adds a configurable WarningsPolicy in ParserConfig to allow users to customize the strictness of the parser. In some cases, a best-effort parse is desired and warnings either streamed to sdfwarn or sdfdbg. But in other cases, like unit tests for example, it makes sense to be pedantic about warnings.

Fixes #473, #327

This is a first approach after discussion over at #473 and I'm totally open to other approaches. Likewise, based on the use cases described in #473 and Bitbucket PR, it might also make sense to add a second configuration option specifically for unrecognized elements.

@brawner
Copy link
Collaborator Author

brawner commented Jan 30, 2021

Force pushed 166b995 with a small amount of cleanup.

src/parser.cc Outdated
<< "], child of element[" << _xml->Value()
<< "], not defined in SDF. Copying[" << elemXml->Value() << "] "
<< "as children of [" << _xml->Value() << "].\n";

addRecoverableWarning(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, for now this changes the default behavior for this specific check back to warn. However, this function takes in a policy enum value so it could be overridden for the default case with something like:

sdf::WarningsPolicy policy = _config.WarningsPolicy();
if (policy != sdf::WarningsPolicy::PEDANTIC)
  policy = sdf::WarningsPolicy::IGNORED;

It might also be desired to separate WarningsPolicy and add something specific like _config.IgnoreUnrecognizedElements()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we've gone back and forth on this behavior in the past, I would vote for having _config.IgnoreUnrecognizedElements() or something similar.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, the more I think about it that makes sense. I switched this back to warn and I'll open a separate PR for easy review of the new additions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, after working on it I decided to update this PR. I think there is enough overlap between the two policies that they should have a common enum, which necessitated changes here.

src/parser.cc Outdated
sdfwarn << "Converting a deprecated SDF source[" << _source << "].\n";
std::stringstream ss;
ss << "Converting a deprecated SDF source[" << _source << "].\n";
addRecoverableWarning(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to emit an error for reading an older SDFormat file even when the policy is PEDANTIC. The version of readDoc that takes a SDFPtr uses sdfdbg for this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to sdfdbg

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, with the new commit this should be easy to add a policy for this in the future should it be desired.

@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<sdf version="1.6">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change the version to 1.8 and use an xmlns attribute to make the xml valid?

Suggested change
<sdf version="1.6">
<sdf version="1.8" xmlns:third_party_software="custom_ns_uri">

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<sdf version="1.6">
<model name="joint_incorrect_tags" some_attribute="staheousth">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what's staheousth?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some arbitrary text content. Changed to "some value"

src/Utils.hh Outdated
@@ -21,8 +21,10 @@
#include <string>
#include <optional>
#include <vector>
#include "sdf/Console.hh"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is this needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was for sdfwarn/sdfdbg, but I guess not. Removed

src/parser.cc Outdated
<< "], child of element[" << _xml->Value()
<< "], not defined in SDF. Copying[" << elemXml->Value() << "] "
<< "as children of [" << _xml->Value() << "].\n";

addRecoverableWarning(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we've gone back and forth on this behavior in the past, I would vote for having _config.IgnoreUnrecognizedElements() or something similar.

@brawner
Copy link
Collaborator Author

brawner commented Feb 3, 2021

Err, had to remove the added tests since they will be carried over to the next PR

Copy link
Collaborator Author

@brawner brawner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After working on a separate commit to handle unrecognized elements, I realized that there was a lot in common with the warnings policy that they should share the same enum, which necessitated some reworking of the naming of different parts.

That said, I think it adds a good framework for adding new recoverable conditions like these in the future.

src/parser.cc Outdated
<< "], child of element[" << _xml->Value()
<< "], not defined in SDF. Copying[" << elemXml->Value() << "] "
<< "as children of [" << _xml->Value() << "].\n";

addRecoverableWarning(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, after working on it I decided to update this PR. I think there is enough overlap between the two policies that they should have a common enum, which necessitated changes here.

src/parser.cc Outdated
sdfwarn << "Converting a deprecated SDF source[" << _source << "].\n";
std::stringstream ss;
ss << "Converting a deprecated SDF source[" << _source << "].\n";
addRecoverableWarning(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, with the new commit this should be easy to add a policy for this in the future should it be desired.

Copy link
Collaborator

@azeey azeey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of minor comments. Looks great!

src/Utils.cc Outdated Show resolved Hide resolved
src/Utils.hh Outdated Show resolved Hide resolved
config.SetUnrecognizedElementsPolicy(sdf::EnforcementPolicy::ERR);
sdf::Root root;
const auto errors = root.Load(testFile, config);
EXPECT_FALSE(errors.empty());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an expectation on at least one of the output messages? Just so it doesn't pass due to other non-related error messages in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<sdf version="1.8" xmlns:third_party_software="custom_ns_uri">
<model name="joint_incorrect_tags" some_attribute="some value">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: joint_incorrect_tags makes it sound this is is not a valid file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name to namespaced_tags

@azeey azeey added this to the SDFormat 1.8 / libsdformat11 milestone Feb 4, 2021
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Copy link
Member

@scpeters scpeters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awesome! it shouldn't be in this PR, but I'd love to add a flag to ign sdf commands for setting the parsing level

src/Utils.hh Outdated Show resolved Hide resolved
include/sdf/ParserConfig.hh Outdated Show resolved Hide resolved
Signed-off-by: Stephen Brawner <brawner@gmail.com>
@scpeters
Copy link
Member

scpeters commented Feb 5, 2021

this is awesome! it shouldn't be in this PR, but I'd love to add a flag to ign sdf commands for setting the parsing level

#485

@brawner brawner changed the title Add WarningsPolicy in ParserConfig to enable configurable parsing strictness Add EnforcementPolicy in ParserConfig to enable configurable parsing strictness Feb 5, 2021
@brawner brawner merged commit 53491da into master Feb 5, 2021
@brawner brawner deleted the brawner/pedantic-flag branch February 5, 2021 23:53
@brawner
Copy link
Collaborator Author

brawner commented Feb 5, 2021

Thanks for the reviews you two!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏢 edifice Ignition Edifice
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Joint limits silently ignored if improperly nested
3 participants