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(NODE-2883): Aggregate Operation should not require parent parameter #2918

Conversation

W-A-James
Copy link
Contributor

Description

Remove parent parameter from AggregateOperation and parent reference from AggregationCursor and change any instantiation of these internal classes to match their new signature

What changed?

  • src/change_stream.ts
  • src/collection.ts
  • src/cursor/aggregation_cursor.ts
  • src/db.ts
  • src/operations/aggregate.ts
  • src/operations/count_documents.ts

@W-A-James W-A-James requested a review from nbbeeken July 26, 2021 18:37
@W-A-James W-A-James changed the title fix(NODE-2883): Remove parent parameter from AggregateOperation fix(NODE-2883): Aggregate Operation should not require parent parameter Jul 26, 2021
@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Jul 26, 2021
src/cursor/aggregation_cursor.ts Outdated Show resolved Hide resolved
src/operations/aggregate.ts Outdated Show resolved Hide resolved
src/operations/aggregate.ts Outdated Show resolved Hide resolved
src/operations/aggregate.ts Outdated Show resolved Hide resolved
@W-A-James W-A-James requested a review from nbbeeken July 26, 2021 19:43
src/cursor/aggregation_cursor.ts Outdated Show resolved Hide resolved
parent.s.namespace && parent.s.namespace.collection
? parent.s.namespace.collection
: DB_AGGREGATE_COLLECTION;
this.target = ns.collection || DB_AGGREGATE_COLLECTION;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
this.target = ns.collection || DB_AGGREGATE_COLLECTION;
this.target = ns.collection ?? DB_AGGREGATE_COLLECTION;

just a nit, optional

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed that because when ns.collection is set to the empty string, we set the target to the empty string instead of DB_AGGREGATE_COLLECTION, which leads to a few test failures

Copy link
Contributor

Choose a reason for hiding this comment

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

ah Fascinating, I feel like that makes this worthy of more explicit code.

This but with curly braces

if (ns.collection === undefined || ns.collection === '') this.target = DB_AGGREGATE_COLLECTION;
else this.target = ns.collection;

Or we can bring back the ternary, Or leave it as is with a comment, // covers undefined and empty string, It's just easy to over look if the code itself or a comment doesn't call it out, what do you think?

Copy link
Contributor Author

@W-A-James W-A-James Jul 26, 2021

Choose a reason for hiding this comment

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

yeah that sounds good. Makes sense to have it be more explicit so the next person to see it and thinks to change it won't have to go bughunting like I did

Copy link
Contributor

@dariakp dariakp Jul 27, 2021

Choose a reason for hiding this comment

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

Lol I just noticed this after doing my review... I think a comment would be better; explicit undefined checks are somewhat weird and puts into question why we aren't validating for the other excluded cases... if we're concerned about nullish values but only these ones, why aren't we throwing errors on the other kinds? Edit: just to clarify, what I'm trying to convey is that reading that code with just that check there communicates to me that we are ok with assigning null to the target, but not undefined, and that, logically, feels wrong; but if we aren't ok with assigning null to the target, then I would expect some sort of validation that throws an error if we somehow do pass in null, and that validation is missing here, which makes the intent of the code seemingly inconsistent (it's important to remember that typescript types don't guarantee that at runtime only those types will actually be passed in those inputs, so "manual" validation is still valuable wherever such validation is important).

Copy link
Contributor

Choose a reason for hiding this comment

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

if (typeof ns.collection === 'string' && ns.collection !== '') this.target = ns.collection
else this.target = DB_AGGREGATE_COLLECTION;

Ok one more code suggestion ^
But also reverting to the ternary and using the same nullish that was there before along with a comment is a good solution to me as well. Your point is correct we should make sure that we are actually filtering for our intention and not leaving a gap that might suggest null is okay here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd recommend against the potential behavior change unless truly necessary; and if we do make that behavior change, we'd want to throw on ns.collection not being null/undefined so that we can guard against unexpected input sneaking through (one of those runtime errors)... feels like overkill for this ticket though, hence easier to just keep the original behavior (this.target = ns.collection || DB_AGGREGATE_COLLECTION;)

Copy link
Contributor

@nbbeeken nbbeeken left a comment

Choose a reason for hiding this comment

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

LGTM

@nbbeeken nbbeeken added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels Jul 26, 2021
@nbbeeken nbbeeken requested a review from dariakp July 26, 2021 21:44
Copy link
Contributor

@dariakp dariakp left a comment

Choose a reason for hiding this comment

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

Just one small thing here

? parent.s.namespace.collection
: DB_AGGREGATE_COLLECTION;

if (ns.collection === undefined || ns.collection === '') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can capture the desired outcome with just this.target = ns.collection || DB_AGGREGATE_COLLECTION (it would preserve the previous logic, and it's different from existing logic in that it would still assign the target to null or 0 if those happened to get passed in as values; however, I don't see why we would want to start doing that)

Copy link
Contributor

Choose a reason for hiding this comment

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

(See my comment on the other thread)

@nbbeeken nbbeeken merged commit dc6e2d6 into 4.0 Jul 28, 2021
@nbbeeken nbbeeken deleted the NODE-2883/AggregationOperation-shoould-not-require-parent-parameter branch July 28, 2021 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
3 participants