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

Validate if the statement ID is of the right format #500

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kanikasaini
Copy link
Contributor

Resolves #166

@kanikasaini
Copy link
Contributor Author

Hi,

I have fixed a few tests as of now. Once the validate function is approved, I will commit the fixes for the rest of the cases. Please let me know what you think.

Thank you.

throw new TypeNotPresentException("Query,Property or Lexeme", null);

if(!statementParts[1].matches(statementFormat))
throw new IllegalStateException("Statement part does not have a correct format");
Copy link
Member

Choose a reason for hiding this comment

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

The indentation of this line is inconsistent with the rest. Also, I do not think IllegalStateException is appropriate: this should only be used when you detect that some data structure has reached an inconsistent state. To validate arguments, IllegalArgumentException is the one to use.

Copy link
Collaborator

@Tpt Tpt left a comment

Choose a reason for hiding this comment

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

Thank you for the PR. There are some details to improve.

*/
public static boolean validateStatementID(String statementID)
{
if(statementID.equals(""))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please always use curly braces with if to avoid ambiguities.

* @return true if the statement ID is of the valid format
*/
public static boolean validateStatementID(String statementID)
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please put curly brace in the same line like all the code base.

* @param statementID
* @return true if the statement ID is of the valid format
*/
public static boolean validateStatementID(String statementID)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you raise an exception if the ID is not valid, then returning a boolean is not useful. If the function returns, then you aready know the ID is valid.

return true;

String separator = "\\$";
String statementFormat ="^\\{?[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}\\}?$";
Copy link
Collaborator

Choose a reason for hiding this comment

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

We will validate millions of IDs. I belive it would be better to precompile the pattern using Pattern.compile and store it in a final static class field.

if(statementParts.length != 2)
throw new IllegalArgumentException("Statement ID does not have the correct number of parts");

if(statementParts[0].charAt(0) != 'Q' && statementParts[0].charAt(0) != 'P' && statementParts[0].charAt(0) != 'L' && statementParts[0].charAt(0) != 'M')
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are actually more types (forms, senses, media-info...). You could use EntityIdValueImpl.guessEntityTypeFromId that will does the same thing and supports all types.

@wetneb
Copy link
Member

wetneb commented Feb 3, 2021

@kanikasaini do you have the intention to come back to this PR?

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.

Validate statement ids
3 participants