-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The following format is expected to be supported. #1
Comments
Hi @vampireslove, First of all I appreciate your feedback, however, your example is not according to the BNF {
"<path1>" : { "<operator>": "<value> | @<path2>" }
} It lacks the But, your example revealed a false positive which I fixed in // Issue: #1
@Test
public void testSimpleMatch() throws Exception {
PredicateFactoryJson factory = new PredicateFactoryJson();
Map<String, Object> mapData = new HashMap<>();
String rule = null;
// exact match
rule = "{\n" + " \"$and\": [\n" + " {\n" + " \"source\": {\"$eq\":\"weather2\"}\n" + " }\n" + " ]\n" + "}";
Predicate<Object> pred = factory.read(rule.getBytes());
// positive test
mapData.put("source", "weather2");
Assert.assertTrue(pred.test(mapData));
// negative test
mapData.put("source", "weather3");
Assert.assertFalse(pred.test(mapData));
// example with contains 'weather'
rule = "{\n" + " \"$and\": [\n" + " {\n" + " \"source\": {\"$contains\":\"weather\"}\n" + " }\n" + " ]\n" + "}";
mapData.put("source", "weather2");
pred = factory.read(rule.getBytes());
Assert.assertTrue(pred.test(mapData));
mapData.put("source", "weather3");
Assert.assertTrue(pred.test(mapData));
mapData.put("source", "anyvalue");
Assert.assertFalse(pred.test(mapData));
} Best regards, Thiago. |
Hi @thiagolvlsantos ,
|
Modified io.github.thiagolvlsantos.json.predicate.impl.PredicateFactoryJson fields method 。
|
Hi @vampireslove, Now I can see your point, in fact these kind of syntactic suggar are very welcome. I will incorporate your suggestion/code and later try refactor it to a more general rewriting engine in order to "sanitize expressions" to the stricter form. I mean, any reduced or complex expression can be rewritten in terms of the expected "normal form"; it seams to be the case. With this abstraction anyone can include or rewrite as wish the accepted expressions. []'s |
I´ll also include a new README section to it when it`s done. Ps. Keeping this issue open until the refactoring is done. []'s |
@thiagolvlsantos Looking forward to the idea ... |
Hi @vampireslove, I made it very simpler from start: only call rewrite activated for missing operators add it as "$eq". In the future it can be done in advance just after parsing the JSON, something like: sanitizing the rule before application. However this approach could also bring disvantages, the user could not know exactly which rule is being performed. Something like not writing HTML properly where browsers have different behaviors, usualy is prefered to abort in order avoid missunderstandings, this approach is used by the XML lib XOM, for example. Ps. If you consider it`s enough by now, please close the issue. Best regards, Thiago. |
// {"name": "projectA"}
@test
public void testSimpleMatch() throws Exception {
String rule ="{\n" +
" "$and": [\n" +
" {\n" +
" "source": "weather2"\n" +
" }\n" +
" ]\n" +
"}";
System.out.println(rule);
Map<String,Object> mapData = new HashMap<>();
mapData.put("source","weather2");
ObjectMapper mapper = new ObjectMapper();
Predicate pred = factory.read(rule.getBytes());
boolean test = pred.test(mapData);
Assert.assertTrue(test);
}
The text was updated successfully, but these errors were encountered: