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

Feat/#2764/add hash in xml #2781

Closed

Conversation

Yanich96
Copy link
Contributor

@Yanich96 Yanich96 commented Jan 15, 2024

Closes #2764

What was done:

  1. Class StHash was created to add the program hash code to the XML file header.
  2. Class StHashTest was created to check adding the program hash code to the XML file header.
  3. The schema XML was changed to add the program hash code to the XML file header.
  4. Added tests checking hash code presence in xml.

PR-Codex overview

This PR focuses on adding a new 'hash' attribute to the 'program' node in XML files.

Detailed summary

  • Added 'hash' attribute to the 'program' node in XMIR.xsd and EoSyntaxTest.java
  • Created StHash class to generate hash code using MD5 algorithm
  • Updated PhiSyntax.java and EoSyntax.java to use StHash for generating hash code
  • Added StHashTest.java to test the functionality of StHash class

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

@Yanich96
Copy link
Contributor Author

@volodya-lombrozo check please

Copy link
Member

@volodya-lombrozo volodya-lombrozo left a comment

Choose a reason for hiding this comment

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

@Yanich96 Could you, please:

  1. Link the current PR description with the issue you are trying to solve (https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
  2. Close the previous similar PR (fix(#2764): caching logic changed, added StHash, StHashTest, fix tests #2771) if you aren't going to continue with it.

*
* @since 0.35.0
*/
public final class StHash extends StEnvelope {
Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 I have some doubts about the StHash implementation and if we need it. I believe you can set the hash function directly in XeEoListener during the parsing. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Hash code computes from node 'objects'. There isn't this node in parsing stage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Xembly directives will have to be run 2 times: 1 - to calculate the hash, 2 - to completely parse, and during the parsing process there may be XSD schema errors and parsing errors, as a result of which the hash calculation ceases to make sense.

Copy link
Member

Choose a reason for hiding this comment

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

@volodya-lombrozo Hash code computes from node 'objects'. There isn't this node in parsing stage.

@Yanich96 Would exitProgram help us here? It looks like the final parsing stage.

Copy link
Member

Choose a reason for hiding this comment

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

@volodya-lombrozo Xembly directives will have to be run 2 times: 1 - to calculate the hash, 2 - to completely parse, and during the parsing process there may be XSD schema errors and parsing errors, as a result of which the hash calculation ceases to make sense.

We anyway will need to read XML twice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo I will write todo for it.

@@ -113,6 +113,7 @@ SOFTWARE.
<xs:attribute name="version" type="xs:string" use="required"/>
<xs:attribute name="revision" type="xs:string" use="required"/>
<xs:attribute name="dob" type="xs:dateTime" use="required"/>
<xs:attribute name="hash" type="xs:string" use="required"/>
Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 Returning to the discussion. Maybe we don't need the hash attribute? Especially if we will save hash as a file attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo
I don't understand how to save hash as a file attribute.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo In OptCahed.java the method 'contains(final XML xml)' get XML, but not file. We should get hash code of program, because we can't have file attributes. This reason is saving hash code in XML.

Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 You can always calculate the hash on the fly, like hash(xml); and compare it with the cache file attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@Yanich96 Yanich96 Jan 15, 2024

Choose a reason for hiding this comment

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

@volodya-lombrozo I have done todo. This line will be deleted after it.

@@ -47,6 +47,7 @@
*
* @since 0.1
*/
@SuppressWarnings("PMD.TooManyMethods")
Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 I believe, you don't need that line. Could you please, remove the line and run the following command:

mvn qulice:check -Pqulice

I did it myself and the linter said nothing about methods number.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Test failed : PMD: eo-parser/src/test/java/org/eolang/parser/EoSyntaxTest.java[50-250]: This class has too many methods, consider refactoring it. (TooManyMethods)
I used this command.

Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 Ok. Let's leave it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo This line will be deleted in the next pr, when I will transfer hash code in file attributes.

* @return String hash of this XML.
* @throws NoSuchAlgorithmException If fails.
*/
public String compute() throws NoSuchAlgorithmException {
Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 This method contains lots of redundant variables. https://www.yegor256.com/2015/09/01/redundant-variables-are-evil.html

Copy link
Member

@volodya-lombrozo volodya-lombrozo Jan 16, 2024

Choose a reason for hiding this comment

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

@Yanich96 The method name looks a bit misleading. The method returns a value, but you use verb here. Maybe it's better to use a noun?

@Yanich96
Copy link
Contributor Author

@volodya-lombrozo check please

@Yanich96
Copy link
Contributor Author

@volodya-lombrozo I have fixed redundant variables in method StHash.compute(). Other comments will be resolved in added issues.

* @return String hash of this XML.
* @throws NoSuchAlgorithmException If fails.
*/
public String compute() throws NoSuchAlgorithmException {
Copy link
Member

@volodya-lombrozo volodya-lombrozo Jan 16, 2024

Choose a reason for hiding this comment

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

@Yanich96 The method name looks a bit misleading. The method returns a value, but you use verb here. Maybe it's better to use a noun?

* @throws NoSuchAlgorithmException If fails.
*/
public String compute() throws NoSuchAlgorithmException {
final BigInteger number = new BigInteger(
Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 number is a redundant variable.

this.xml.nodes(
"/program/objects"
)
.toString().getBytes()
Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 Here you apply toString method to a List instance, which is "ok" in this case (since the list contains strings.) However, why don't you use just xml.toString ? It looks simpler and doesn't require additional computations like applying paths

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo I use '/program/objects' for creating the hash code. If 'xml.toString' is used, hash code will be created from all xml.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@volodya-lombrozo Using the entire xml will cause the hash code to be inconsistent.

Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 Why?

.toString().getBytes()
)
);
final StringBuilder hash = new StringBuilder(number.toString(16));
Copy link
Member

Choose a reason for hiding this comment

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

@Yanich96 Could you just use Hex.encodeHexString() from Apache?

https://stackoverflow.com/a/9655275/10423604

Seems, we have this library in the classpath.

@Yanich96 Yanich96 closed this Jan 16, 2024
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.

OptCached.java:44-49: Use checksum, not time. The...
2 participants