-
Notifications
You must be signed in to change notification settings - Fork 93
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
Fix order by multiple field with different orders and added Filter support. #319
Conversation
52262ad
to
64dc22b
Compare
This PR addresses the following: 1. Fixes an issue when ordering by multiple fields with different sort orders 2. Closes atn832#293 with support for Filter.or and Filter.and
64dc22b
to
8b3ede9
Compare
Wow thank you for tackling these big issues! It's a shame you didn't split it into two PRs. It'll take me quite a bit longer to review. Would you mind splitting them if it is not too much to ask? If the issues are tightly coupled, don't worry about it and I'll review this PR in a week or two. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for your excellent PR!
} | ||
|
||
sortedList.sort((d1, d2) { | ||
var compare = doCompare(fields.first, directions.first, d1, d2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's just a matter of preference. This could work too and is sligthly more readable:
for (var i = 0; i < fields.length; i++) {
final compare = doCompare(fields[i], directions[i], d1, d2);
if (compare != 0) {
return compare;
}
}
return 0;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up making the change at 57311c7.
whereNotIn: whereNotIn, | ||
isNull: isNull); | ||
}).toList(); | ||
if (field is Filter) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first glance, it is a bit difficult to see that the only thing that changes is the predicate. We could rewrite it like so:
final predicate = field is Filter ?
_buildFilterPredicate(field.toJson())
: (document) => _wherePredicate(document, field,
isEqualTo: isEqualTo,
isNotEqualTo: isNotEqualTo,
isLessThan: isLessThan,
isLessThanOrEqualTo: isLessThanOrEqualTo,
isGreaterThan: isGreaterThan,
isGreaterThanOrEqualTo: isGreaterThanOrEqualTo,
arrayContains: arrayContains,
arrayContainsAny: arrayContainsAny,
whereIn: whereIn,
whereNotIn: whereNotIn,
isNull: isNull)
final operation = (List<DocumentSnapshot<T>> docs) => docs
.where(predicate)
.toList();
return MockQuery<T>(this, operation);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the change at 9af8832.
return MockQuery<T>(this, operation); | ||
} | ||
|
||
bool Function(DocumentSnapshot<T> document) _buildFilterPredicate( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps explaining briefly in the method's docs how a Filter gets serialized to JSON could help others understand the code faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub doesn't really show the preexisting tests above. So it is not immediately clear which tests were simply a copy/paste of existing tests with Filter applied, and which ones are novel. Pointing out the few ones that are novel would have helped me review.
@@ -1269,6 +1705,64 @@ void main() { | |||
]); | |||
}); | |||
|
|||
test('orderBy with second field descending', () async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome
.snapshots() | ||
.listen(expectAsync1((QuerySnapshot snapshot) { | ||
expect(snapshot.docs.length, equals(1)); | ||
expect(snapshot.docs.first.get('name'), equals('Washington')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
I just published your code to https://pub.dev/packages/fake_cloud_firestore/changelog#310 and updated the README with the new features you implemented. Thanks again! |
Thanks, I saw the changes you made and the published version. |
This PR addresses the following: