Skip to content

Commit dde59d4

Browse files
authored
feat(firestore, windows): add Filters to windows (#11726)
* feat(firestore, windows): add Filters to windows * format
1 parent 679beaa commit dde59d4

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

packages/cloud_firestore/cloud_firestore/windows/cloud_firestore_plugin.cpp

+80-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "firebase/app.h"
2424
#include "firebase/firestore.h"
25+
#include "firebase/firestore/filter.h"
2526
#include "firebase/log.h"
2627
#include "firebase_core/firebase_core_plugin_c_api.h"
2728
#include "messages.g.h"
@@ -1151,6 +1152,81 @@ std::vector<std::vector<EncodableValue>> ConvertToConditions(
11511152
return conditions;
11521153
}
11531154

1155+
using firebase::firestore::Filter;
1156+
1157+
firebase::firestore::Filter filterFromJson(const EncodableMap& map) {
1158+
if (map.find(EncodableValue("fieldPath")) != map.end()) {
1159+
// Deserialize a FilterQuery
1160+
std::string op = std::get<std::string>(map.at(EncodableValue("op")));
1161+
const FieldPath& fieldPath = std::any_cast<FieldPath>(
1162+
std::get<CustomEncodableValue>(map.at(EncodableValue("fieldPath"))));
1163+
1164+
auto value = map.at(EncodableValue("value"));
1165+
1166+
// All the operators from Firebase
1167+
if (op == "==") {
1168+
return Filter::EqualTo(fieldPath,
1169+
CloudFirestorePlugin::ConvertToFieldValue(value));
1170+
} else if (op == "!=") {
1171+
return Filter::NotEqualTo(
1172+
fieldPath, CloudFirestorePlugin::ConvertToFieldValue(value));
1173+
} else if (op == "<") {
1174+
return Filter::LessThan(fieldPath,
1175+
CloudFirestorePlugin::ConvertToFieldValue(value));
1176+
} else if (op == "<=") {
1177+
return Filter::LessThanOrEqualTo(
1178+
fieldPath, CloudFirestorePlugin::ConvertToFieldValue(value));
1179+
} else if (op == ">") {
1180+
return Filter::GreaterThan(
1181+
fieldPath, CloudFirestorePlugin::ConvertToFieldValue(value));
1182+
} else if (op == ">=") {
1183+
return Filter::GreaterThanOrEqualTo(
1184+
fieldPath, CloudFirestorePlugin::ConvertToFieldValue(value));
1185+
} else if (op == "array-contains") {
1186+
return Filter::ArrayContains(
1187+
fieldPath, CloudFirestorePlugin::ConvertToFieldValue(value));
1188+
} else if (op == "array-contains-any") {
1189+
// Here you should make sure 'value' is the correct type, e.g., a vector
1190+
return Filter::ArrayContainsAny(
1191+
fieldPath,
1192+
ConvertToFieldValueList(std::get<flutter::EncodableList>(value)));
1193+
} else if (op == "in") {
1194+
return Filter::In(
1195+
fieldPath,
1196+
ConvertToFieldValueList(std::get<flutter::EncodableList>(value)));
1197+
} else if (op == "not-in") {
1198+
return Filter::NotIn(
1199+
fieldPath,
1200+
ConvertToFieldValueList(std::get<flutter::EncodableList>(value)));
1201+
} else {
1202+
throw std::runtime_error("Invalid operator");
1203+
}
1204+
}
1205+
1206+
// Deserialize a FilterOperator
1207+
std::string op = std::get<std::string>(map.at(EncodableValue("op")));
1208+
// Assuming the queries are a list of maps
1209+
1210+
std::vector<EncodableMap> queries;
1211+
for (const auto& query :
1212+
std::get<flutter::EncodableList>(map.at(EncodableValue("queries")))) {
1213+
queries.push_back(std::get<EncodableMap>(query));
1214+
}
1215+
1216+
std::vector<Filter> parsedFilters;
1217+
for (const auto& query : queries) {
1218+
parsedFilters.push_back(filterFromJson(query));
1219+
}
1220+
1221+
if (op == "OR") {
1222+
return Filter::Or(parsedFilters);
1223+
} else if (op == "AND") {
1224+
return Filter::And(parsedFilters);
1225+
}
1226+
1227+
throw std::runtime_error("Invalid operator");
1228+
}
1229+
11541230
firebase::firestore::Query ParseQuery(firebase::firestore::Firestore* firestore,
11551231
const std::string& path,
11561232
bool isCollectionGroup,
@@ -1164,10 +1240,10 @@ firebase::firestore::Query ParseQuery(firebase::firestore::Firestore* firestore,
11641240
query = firestore->Collection(path);
11651241
}
11661242

1167-
// Assume filterFromJson function converts filters to appropriate Firestore
1168-
// filter
1169-
// TODO: not available in the SDK
1170-
// auto filter = filterFromJson(*parameters.filters());
1243+
if (parameters.filters()) {
1244+
Filter filter = filterFromJson(*parameters.filters());
1245+
query = query.Where(filter);
1246+
}
11711247

11721248
std::vector<std::vector<EncodableValue>> conditions =
11731249
ConvertToConditions(*parameters.where());

packages/firebase_core/firebase_core/windows/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# customers of the plugin.
55
cmake_minimum_required(VERSION 3.14)
66

7-
set(FIREBASE_SDK_VERSION "11.5.0")
7+
set(FIREBASE_SDK_VERSION "11.6.0")
88

99
if (EXISTS $ENV{FIREBASE_CPP_SDK_DIR}/include/firebase/version.h)
1010
file(READ "$ENV{FIREBASE_CPP_SDK_DIR}/include/firebase/version.h" existing_version)

0 commit comments

Comments
 (0)