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

[Extensions] Adds a parameter to Setting Validator to toggle regex matching #6823

Merged
merged 5 commits into from
Apr 7, 2023

Conversation

Kuanysh-kst
Copy link
Contributor

@Kuanysh-kst Kuanysh-kst commented Mar 24, 2023

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

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff
  • Commit changes are listed out in CHANGELOG.md file (See: Changelog)

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.

Signed-off-by: Kuanysh <kuanysh4646@gmail.com>
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

Copy link
Member

@dbwiddis dbwiddis left a 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:

  1. 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".
  2. 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.
  3. 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() + "]");
Copy link
Member

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() + "]");
Copy link
Member

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".
Copy link
Member

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.
Copy link
Member

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);
Copy link
Member

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.

@Kuanysh-kst Kuanysh-kst changed the title [BUG] Hello World sample validator reverses logic Adds a parameter to Setting Validator to toggle regex matching Mar 24, 2023
Signed-off-by: Kuanysh <kuanysh4646@gmail.com>
@dbwiddis dbwiddis added the CCI College Contributor Initiative related issues and PRs label Mar 24, 2023
@github-actions
Copy link
Contributor

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..
Copy link
Member

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){
Copy link
Member

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);
Copy link
Member

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>
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

Copy link
Member

@dbwiddis dbwiddis left a 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);
Copy link
Member

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
Copy link
Member

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);
Copy link
Member

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>
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

  • RESULT: UNSTABLE ❕
  • TEST FAILURES:
      1 org.opensearch.backwards.MixedClusterClientYamlTestSuiteIT.test {p0=indices.shrink/30_copy_settings/Copy settings during shrink index}

@codecov-commenter
Copy link

codecov-commenter commented Mar 30, 2023

Codecov Report

Merging #6823 (15d5ba8) into main (4d78bbf) will increase coverage by 0.13%.
The diff coverage is 77.77%.

📣 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     
Impacted Files Coverage Δ
...n/java/org/opensearch/common/settings/Setting.java 86.20% <77.77%> (-0.12%) ⬇️

... 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>
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

  • RESULT: UNSTABLE ❕
  • TEST FAILURES:
      2 org.opensearch.backwards.MixedClusterClientYamlTestSuiteIT.test {p0=indices.split/30_copy_settings/Copy settings during split index}

@Kuanysh-kst Kuanysh-kst requested a review from dbwiddis April 3, 2023 09:52
Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

LGTM!

@Kuanysh-kst Kuanysh-kst changed the title Adds a parameter to Setting Validator to toggle regex matching [Extensions] Adds a parameter to Setting Validator to toggle regex matching Apr 6, 2023
@owaiskazi19 owaiskazi19 merged commit c765e3a into opensearch-project:main Apr 7, 2023
@ryanbogan ryanbogan added the backport 2.x Backport to 2.x branch label Apr 7, 2023
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 7, 2023
…tching (#6823)

(cherry picked from commit c765e3a)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
owaiskazi19 pushed a commit that referenced this pull request Apr 10, 2023
…tching (#6823) (#7054)

(cherry picked from commit c765e3a)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
austintlee pushed a commit to austintlee/OpenSearch that referenced this pull request Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x Backport to 2.x branch CCI College Contributor Initiative related issues and PRs skip-changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants