fix(firestore): handle fieldpath as array value (#3179)

Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>
Co-authored-by: Russell Wheatley <russellwheatley85@gmail.com>
This commit is contained in:
Elliot Hesp
2020-02-11 09:36:08 +00:00
committed by GitHub
parent c721f4b63f
commit 2cb6d44b77
5 changed files with 34 additions and 3 deletions

View File

@@ -22,10 +22,12 @@ import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.firestore.FieldPath;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.firestore.Source;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -60,7 +62,10 @@ public class ReactNativeFirebaseFirestoreQuery {
for (int i = 0; i < filters.size(); i++) {
ReadableMap filter = filters.getMap(i);
String fieldPath = Objects.requireNonNull(filter).getString("fieldPath");
ArrayList fieldPathArray = Objects.requireNonNull(filter).getArray("fieldPath").toArrayList();
String[] segmentArray = (String[]) fieldPathArray.toArray(new String[0]);
FieldPath fieldPath = FieldPath.of(Objects.requireNonNull(segmentArray));
String operator = filter.getString("operator");
ReadableArray arrayValue = filter.getArray("value");
Object value = parseTypeMap(query.getFirestore(), Objects.requireNonNull(arrayValue));

View File

@@ -374,4 +374,25 @@ describe('firestore().collection().where()', () => {
const snapshot = await colRef.where('category', 'array-contains-any', expect).get();
snapshot.size.should.eql(3); // 2nd record should only be returned once
});
it('returns with a FieldPath', async () => {
const colRef = firebase.firestore().collection('v6/filter/where-fieldpath');
const fieldPath = new firebase.firestore.FieldPath('map', 'foo.bar@gmail.com');
await colRef.add({
map: {
'foo.bar@gmail.com': true,
},
});
await colRef.add({
map: {
'bar.foo@gmail.com': true,
},
});
const snapshot = await colRef.where(fieldPath, '==', true).get();
snapshot.size.should.eql(1); // 2nd record should only be returned once
const data = snapshot.docs[0].data();
should.equal(data.map['foo.bar@gmail.com'], true);
});
});

View File

@@ -51,7 +51,8 @@
- (void)applyFilters {
for (NSDictionary *filter in _filters) {
NSString *fieldPath = filter[@"fieldPath"];
NSArray *fieldPathArray = filter[@"fieldPath"];
FIRFieldPath* fieldPath = [[FIRFieldPath init] initWithFields:fieldPathArray];
NSString *operator = filter[@"operator"];
id value = [RNFBFirestoreSerialize parseTypeMap:_firestore typeMap:filter[@"value"]];

View File

@@ -54,6 +54,10 @@ export default class FirestoreFieldPath {
_toPath() {
return this._segments.join('.');
}
_toArray() {
return this._segments;
}
}
export const DOCUMENT_ID = new FirestoreFieldPath('__name__');

View File

@@ -162,7 +162,7 @@ export default class FirestoreQueryModifiers {
where(fieldPath, opStr, value) {
const filter = {
fieldPath: fieldPath._toPath(),
fieldPath: fieldPath._toArray(),
operator: OPERATORS[opStr],
value: generateNativeData(value),
};