Skip to content

Commit

Permalink
support parenthesis as group expression
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Feb 1, 2022
1 parent 6015b3e commit 4240bf2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -64,13 +65,18 @@ private List<List<String>> buildFilters(List list) {

/** Build filters from (legacy) string */
private List<List<String>> buildFilters(String string) {
if (string.startsWith("(") && string.endsWith(")")) {
String input = string.substring(1, string.length() - 1);
return Collections.singletonList(Arrays.asList(input.split(",")));
} else {
return Arrays.stream(string.split(","))
.map(Collections::singletonList)
.collect(Collectors.toList());
}
// Extract groups: "(A:1,B:2),C:3" -> ["(A:1,B:2)","C:3"]
List<String> groups = Arrays.asList(string.split(",(?![^()]*\\))"));
return groups.stream()
.map(
group -> {
if (group.startsWith("(") && group.endsWith(")")) {
String input = group.substring(1, group.length() - 1);
return Arrays.asList(input.split(","));
} else {
return Collections.singletonList(group);
}
})
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ void testLegacyFiltersFormat(String input) throws IOException {
input));

// Testing "one string with parenthesis" legacy filters => should be converted to "ORED" filters
// [["color:green", "color:yellow"]]
// [["color:green", "color:yellow"], ["color:blue"]]
String stringParenthesisFilters =
String.format("{\"%s\":\"(color:green,color:yellow)\"}", input);
assertOREDResult(
String.format("{\"%s\":\"(color:green,color:yellow),color:blue\"}", input);
assertOREDLatestResult(
extractFilters(
Defaults.getObjectMapper().readValue(stringParenthesisFilters, ConsequenceParams.class),
input));

// Testing mixed case with array and string
// [["color:green","color:yellow"],"color:blue"]
// [["color:green","color:yellow"], ["color:blue"]]
String stringAndArrayFilters =
String.format("{\"%s\":[[\"color:green\",\"color:yellow\"],\"color:blue\"]}", input);
List<List<String>> mixedDeserialized =
Expand Down

0 comments on commit 4240bf2

Please sign in to comment.