Skip to content

Commit

Permalink
fix(firestore): allow FieldPath.documentId as a field argument in q…
Browse files Browse the repository at this point in the history
…ueries (#11443)
  • Loading branch information
russellwheatley committed Aug 15, 2023
1 parent 15f3cf5 commit 4e01a9d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,53 @@ void runQueryTests() {
expect(results.docs[1].data()['genre'], equals(['sci-fi', 'action']));
});

testWidgets('allow FieldPathType for Filter queries', (_) async {
CollectionReference<Map<String, dynamic>> collection =
await initializeTest('filter-path-type');

await Future.wait([
collection.doc('doc1').set({
'genre': ['fantasy', 'action'],
'rating': 4.5,
}),
collection.doc('doc2').set({
'genre': ['sci-fi', 'thriller'],
'rating': 3.8,
}),
collection.doc('doc3').set({
'genre': ['sci-fi', 'action'],
'rating': 4.2,
}),
collection.doc('doc4').set({
'genre': ['mystery', 'action'],
'rating': 4.7,
}),
]);

final results = await collection
.where(
Filter.or(
Filter.and(
Filter(FieldPath.documentId, isEqualTo: 'doc1'),
Filter('rating', isEqualTo: 4.5),
),
Filter.and(
Filter(FieldPath.documentId, isEqualTo: 'doc2'),
Filter('rating', isEqualTo: 3.8),
),
),
)
.orderBy(FieldPath.documentId, descending: false)
.get();

expect(results.docs.length, equals(2));
expect(results.docs[0].id, equals('doc1'));
expect(results.docs[0].data()['rating'], equals(4.5));

expect(results.docs[1].id, equals('doc2'));
expect(results.docs[1].data()['rating'], equals(3.8));
});

testWidgets('allow multiple conjunctive queries', (_) async {
CollectionReference<Map<String, dynamic>> collection =
await initializeTest('multiple-conjunctive-queries');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';

import 'internal/field_path_type.dart';

class _FilterObject {
Map<String, Object?> build() {
throw UnimplementedError();
}
}

class _FilterQuery extends _FilterObject {
_FilterQuery(this._field, this._operator, this._value);
_FilterQuery(this._field, this._operator, this._value)
: assert(_field is FieldPathType || _field is FieldPath);

final FieldPath _field;
final Object _field;
final String _operator;
final Object? _value;

Expand Down Expand Up @@ -114,9 +117,10 @@ class Filter {
}(),
'Exactly one operator must be specified',
),
assert(field is String || field is FieldPath) {
final _field =
field is String ? FieldPath.fromString(field) : field as FieldPath;
assert(
field is String || field is FieldPath || field is FieldPathType,
) {
final _field = (field is String ? FieldPath.fromString(field) : field);

_filterQuery = _FilterQuery(
_field,
Expand Down

0 comments on commit 4e01a9d

Please sign in to comment.