Skip to content

Commit

Permalink
feat(firestore): add the Filter class and support for the OR query (#…
Browse files Browse the repository at this point in the history
…10678)

* feat(firestore): add support for the OR query

* feat(firestore): add support for the OR query

* feat(firestore): add support for the OR query

* feat(firestore): add support for the OR query in Android

* feat(firestore): add support for the OR query in Android

* feat(firestore): add support for the OR query in Web

* feat(firestore): add support for the OR query with orderBy

* feat(firestore): test

* feat(firestore): licence

* feat(firestore): add documentation

* feat(firestore): add documentation

* feat(firestore): fix typing web

* feat(firestore): add assert
  • Loading branch information
Lyokone committed Mar 30, 2023
1 parent fcc8e9a commit ac43404
Show file tree
Hide file tree
Showing 14 changed files with 1,196 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FieldPath;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.Filter;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreSettings;
import com.google.firebase.firestore.GeoPoint;
Expand Down Expand Up @@ -332,6 +333,58 @@ private FirebaseFirestoreSettings readFirestoreSettings(ByteBuffer buffer) {
return settingsBuilder.build();
}

private Filter filterFromJson(Map<String, Object> map) {
if (map.containsKey("fieldPath")) {
// Deserialize a FilterQuery
String op = (String) map.get("op");
FieldPath fieldPath = (FieldPath) map.get("fieldPath");
Object value = map.get("value");

// All the operators from Firebase
switch (op) {
case "==":
return Filter.equalTo(fieldPath, value);
case "!=":
return Filter.notEqualTo(fieldPath, value);
case "<":
return Filter.lessThan(fieldPath, value);
case "<=":
return Filter.lessThanOrEqualTo(fieldPath, value);
case ">":
return Filter.greaterThan(fieldPath, value);
case ">=":
return Filter.greaterThanOrEqualTo(fieldPath, value);
case "array-contains":
return Filter.arrayContains(fieldPath, value);
case "array-contains-any":
return Filter.arrayContainsAny(fieldPath, (List<? extends Object>) value);
case "in":
return Filter.inArray(fieldPath, (List<? extends Object>) value);
case "not-in":
return Filter.notInArray(fieldPath, (List<? extends Object>) value);
default:
throw new Error("Invalid operator");
}
}
// Deserialize a FilterOperator
String op = (String) map.get("op");
List<Map<String, Object>> queries = (List<Map<String, Object>>) map.get("queries");

// Map queries recursively
ArrayList<Filter> parsedFilters = new ArrayList<>();
for (Map<String, Object> query : queries) {
parsedFilters.add(filterFromJson(query));
}

if (op.equals("OR")) {
return Filter.or(parsedFilters.toArray(new Filter[0]));
} else if (op.equals("AND")) {
return Filter.and(parsedFilters.toArray(new Filter[0]));
}

throw new Error("Invalid operator");
}

private Query readFirestoreQuery(ByteBuffer buffer) {
try {
@SuppressWarnings("unchecked")
Expand All @@ -353,6 +406,12 @@ private Query readFirestoreQuery(ByteBuffer buffer) {

if (parameters == null) return query;

boolean isFilterQuery = parameters.containsKey("filters");
if (isFilterQuery) {
Filter filter = filterFromJson((Map<String, Object>) parameters.get("filters"));
query = query.where(filter);
}

// "where" filters
@SuppressWarnings("unchecked")
List<List<Object>> filters =
Expand Down
Loading

0 comments on commit ac43404

Please sign in to comment.