-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove Generex and implement string generation from regex (6060)
[WIP] Generating string from RegEx --- ported the Go implementation --- more cleanup --- fmt --- minor simplification --- add reference links --- finalize and remove generex --- changelog --- Merge branch 'main' into string-from-regex
- Loading branch information
Showing
9 changed files
with
331 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/core/CharRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.openshift.client.dsl.internal.core; | ||
|
||
final class CharRange { | ||
private final char start; | ||
private final char end; | ||
|
||
public CharRange(char start, char end) { | ||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
public char start() { | ||
return start; | ||
} | ||
|
||
public char end() { | ||
return end; | ||
} | ||
|
||
public String rangeStr() { | ||
return new String(new char[] { start, end }); | ||
} | ||
|
||
} |
134 changes: 134 additions & 0 deletions
134
...src/main/java/io/fabric8/openshift/client/dsl/internal/core/ExpressionValueGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.openshift.client.dsl.internal.core; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
// from: https://github.com/openshift/library-go/blob/aed018c215a122871be1768155cf9f3e658278fc/pkg/template/generator/expressionvalue.go | ||
public class ExpressionValueGenerator { | ||
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
private static final String NUMERALS = "0123456789"; | ||
private static final String SYMBOLS = "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:`"; | ||
private static final String ASCII = ALPHABET + NUMERALS + SYMBOLS; | ||
|
||
private static final Pattern RANGE_EXP = Pattern.compile("([\\\\]?[a-zA-Z0-9]\\-?[a-zA-Z0-9]?)"); | ||
private static final Pattern GENERATORS_EXP = Pattern.compile("\\[([a-zA-Z0-9\\-\\\\]+)\\](\\{([0-9]+)\\})"); | ||
private static final Pattern EXPRESSION_EXP = Pattern.compile("\\[(\\\\w|\\\\d|\\\\a|\\\\A)|([a-zA-Z0-9]\\-[a-zA-Z0-9])+\\]"); | ||
|
||
private final Random random; | ||
|
||
public ExpressionValueGenerator(Random random) { | ||
this.random = random; | ||
} | ||
|
||
public String generateValue(final String expression) { | ||
String result = expression; | ||
Matcher matcher = GENERATORS_EXP.matcher(result); | ||
while (matcher.find()) { | ||
String matched = result.substring(matcher.start(), matcher.end()); | ||
String ranges = getRange(matched); | ||
int length = Integer.parseInt(getLength(matched)); | ||
|
||
result = replaceWithGenerated( | ||
result, | ||
matched, | ||
findExpressionPos(ranges), | ||
length, | ||
random); | ||
matcher = GENERATORS_EXP.matcher(result); | ||
} | ||
return result; | ||
} | ||
|
||
private static String alphabetSlice(char from, char to) { | ||
int leftPos = ASCII.indexOf(from); | ||
int rightPos = ASCII.lastIndexOf(to) + 1; | ||
if (leftPos > rightPos) { | ||
throw new IllegalArgumentException("invalid range specified: " + from + "-" + to); | ||
} | ||
return ASCII.substring(leftPos, rightPos); | ||
} | ||
|
||
private static String replaceWithGenerated(String s, String expression, List<CharRange> ranges, int length, Random random) { | ||
StringBuilder alphabet = new StringBuilder(); | ||
for (CharRange r : ranges) { | ||
switch (r.rangeStr()) { | ||
case "\\w": | ||
alphabet.append(ALPHABET).append(NUMERALS).append("_"); | ||
break; | ||
case "\\d": | ||
alphabet.append(NUMERALS); | ||
break; | ||
case "\\a": | ||
alphabet.append(ALPHABET).append(NUMERALS); | ||
break; | ||
case "\\A": | ||
alphabet.append(SYMBOLS); | ||
break; | ||
default: | ||
alphabet.append(alphabetSlice(r.start(), r.end())); | ||
break; | ||
} | ||
} | ||
String alphabetStr = removeDuplicateChars(alphabet.toString()); | ||
StringBuilder result = new StringBuilder(length); | ||
for (int i = 0; i < length; i++) { | ||
result.append(alphabetStr.charAt(random.nextInt(alphabetStr.length()))); | ||
} | ||
return s.replace(expression, result.toString()); | ||
} | ||
|
||
protected static String removeDuplicateChars(String input) { | ||
return input.chars() | ||
.distinct() | ||
.mapToObj(c -> String.valueOf((char) c)) | ||
.collect(Collectors.joining()); | ||
} | ||
|
||
private static List<CharRange> findExpressionPos(String s) { | ||
Matcher matcher = RANGE_EXP.matcher(s); | ||
List<CharRange> result = new ArrayList<>(); | ||
while (matcher.find()) { | ||
result.add(new CharRange(s.charAt(matcher.start()), s.charAt(matcher.end() - 1))); | ||
} | ||
return result; | ||
} | ||
|
||
private static String getRange(String s) { | ||
int lastOpenCurly = s.lastIndexOf("{"); | ||
String expr = s.substring(0, lastOpenCurly); | ||
if (!EXPRESSION_EXP.matcher(expr).find()) { | ||
throw new IllegalArgumentException("malformed expression syntax: " + expr); | ||
} | ||
return expr; | ||
} | ||
|
||
private static String getLength(String s) { | ||
int lastOpenCurly = s.lastIndexOf("{"); | ||
String lengthStr = s.substring(lastOpenCurly + 1, s.length() - 1); | ||
int length = Integer.parseInt(lengthStr); | ||
if (length > 0 && length <= 255) { | ||
return lengthStr; | ||
} | ||
throw new IllegalArgumentException("invalid range: must be within [1-255] characters (" + length + ")"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...test/java/io/fabric8/openshift/client/dsl/internal/core/ExpressionValueGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.openshift.client.dsl.internal.core; | ||
|
||
import org.junit.Test; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
|
||
import java.util.Random; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
// from: https://github.com/openshift/library-go/blob/aed018c215a122871be1768155cf9f3e658278fc/pkg/template/generator/expressionvalue_test.go | ||
public class ExpressionValueGeneratorTest { | ||
|
||
static class ExpressionValuesArgumentsProvider implements ArgumentsProvider { | ||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception { | ||
return Stream.of( | ||
Arguments.of("test[0-9]{1}x", "test6x"), | ||
Arguments.of("[0-1]{8}", "11100011"), | ||
Arguments.of("0x[A-F0-9]{4}", "0x545A"), | ||
Arguments.of("[a-zA-Z0-9]{8}", "KaP00gmR"), | ||
Arguments.of("test[A-Z0-9]{4}template", "testQU7Etemplate"), | ||
Arguments.of("[\\d]{3}", "645"), | ||
Arguments.of("[\\w]{20}", "0V86t3tosHvHdzUwQKTB"), | ||
Arguments.of("[\\a]{10}", "KaP00gmRS2"), | ||
Arguments.of("[\\A]{10}", ">|>~-{'>,]"), | ||
Arguments.of("strongPassword[\\w]{3}[\\A]{3}", "strongPassword0V8~-{"), | ||
Arguments.of("admin[0-9]{2}[A-Z]{2}", "admin64DK"), | ||
Arguments.of("admin[0-9]{2}test[A-Z]{2}", "admin64testDK")); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(ExpressionValuesArgumentsProvider.class) | ||
public void generateStringFromRegEx(String input, String expected) { | ||
// Arrange | ||
Random random = new Random(); | ||
random.setSeed(7); | ||
ExpressionValueGenerator generator = new ExpressionValueGenerator(random); | ||
|
||
// Act | ||
String result = generator.generateValue(input); | ||
|
||
// Assert | ||
assertEquals(expected, result); | ||
} | ||
|
||
static class DuplicatesArgumentsProvider implements ArgumentsProvider { | ||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception { | ||
return Stream.of( | ||
Arguments.of("abcdefgh", "abcdefgh"), | ||
Arguments.of("abcabc", "abc"), | ||
Arguments.of("1111111", "1"), | ||
Arguments.of("1234567890", "1234567890"), | ||
Arguments.of("test@@", "tes@")); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(DuplicatesArgumentsProvider.class) | ||
public void removeDuplicatedCharacters(String input, String expected) { | ||
// Arrange | ||
ExpressionValueGenerator generator = new ExpressionValueGenerator(new Random()); | ||
|
||
// Act | ||
String result = ExpressionValueGenerator.removeDuplicateChars(input); | ||
|
||
// Assert | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void maformedSyntax() { | ||
// Arrange | ||
ExpressionValueGenerator generator = new ExpressionValueGenerator(new Random()); | ||
|
||
// Act | ||
IllegalArgumentException result = assertThrows(IllegalArgumentException.class, | ||
() -> generator.generateValue("[ABC]{3}")); | ||
|
||
// Assert | ||
assertTrue(result.getMessage().contains("malformed")); | ||
assertTrue(result.getMessage().contains("syntax")); | ||
} | ||
|
||
@Test | ||
public void invalidRange() { | ||
// Arrange | ||
ExpressionValueGenerator generator = new ExpressionValueGenerator(new Random()); | ||
|
||
// Act | ||
IllegalArgumentException result = assertThrows(IllegalArgumentException.class, | ||
() -> generator.generateValue("[Z-A]{3}")); | ||
|
||
// Assert | ||
assertTrue(result.getMessage().contains("invalid")); | ||
assertTrue(result.getMessage().contains("range")); | ||
} | ||
|
||
@Test | ||
public void rangeOutOfBound() { | ||
// Arrange | ||
ExpressionValueGenerator generator = new ExpressionValueGenerator(new Random()); | ||
|
||
// Act | ||
IllegalArgumentException result = assertThrows(IllegalArgumentException.class, | ||
() -> generator.generateValue("[A-Z]{300}")); | ||
|
||
// Assert | ||
assertTrue(result.getMessage().contains("invalid")); | ||
assertTrue(result.getMessage().contains("range")); | ||
} | ||
|
||
@Test | ||
public void zeroRange() { | ||
// Arrange | ||
ExpressionValueGenerator generator = new ExpressionValueGenerator(new Random()); | ||
|
||
// Act | ||
IllegalArgumentException result = assertThrows(IllegalArgumentException.class, | ||
() -> generator.generateValue("[A-Z]{0}")); | ||
|
||
// Assert | ||
assertTrue(result.getMessage().contains("invalid")); | ||
assertTrue(result.getMessage().contains("range")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.