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 authored and deshsidd committed Jun 12, 2024
1 parent 0fff808 commit b17cac9
Showing 1 changed file with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,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 b17cac9

Please sign in to comment.