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

CharReader: Add StructuredError #1409

Merged
merged 3 commits into from
Sep 10, 2024

Conversation

martinduffy1
Copy link
Contributor

The move from Json::Reader to Json::CharReader omitted the ability to get structured error messages from the parser. Clients which want to extract the line and column information have to parse the returned string. Add a new getStructuredErrors() method which exposes the error as structured data instead of a string.

Based on the work from @KyleFromKitware and comments from @BillyDonahue on #1281.

@ahedberg
Copy link

Out of curiosity, what's the status of this PR? We'd find this addition useful as well.

@BillyDonahue
Copy link
Contributor

Out of curiosity, what's the status of this PR? We'd find this addition useful as well.

I have some pending comments that I never hit "Send" on. Sorry about that.

src/test_lib_json/main.cpp Outdated Show resolved Hide resolved
src/test_lib_json/main.cpp Outdated Show resolved Hide resolved
src/test_lib_json/main.cpp Outdated Show resolved Hide resolved
src/test_lib_json/main.cpp Outdated Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
@martinduffy1
Copy link
Contributor Author

@BillyDonahue I responded to all the comments from your last review. Let me know if you have any questions.

include/json/reader.h Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
@martinduffy1
Copy link
Contributor Author

Thank you for the quick review @BillyDonahue
I added some comments about why I am handling impl_ the way I am, and fixed everything else according to your suggestions.

include/json/reader.h Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
include/json/reader.h Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
include/json/reader.h Outdated Show resolved Hide resolved
@martinduffy1
Copy link
Contributor Author

@BillyDonahue thanks for your review, I've addressed these comments.

@BillyDonahue
Copy link
Contributor

@BillyDonahue thanks for your review, I've addressed these comments.

I still see some comments unaddressed.

@martinduffy1
Copy link
Contributor Author

martinduffy1 commented Oct 12, 2022 via email

@BillyDonahue
Copy link
Contributor

BillyDonahue commented Oct 12, 2022

Which comments are unaddressed? Is the problem that I didn't click resolve on the comments? I did notice another indentation error though which I will commit now.

Please look through some of those comments. I think you'll see what I mean.

@martinduffy1
Copy link
Contributor Author

Which comments are unaddressed? Is the problem that I didn't click resolve on the comments? I did notice another indentation error though which I will commit now.

Please look through some of those comments. I think you'll see what I mean.

The comment you responded to was addressed elsewhere in the code. I added some comments to the issues not showing as Outdated to explain how these were addressed. In the future, I'll make sure to include a description of how the comments were addressed to make it more straightforward to review.

@martinduffy1
Copy link
Contributor Author

Which comments are unaddressed? Is the problem that I didn't click resolve on the comments? I did notice another indentation error though which I will commit now.

Please look through some of those comments. I think you'll see what I mean.

The comment you responded to was addressed elsewhere in the code. I added some comments to the issues not showing as Outdated to explain how these were addressed. In the future, I'll make sure to include a description of how the comments were addressed to make it more straightforward to review.

@BillyDonahue can you take a look at this comment and let me know if there are still changes you are waiting on from me, or if you just haven't gotten a chance to review them yet?

@BillyDonahue
Copy link
Contributor

Which comments are unaddressed? Is the problem that I didn't click resolve on the comments? I did notice another indentation error though which I will commit now.

Please look through some of those comments. I think you'll see what I mean.

The comment you responded to was addressed elsewhere in the code. I added some comments to the issues not showing as Outdated to explain how these were addressed. In the future, I'll make sure to include a description of how the comments were addressed to make it more straightforward to review.

@BillyDonahue can you take a look at this comment and let me know if there are still changes you are waiting on from me, or if you just haven't gotten a chance to review them yet?

Sorry it's very hard to follow Github PR reviews, so if something is addressed elsewhere I may not make the connection. I will take another deeper look.

src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
src/lib_json/json_reader.cpp Outdated Show resolved Hide resolved
@@ -900,7 +900,7 @@ class OurReader {
bool parse(const char* beginDoc, const char* endDoc, Value& root,
bool collectComments = true);
String getFormattedErrorMessages() const;
std::vector<StructuredError> getStructuredErrors() const;
std::vector<OurReader::StructuredError> getStructuredErrors() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

revert. we're in OurReader's class scope already

Comment on lines 1896 to 1893
std::vector<CharReader::StructuredError> getStructuredErrors() const override {
std::vector<OurReader::StructuredError> errors = reader_.getStructuredErrors();
std::vector<CharReader::StructuredError> out_errors;
std::transform(errors.begin(), errors.end(), std::back_inserter(out_errors),
[](OurReader::StructuredError x) {
CharReader::StructuredError y;
y.offset_start = x.offset_start;
y.offset_limit = x.offset_limit;
y.message = x.message;
return y;
});
return out_errors;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be written more economically, but I think the translation can be made unnecessary.

Suggested change
std::vector<CharReader::StructuredError> getStructuredErrors() const override {
std::vector<OurReader::StructuredError> errors = reader_.getStructuredErrors();
std::vector<CharReader::StructuredError> out_errors;
std::transform(errors.begin(), errors.end(), std::back_inserter(out_errors),
[](OurReader::StructuredError x) {
CharReader::StructuredError y;
y.offset_start = x.offset_start;
y.offset_limit = x.offset_limit;
y.message = x.message;
return y;
});
return out_errors;
}
std::vector<CharReader::StructuredError> getStructuredErrors() const override {
std::vector<CharReader::StructuredError> out;
auto in = reader_.getStructuredErrors();
out.reserve(in.size());
for (auto&& x : in)
out.push_back({x.offset_start, x.offset_limit, std::move(x.message)});
return out;
}

But I don't see the need for the extra translation step.

There doesn't need to be a separate type for OurReader::StructuredError now that there's a CharReader::StructuredError. This OurReader is a private detail of this .cpp file, and its only purpose is to serve as an implementation detail of OurCharReader. It can be made to produce CharReader::StructuredError structs directly.

A great thing about OurReader is that it's 100% an implementation detail of the .cpp file and we can change it without worrying about API breaks. I didn't realize this before, which wasted your time perhaps. Apologies.
I feel like the Our stuff makes the library pretty confusing to work on and we should just make clean break from the deprecated Reader by putting it in another file where it can't bother us anymore. :)

virtual std::vector<StructuredError> getStructuredErrors() const = 0;
};

CharReader(Impl* impl) : _impl(impl) { }
Copy link
Contributor

Choose a reason for hiding this comment

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

  • My suggestion was to accept a std::unique_ptr<Impl> here.

  • Unary constructors should be explicit.

Suggested change
CharReader(Impl* impl) : _impl(impl) { }
explicit CharReader(std::unique_ptr<Impl> impl) : _impl(std::move(impl)) { }

bool ok = reader_.parse(beginDoc, endDoc, *root, collectComments_);
if (errs) {
*errs = reader_.getFormattedErrorMessages();
: CharReader(new OurImpl(collectComments, features)) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
: CharReader(new OurImpl(collectComments, features)) {}
: CharReader(std::make_unique<OurImpl>(collectComments, features)) {}

@martinduffy1
Copy link
Contributor Author

@BillyDonahue let me know if these changes address your comment

@martinduffy1
Copy link
Contributor Author

@BillyDonahue it's been 8 weeks since your last update on this. Is there anything you're waiting on from me?

@BillyDonahue
Copy link
Contributor

BillyDonahue commented Jan 18, 2023

@BillyDonahue it's been 8 weeks since your last update on this. Is there anything you're waiting on from me?

Sorry. I made a few suggestions and didn't hear back. If you have resolved them can you comment and resolve?
Update: My mistake. You responded on Dec 16 and I missed it. I'll take a look.

@martinduffy1
Copy link
Contributor Author

@BillyDonahue I see this marked as approved. Is there a release timeline that I can expect for these changes to get merged?

@BillyDonahue
Copy link
Contributor

@BillyDonahue I see this marked as approved. Is there a release timeline that I can expect for these changes to get merged?

Sorry I thought the approval was sufficient for you to merge it rather than me.
I see that we have to rerun Travis.

@BillyDonahue
Copy link
Contributor

Hm. I don't actually know how to restart the stuck Travis CI job.
I poked around and didn't find anything.
If you push an update maybe it will kickstart itself.

Add getStructuredError to CharReader
@martinduffy1
Copy link
Contributor Author

@BillyDonahue I squashed the commits and pushed to try to retrigger the CI job. I don't think I can merge this as I don't have write access to the repository. I'm also wondering if there can be a minor version increase when this and #1412 are merged so that we can detect versions with these updates.

@baylesj baylesj merged commit c04c0c2 into open-source-parsers:master Sep 10, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants