Skip to content

Fix querying for a ParseObject with JSONArray key #5

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

Merged
merged 1 commit into from
Aug 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Parse/src/main/java/com/parse/OfflineQueryLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.parse.ParseQuery.RelationConstraint;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
Expand Down Expand Up @@ -178,13 +179,33 @@ private static boolean compareList(Object constraint, List<?> values, Decider de
}

/**
* Returns true if decider returns true for any value in the given list.
*/
private static boolean compareArray(Object constraint, JSONArray values, Decider decider) {
for (int i = 0; i < values.length(); ++i) {
try {
if (decider.decide(constraint, values.get(i))) {
return true;
}
} catch (JSONException e) {
// This can literally never happen.
throw new RuntimeException(e);
}
}
return false;
}

/**
*
* Returns true if the decider returns true for the given value and the given constraint. This
* method handles Mongo's logic where an item can match either an item itself, or any item within
* the item, if the item is an array.
*/
private static boolean compare(Object constraint, Object value, Decider decider) {
if (value instanceof List) {
return compareList(constraint, (List<?>) value, decider);
} else if (value instanceof JSONArray) {
return compareArray(constraint, (JSONArray) value, decider);
} else {
return decider.decide(constraint, value);
}
Expand Down
50 changes: 50 additions & 0 deletions Parse/src/test/java/com/parse/OfflineQueryLogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,56 @@ public void testMatchesWithin() throws ParseException {

//endregion

//region compare

@Test
public void testCompareList() throws Exception {
ParseObject object = new ParseObject("SomeObject");
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
object.put("list", list);

ParseQuery.State<ParseObject> query;
OfflineQueryLogic logic = new OfflineQueryLogic(null);

query = new ParseQuery.State.Builder<>("SomeObject")
.whereEqualTo("list", 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test .whereEqualTo("list", 4) and make sure it returns false.

.build();
assertTrue(matches(logic, query, object));

query = new ParseQuery.State.Builder<>("SomeObject")
.whereEqualTo("list", 4)
.build();
assertFalse(matches(logic, query, object));
}

@Test
public void testCompareJSONArray() throws Exception {
ParseObject object = new ParseObject("SomeObject");
JSONArray array = new JSONArray();
array.put(1);
array.put(2);
array.put(3);
object.put("array", array);

ParseQuery.State<ParseObject> query;
OfflineQueryLogic logic = new OfflineQueryLogic(null);

query = new ParseQuery.State.Builder<>("SomeObject")
.whereEqualTo("array", 2)
.build();
assertTrue(matches(logic, query, object));

query = new ParseQuery.State.Builder<>("SomeObject")
.whereEqualTo("array", 4)
.build();
assertFalse(matches(logic, query, object));
}

//endregion

//region compareTo

@Test
Expand Down