Skip to content
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

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

ominibyte
Copy link
Contributor

@ominibyte ominibyte commented Sep 28, 2024

This PR addresses the following:

  1. Fixes an issue when ordering by multiple fields with different sort orders
  2. Closes Support for Firestore OR queries #293 with support for Filter.or and Filter.and

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
@ominibyte ominibyte force-pushed the fix-multiple-orderby branch from 64dc22b to 8b3ede9 Compare October 8, 2024 04:43
@ominibyte ominibyte changed the title Fix order by multiple field with different orders Fix order by multiple field with different orders and added Filter support. Oct 8, 2024
@atn832
Copy link
Owner

atn832 commented Oct 15, 2024

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.

Copy link
Owner

@atn832 atn832 left a 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);
Copy link
Owner

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;

Copy link
Owner

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) {
Copy link
Owner

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);

Copy link
Owner

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(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice

Copy link
Owner

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

@atn832 atn832 Nov 6, 2024

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 {
Copy link
Owner

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'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@atn832 atn832 merged commit 661f08c into atn832:master Nov 6, 2024
4 checks passed
@atn832
Copy link
Owner

atn832 commented Nov 6, 2024

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!

@ominibyte ominibyte deleted the fix-multiple-orderby branch November 12, 2024 05:22
@ominibyte
Copy link
Contributor Author

Thanks, I saw the changes you made and the published version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Firestore OR queries
2 participants