Skip to content

Commit

Permalink
Refactor FilterPath parse function (opensearch-project#12067)
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Friedmann <[email protected]>
  • Loading branch information
robinf95 committed Feb 1, 2024
1 parent 2f8d267 commit 5abffd7
Showing 1 changed file with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ protected FilterPath(String filter, String segment, FilterPath next) {
this.segment = segment;
this.next = next;
this.simpleWildcard = (segment != null) && (segment.length() == 1) && (segment.charAt(0) == '*');
this.doubleWildcard = (segment != null) && (segment.length() == 2) && (segment.charAt(0) == '*') && (segment.charAt(1) == '*');
this.doubleWildcard =
(segment != null) && (segment.length() == 2) && (segment.charAt(0) == '*') && (segment.charAt(1) == '*');
}

private FilterPath() {
Expand Down Expand Up @@ -99,32 +100,26 @@ public static FilterPath[] compile(Set<String> filters) {

List<FilterPath> paths = new ArrayList<>();
for (String filter : filters) {
if (filter != null) {
if (filter != null && !filter.isEmpty()) {
filter = filter.trim();
if (filter.length() > 0) {
paths.add(parse(filter, filter));
paths.add(parse(filter));
}
}
}
return paths.toArray(new FilterPath[0]);
}

private static FilterPath parse(final String filter, final String segment) {
int end = segment.length();
private static FilterPath parse(final String filter) {
String[] segments = filter.split("(?<!\\\\)\\.");
FilterPath next = EMPTY;

for (int i = 0; i < end;) {
char c = segment.charAt(i);

if (c == '.') {
String current = segment.substring(0, i).replaceAll("\\\\.", ".");
return new FilterPath(filter, current, parse(filter, segment.substring(i + 1)));
}
++i;
if ((c == '\\') && (i < end) && (segment.charAt(i) == '.')) {
++i;
}
for (int i = segments.length - 1; i >= 0; i--) {
String segment = segments[i].replaceAll("\\\\.", ".");
next = new FilterPath(filter, segment, next);
}
return new FilterPath(filter, segment.replaceAll("\\\\.", "."), EMPTY);

return next;
}

@Override
Expand Down

0 comments on commit 5abffd7

Please sign in to comment.