Browse Source

Merge pull request #365 from dhalperi/337-subset-with-tests

implement subsetof filter operator
pull/378/head
kallestenflo 7 years ago committed by GitHub
parent
commit
65ceb10177
  1. 1
      README.md
  2. 27
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  3. 31
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java
  4. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java
  5. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java
  6. 10
      json-path/src/test/java/com/jayway/jsonpath/FilterParseTest.java
  7. 20
      json-path/src/test/java/com/jayway/jsonpath/FilterTest.java

1
README.md

@ -100,6 +100,7 @@ Filters are logical expressions used to filter arrays. A typical filter would be
| =~ | left matches regular expression [?(@.name =~ /foo.*?/i)] |
| in | left exists in right [?(@.size in ['S', 'M'])] |
| nin | left does not exists in right |
| subsetof | left is a subset of right [?(@.sizes subsetof ['S', 'M', 'L'])] |
| size | size of left (array or string) should match right |
| empty | left (array or string) should be empty |

27
json-path/src/main/java/com/jayway/jsonpath/Criteria.java

@ -267,6 +267,33 @@ public class Criteria implements Predicate {
return this;
}
/**
* The <code>subsetof</code> operator selects objects for which the specified field is
* an array whose elements comprise a subset of the set comprised by the elements of
* the specified array.
*
* @param o the values to match against
* @return the criteria
*/
public Criteria subsetof(Object... o) {
return subsetof(Arrays.asList(o));
}
/**
* The <code>subsetof</code> operator selects objects for which the specified field is
* an array whose elements comprise a subset of the set comprised by the elements of
* the specified array.
*
* @param c the values to match against
* @return the criteria
*/
public Criteria subsetof(Collection<?> c) {
notNull(c, "collection can not be null");
this.criteriaType = RelationalOperator.SUBSETOF;
this.right = new ValueNode.ValueListNode(c);
return this;
}
/**
* The <code>all</code> operator is similar to $in, but instead of matching any value
* in the specified array all values in the array must be matched.

31
json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java

@ -29,6 +29,7 @@ public class EvaluatorFactory {
evaluators.put(RelationalOperator.CONTAINS, new ContainsEvaluator());
evaluators.put(RelationalOperator.MATCHES, new PredicateMatchEvaluator());
evaluators.put(RelationalOperator.TYPE, new TypeEvaluator());
evaluators.put(RelationalOperator.SUBSETOF, new SubsetOfEvaluator());
}
public static Evaluator createEvaluator(RelationalOperator operator){
@ -264,4 +265,34 @@ public class EvaluatorFactory {
return input;
}
}
private static class SubsetOfEvaluator implements Evaluator {
@Override
public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateContext ctx) {
ValueNode.ValueListNode rightValueListNode;
if(right.isJsonNode()){
ValueNode vn = right.asJsonNode().asValueListNode(ctx);
if(vn.isUndefinedNode()){
return false;
} else {
rightValueListNode = vn.asValueListNode();
}
} else {
rightValueListNode = right.asValueListNode();
}
ValueNode.ValueListNode leftValueListNode;
if(left.isJsonNode()){
ValueNode vn = left.asJsonNode().asValueListNode(ctx);
if(vn.isUndefinedNode()){
return false;
} else {
leftValueListNode = vn.asValueListNode();
}
} else {
leftValueListNode = left.asValueListNode();
}
return leftValueListNode.subsetof(rightValueListNode);
}
}
}

3
json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java

@ -29,7 +29,8 @@ public enum RelationalOperator {
EXISTS("EXISTS"),
TYPE("TYPE"),
MATCHES("MATCHES"),
EMPTY("EMPTY");
EMPTY("EMPTY"),
SUBSETOF("SUBSETOF");
private final String operatorString;

9
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java

@ -713,6 +713,15 @@ public abstract class ValueNode {
return nodes.contains(node);
}
public boolean subsetof(ValueListNode right) {
for (ValueNode leftNode : nodes) {
if (!right.nodes.contains(leftNode)) {
return false;
}
}
return true;
}
public List<ValueNode> getNodes() {
return Collections.unmodifiableList(nodes);
}

10
json-path/src/test/java/com/jayway/jsonpath/FilterParseTest.java

@ -1,5 +1,6 @@
package com.jayway.jsonpath;
import java.util.Collections;
import org.assertj.core.api.Assertions;
import org.junit.Test;
@ -142,6 +143,15 @@ public class FilterParseTest {
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_subsetof_filter_can_be_serialized() {
String filter = filter(where("a").subsetof(Collections.emptyList())).toString();
String parsed = parse("[?(@['a'] SUBSETOF [])]").toString();
assertThat(filter).isEqualTo(parsed);
}
@Test
public void a_exists_filter_can_be_serialized() {

20
json-path/src/test/java/com/jayway/jsonpath/FilterTest.java

@ -1,5 +1,7 @@
package com.jayway.jsonpath;
import java.util.ArrayList;
import org.assertj.core.util.Lists;
import org.junit.Test;
import java.util.HashMap;
@ -358,6 +360,24 @@ public class FilterTest extends BaseTest {
assertThat(filter(where("null-key").size(6)).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
//
// SUBSETOF
//
//----------------------------------------------------------------------------
@Test
public void array_subsetof_evals() {
// list is a superset
List<String> list = Lists.newArrayList("a", "b", "c", "d", "e", "f", "g");
assertThat(filter(where("string-arr").subsetof(list)).apply(createPredicateContext(json))).isEqualTo(true);
// list is exactly the same set (but in a different order)
list = Lists.newArrayList("e", "d", "b", "c", "a");
assertThat(filter(where("string-arr").subsetof(list)).apply(createPredicateContext(json))).isEqualTo(true);
// list is missing one element
list = Lists.newArrayList("a", "b", "c", "d");
assertThat(filter(where("string-arr").subsetof(list)).apply(createPredicateContext(json))).isEqualTo(false);
}
//----------------------------------------------------------------------------
//
// EXISTS

Loading…
Cancel
Save