-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[Extensions] Adds a parameter to Setting Validator to toggle regex matching #6823
Conversation
Signed-off-by: Kuanysh <kuanysh4646@gmail.com>
Gradle Check (Jenkins) Run Completed with:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing, this will be a good functionality!
A few changes needed as noted below.
Also:
- You have named the PR matching the issue (bug) you're working on, but since that's in a different repo, the maintainers here may not understand the subject. And this isn't actually fixing the bug it's adding a feature to make the bug fix easier on the other Repo! So I'd suggest changing your title to describe what this change actually does, something like "Adds a parameter to Setting Validator to toggle regex matching".
- There are test cases for this function in a test class somewhere (probably SettingTests). Find them and update the tests to work with the new parameters.
- Make sure you check all the boxes in the first comment (one of which is adding tests and that they pass!)
throw new IllegalArgumentException("Setting [" + value + "] does not match regex [" + pattern.pattern() + "]"); | ||
if (isMatching){ | ||
if (!pattern.matcher(value).matches()) { | ||
throw new IllegalArgumentException("Setting must not contain [" + pattern.pattern() + "]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have the same error for both cases here. This one should keep the old "does not match" pattern.
} | ||
}else { | ||
if (pattern.matcher(value).matches()) { | ||
throw new IllegalArgumentException("Setting must not contain [" + pattern.pattern() + "]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't be specific about "contain". Use a variant of the previous message, probably "Setting [value] must match regex [pattern]"
/** | ||
* @param regex A regular expression containing the only valid input for this setting. | ||
*/ | ||
public RegexValidator(String regex) { | ||
this.pattern = Pattern.compile(regex); | ||
} | ||
|
||
/** | ||
* @param regex A regular expression containing the only valid input for this setting. | ||
* @param isMatching this is a boolean switching between "fail, if it matches" or "fail, if it doesn't match". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This javadoc doesn't tell me which is which. It should be clear what true means and what false means.
Something like "If true, the setting must match the given regex. If false, the setting must not match the given regex."
/** | ||
* @param regex A regular expression containing the only valid input for this setting. | ||
*/ | ||
public RegexValidator(String regex) { | ||
this.pattern = Pattern.compile(regex); | ||
} | ||
|
||
/** | ||
* @param regex A regular expression containing the only valid input for this setting. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence is no longer true all the time with the boolean. Consider adding a summary statement like "Constructs a validator based on a regular expression." and then this param can just be "the regular expression".
@@ -1256,13 +1256,24 @@ private static boolean isFiltered(Property[] properties) { | |||
public static class RegexValidator implements Writeable, Validator<String> { | |||
private Pattern pattern; | |||
|
|||
private boolean isMatching = true; | |||
|
|||
/** | |||
* @param regex A regular expression containing the only valid input for this setting. | |||
*/ | |||
public RegexValidator(String regex) { | |||
this.pattern = Pattern.compile(regex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Style) This (still) works, but the pattern in most of this class is to overload constructors to call other constructors. So you could use this(regex, true)
here, and then not pre-initialize the boolean.
Also probably want to make a similar summary statement as the one you are adding in the constructor below.
Signed-off-by: Kuanysh <kuanysh4646@gmail.com>
Gradle Check (Jenkins) Run Completed with:
|
|
||
/** | ||
* @param regex constructs a validator based on a regular expression. | ||
* @param isMatching If true, the setting must match the given regex. If false, the setting must not match the given regex.. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the ..
at the end.
@@ -1273,14 +1285,21 @@ Pattern getPattern() { | |||
|
|||
@Override | |||
public void validate(String value) { | |||
if (!pattern.matcher(value).matches()) { | |||
throw new IllegalArgumentException("Setting [" + value + "] does not match regex [" + pattern.pattern() + "]"); | |||
if (isMatching){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic here is good, but all the nesting can be simplified.
if (isMatching && !pattern.matcher(value).matches()) {
first throw
} else if (pattern.matcher(value).matches()) {
second throw
}
@@ -329,12 +329,18 @@ public void testRegexValidator() throws Exception { | |||
String expectedRegex = "\\d+"; | |||
Pattern expectedPattern = Pattern.compile(expectedRegex); | |||
RegexValidator regexValidator = new RegexValidator(expectedRegex); | |||
RegexValidator regexValidatorMatcher = new RegexValidator(expectedRegex,false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great for testing the false
case, but can we add a test for the true case?
Also, I suspect spotless is going to complain when CI is run here. Make sure you always run ./gradlew spotlessApply
before pushing your commits here.
Signed-off-by: Kuanysh <kuanysh4646@gmail.com>
Gradle Check (Jenkins) Run Completed with:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few more tweaks!
@@ -329,12 +329,24 @@ public void testRegexValidator() throws Exception { | |||
String expectedRegex = "\\d+"; | |||
Pattern expectedPattern = Pattern.compile(expectedRegex); | |||
RegexValidator regexValidator = new RegexValidator(expectedRegex); | |||
RegexValidator regexValidatorMatcherFalse = new RegexValidator(expectedRegex, false); | |||
RegexValidator regexValidatorMatcherTrue = new RegexValidator(expectedRegex, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This duplicates the regexValidator
on line 331, so you don't need it.
assertNotNull(regexValidatorMatcherFalse.getPattern()); | ||
assertEquals(expectedPattern.pattern(), regexValidatorMatcherFalse.getPattern().pattern()); | ||
|
||
// Test that checks the pattern and isMatching with the set value true parameters are working correctly during initialization |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IF you delete line 333 you don't need these tests.
assertNotNull(regexValidatorMatcherTrue); | ||
assertNotNull(regexValidatorMatcherTrue); | ||
assertEquals(expectedPattern.pattern(), regexValidatorMatcherTrue.getPattern().pattern()); | ||
|
||
// Test that validate() throws an exception for invalid input | ||
final RegexValidator finalValidator = new RegexValidator(expectedRegex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to make a copy of these tests (line 351-358) for regexValidatorMatcherFalse
. You should get the opposite behavior (exception when it matches and no exception when it doesn't match).
Signed-off-by: Kuanysh <kuanysh4646@gmail.com>
Gradle Check (Jenkins) Run Completed with:
|
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more @@ Coverage Diff @@
## main #6823 +/- ##
============================================
+ Coverage 70.65% 70.78% +0.13%
- Complexity 59184 59221 +37
============================================
Files 4810 4810
Lines 283487 283494 +7
Branches 40877 40878 +1
============================================
+ Hits 200299 200674 +375
+ Misses 66761 66358 -403
- Partials 16427 16462 +35
... and 500 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Signed-off-by: Kuanysh <kuanysh4646@gmail.com>
Gradle Check (Jenkins) Run Completed with:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Description
Update the validator to take a boolean parameter switching between "fail if it matches" or "fail if it doesn't match". This provides the most flexibility.
Issues Resolved
opensearch-project/opensearch-sdk-java#591
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.