Skip to content

Commit

Permalink
support legacy string with parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Feb 1, 2022
1 parent 99a40c3 commit 2a00803
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 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 @@ -33,10 +34,8 @@ public List<List<String>> deserialize(JsonParser p, DeserializationContext ctxt)
result = buildFilters(list);
break;
case VALUE_STRING:
result =
Arrays.stream(p.getValueAsString().split(","))
.map(Collections::singletonList)
.collect(Collectors.toList());
String string = p.getValueAsString();
result = buildFilters(string);
break;
case VALUE_NULL:
break;
Expand All @@ -48,6 +47,7 @@ public List<List<String>> deserialize(JsonParser p, DeserializationContext ctxt)
return result;
}

/** Build filters from a list */
@SuppressWarnings("unchecked")
private List<List<String>> buildFilters(List list) {
return (List<List<String>>)
Expand All @@ -62,4 +62,16 @@ private List<List<String>> buildFilters(List list) {
})
.collect(Collectors.toList());
}

/** 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ void testLegacyFiltersFormat(String input) throws IOException {
// Testing "one string" legacy filters => should be converted to "ANDED" filters
// [["color:green"],["color:yellow"]]
String stringFilters = String.format("{\"%s\":\"color:green,color:yellow\"}", input);

assertANDEDListResult(
extractFilters(
Defaults.getObjectMapper().readValue(stringFilters, ConsequenceParams.class), input));
Expand All @@ -193,6 +192,13 @@ void testLegacyFiltersFormat(String input) throws IOException {
Defaults.getObjectMapper().readValue(nestedArrayFilters, ConsequenceParams.class),
input));

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

// Testing mixed case with array and string
// [["color:green","color:yellow"],"color:blue"]
String stringAndArrayFilters =
Expand Down

0 comments on commit 2a00803

Please sign in to comment.