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 a new parseJabRefComment with unit test #12145

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

leaf-soba
Copy link
Contributor

Mandatory checks

  • I own the copyright of the code submitted and I licence it under the MIT license
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (for UI changes)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

@leaf-soba leaf-soba marked this pull request as draft November 4, 2024 08:34
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

fix logic check string contains a valid json
@leaf-soba leaf-soba marked this pull request as ready for review November 4, 2024 08:52
Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

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

Please do differently.

I highlighted the idea using the diff:

image

Check for jabref-meta-0.1.0string at the beginning of the comment. Then remove that line. The remainder of the comment is the complete JSON.

Do NOT read the JSON as BibEntry. It is NOT a BibEntry. It is META data for JabRef. E.g., preferences etc.

Background: JabRef displys BibEntrys in the main table - and settings in dialog. We do not render settings in the main table.

@koppor koppor added the status: changes required Pull requests that are not yet complete label Nov 4, 2024
Comment on lines 408 to 409
} catch (
JsonSyntaxException e) {
Copy link
Member

Choose a reason for hiding this comment

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

Checkstyle is somewhat broken, please reduce to one line.

1. read json as metadata
2. small refactor to remove a not in use `LOGGER` in `MetaData`
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

JUnit tests are failing. In the area "Some checks were not successful", locate "Tests / Unit tests (pull_request)" and click on "Details". This brings you to the test output.

You can then run these tests in IntelliJ to reproduce the failing tests locally. We offer a quick test running howto in the section Final build system checks in our setup guide.

@leaf-soba leaf-soba requested a review from koppor November 6, 2024 00:56
@leaf-soba leaf-soba requested a review from calixtus November 11, 2024 09:05
@@ -339,7 +342,7 @@ private void parseAndAddEntry(String type) {
}
}

private void parseJabRefComment(Map<String, String> meta) {
void parseJabRefComment(Map<String, String> meta) {
Copy link
Member

Choose a reason for hiding this comment

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

This needs @VisibileForTesting?

}
}

private void parseCommentToJson(String comment, Map<String, String> meta) {
Copy link
Member

Choose a reason for hiding this comment

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

No - it should return the JSOn object.

The following call should be rewritten:

            MetaData metaData = metaDataParser.parse(
                    meta,
                    importFormatPreferences.bibEntryPreferences().getKeywordSeparator());

meta should be a record of

  • Map<String, String> v5metaData
  • JsonObject v6metaData

And then be handled accordingliny in org.jabref.logic.importer.fileformat.BibtexParser#metaDataParser.


@AllowedToUseLogic("because it needs access to citation pattern and cleanups")
public class MetaData {

public static final String META_FLAG = "jabref-meta: ";
public static final String META_FLAG_010 = "jabref-meta-0.1.0";
Copy link
Member

Choose a reason for hiding this comment

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

Include the string Version in the constant name:

Suggested change
public static final String META_FLAG_010 = "jabref-meta-0.1.0";
public static final String META_FLAG_VERSION_010 = "jabref-meta-0.1.0";

rewrite metaDataParser.parse
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

fix wrong input
@@ -386,7 +390,23 @@ private void parseJabRefComment(Map<String, String> meta) {
} catch (ParseException ex) {
parserResult.addException(ex);
}
} else if (comment.substring(0, Math.min(comment.length(), MetaData.META_FLAG_VERSION_010.length())).equals(MetaData.META_FLAG_VERSION_010)) {
Copy link
Member

Choose a reason for hiding this comment

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

Java strings support startsWith. I think, this should be used here instead of this complicated thign.

}
}

private JsonObject parseCommentToJson(String comment, Map<String, String> meta) {
Copy link
Member

Choose a reason for hiding this comment

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

Remove the map as parameter. It should not be modified.

The JsonObject should be separate - to enable handling v5.x meta data (Map) and v6.x metadata (JSON)

Copy link
Member

Choose a reason for hiding this comment

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

Moreover, return Optional<JsonObject> (which is more modern Java)

Comment on lines 399 to 400
Pattern pattern = Pattern.compile("\\{.*}", Pattern.DOTALL);
Matcher matcher = pattern.matcher(comment);
Copy link
Member

Choose a reason for hiding this comment

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

I think, just doing comments.substring(MetaData.META_FLAG_VERSION_010) should to the trick. - No need for more checking here.

return parse(new MetaData(), data, keywordSeparator);
}

public MetaData parse(MetaData metaData, JsonObject data, Character keywordSeparator) throws ParseException {
Copy link
Member

Choose a reason for hiding this comment

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

Add JavaDoc - I don't get this.

I think, this should not be in this PR for now - We only focus on the JSON - and then, we can work on the parsing of the JSON.

Copy link
Member

Choose a reason for hiding this comment

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

Please, either test this method or remove it - and put it to the next PR.

Copy link
Member

Choose a reason for hiding this comment

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

This method is should be static, shouldn't it?

1. add javaDoc
2. remove necessary regex use substring
3. refactor code to `startsWith`
@leaf-soba leaf-soba requested a review from koppor November 21, 2024 09:07
return parse(new MetaData(), data, keywordSeparator);
}

public MetaData parse(MetaData metaData, JsonObject data, Character keywordSeparator) throws ParseException {
Copy link
Member

Choose a reason for hiding this comment

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

Please, either test this method or remove it - and put it to the next PR.

return parse(new MetaData(), data, keywordSeparator);
}

public MetaData parse(MetaData metaData, JsonObject data, Character keywordSeparator) throws ParseException {
Copy link
Member

Choose a reason for hiding this comment

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

This method is should be static, shouldn't it?

@@ -386,9 +386,17 @@ private void parseJabRefComment(Map<String, String> meta) {
} catch (ParseException ex) {
parserResult.addException(ex);
}
} else if (comment.startsWith(MetaData.META_FLAG_VERSION_010)) {
parseCommentToJson(comment);
Copy link
Member

Choose a reason for hiding this comment

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

The result is not handled. I think, it needs to be handled. It needs to be passed to the Metadata.parse method somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I made this PR too bigger again, so I decided only to add the unit test for parseCommentToJson and implement of it, I'll do more in next PR.

fix the unittest to keep the PR small
@leaf-soba leaf-soba requested a review from koppor November 28, 2024 07:20
Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

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

I think public MetaData parse(MetaData metaData, JsonObject data, Character keywordSeparator) throws ParseException { has to be removed.

You can start with adding a parse method to org.jabref.logic.importer.util.MetaDataParser acceping a JSONObject (with the current save actions configured).

Comment on lines 2260 to 2261
JsonObject actualJson = parser.parseCommentToJson(entries).orElse(null);
assertEquals(actualJson, getExpectedJson());
Copy link
Member

Choose a reason for hiding this comment

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

No. Use the power of Optionals.

Suggested change
JsonObject actualJson = parser.parseCommentToJson(entries).orElse(null);
assertEquals(actualJson, getExpectedJson());
Optional<JsonObject> actualJson = parser.parseCommentToJson(entries);
assertEquals(actualJson, Optional.of(getExpectedJson()));

String user = entry.getKey().substring(MetaData.FILE_DIRECTORY.length() + 1);
metaData.setUserFileDirectory(user, parseDirectory(entry.getValue()));
} else if (entry.getKey().startsWith(MetaData.FILE_DIRECTORY_LATEX)) {
// The user name starts directly after FILE_DIRECTORY_LATEX + '-'
Copy link
Member

Choose a reason for hiding this comment

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

These hacks should be removed when transitioning to JSON.

@leaf-soba
Copy link
Contributor Author

Sorry I just forget to remove public MetaData parse

remove MetaData.parse
Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

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

I know saw that puzzled me at getExpectedJson() {

We need to to proper engineering there, since this will be in the core of JabRef. And proper engineering takes time...


Copy link
Member

Choose a reason for hiding this comment

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

No additional spaces pleace

Comment on lines +2265 to +2279
JsonObject saveActions = new JsonObject();
saveActions.addProperty("state", true);
JsonArray dateArray = new JsonArray();
dateArray.add("normalize_date");
dateArray.add("action2");
saveActions.add("date", dateArray);
JsonArray pagesArray = new JsonArray();
pagesArray.add("normalize_page_numbers");
saveActions.add("pages", pagesArray);
JsonArray monthArray = new JsonArray();
monthArray.add("normalize_month");
saveActions.add("month", monthArray);
JsonObject expectedJson = new JsonObject();
expectedJson.add("saveActions", saveActions);
return expectedJson;
Copy link
Member

Choose a reason for hiding this comment

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

This reads very ugly.

According to https://stackoverflow.com/a/4696873/873282 it can be easier

Can you try with var?

var expected = new Object() {
...

Another thought: Just simplify the contained object.

you are testing the JSON reading - then put a simple JSON inside. Test one thing in one test :-)

Reason: The searialization of saveActions will probalby changed. Especially state will be called enabled for sure (because state has a different meaning)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: changes required Pull requests that are not yet complete
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants