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

Feature/fix aggregate issue with converted queries #291

Conversation

ming-chu
Copy link
Contributor

This PR has fixed the aggregate query problem on converted queries.
#290 (comment)
The error log would be like:

type 'Movie' is not a subtype of type 'Map<String, dynamic>' in type cast
package:fake_cloud_firestore/src/fake_aggregate_query.dart 34:56   FakeAggregateQuery._getAggregateQuerySnapshotPlatform.<fn>
dart:_internal                                                     ListIterator.moveNext
package:fake_cloud_firestore/src/fake_aggregate_query.dart 107:27  FakeAggregateQuery.buildAggregateQueryResponseList
package:fake_cloud_firestore/src/fake_aggregate_query.dart 37:12   FakeAggregateQuery._getAggregateQuerySnapshotPlatform
package:fake_cloud_firestore/src/fake_aggregate_query.dart 21:22   FakeAggregateQuery.get

@@ -0,0 +1,16 @@
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';

extension AggregateTypeExtension on AggregateType {
Copy link
Owner

Choose a reason for hiding this comment

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

Fancy! 👍

@@ -28,10 +31,18 @@ class FakeAggregateQuery implements AggregateQuery {
@override
AggregateQuery count() => _query.count();

Map<String, dynamic> _getRawDocDataMap(QueryDocumentSnapshot<Object?> s) {
if (s is MockQueryDocumentSnapshot && s.snapshot is MockDocumentSnapshot) {
Copy link
Owner

Choose a reason for hiding this comment

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

Why not use an assert statement like you did in the other PR? flutter test does fail if the assert fails. If something does fails for unexpected reasons, let it fail so we can fix it properly next time. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, it's because I want to check the type with the is operator so that we can use the instance method without casting it again.
Let me think and revise it. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

after using DocumentSnapshot.get we don't need this method anymore, haha. Thank you!

AggregateQuerySnapshotPlatform _getAggregateQuerySnapshotPlatform({
required QuerySnapshot<Object?> snapshot,
}) {
final dataMaps = snapshot.docs.map((e) => e.data() as Map<String, dynamic>);
final dataMaps = snapshot.docs.map((s) => _getRawDocDataMap(s));
Copy link
Owner

Choose a reason for hiding this comment

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

Have you considered using DocumentSnapshot.get? Then you don't need to expose the internal map. It's better to use the public API whenever possible.

You would replace

final value = dataMap[field.field];

by

final value = documentSnapshot.get(field.field);

It also has the added benefit to make the loop down there clearer. Instead of this:

    for (final dataMap in dataMaps) {
      for (final field in fields) {

you could write:

    for (final field in fields) {
      for (final documentSnapshot in documentSnapshots) {

Also would you mind renaming valueMap into something more meaningful like aggregateValues? Since you already use the variable name value when referring to a document value, value and valueMap was a bit confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea!

@ming-chu
Copy link
Contributor Author

@atn832 Thanks for the review.
I just pushed the new commit according to the comments.
Please let me know if I missed any :)

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 for the PR @ming-chu !

@atn832 atn832 merged commit 2625de8 into atn832:master Jan 14, 2024
4 checks passed
@atn832
Copy link
Owner

atn832 commented Jan 14, 2024

I published your fix to https://pub.dev/packages/fake_cloud_firestore/changelog#247.

I also switched to Dart 3 and used pattern matching to rewrite getAggregateFieldName: f277193

https://dart.dev/language/patterns#switch-statements-and-expressions

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.

None yet

2 participants