-
Notifications
You must be signed in to change notification settings - Fork 236
feat(compass-collection): Follow-ups tweaks to convertBSONToPrimitive - CLOUDP-351455 #7456
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR implements follow-up improvements to the convertBSONToPrimitive
function based on code review feedback. The changes enhance error handling and improve type checking by using BSON type properties instead of instanceof checks.
- Replace instanceof checks with _bsontype property-based type detection
- Throw error for Binary data edge case instead of returning fallback value
- Add comprehensive test coverage for the Binary data error scenario
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
packages/compass-collection/src/transform-schema-to-field-info.ts | Refactored BSON type detection using _bsontype property and added error handling for Binary data |
packages/compass-collection/src/transform-schema-to-field-info.spec.ts | Added test case to verify Binary data error throwing behavior |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
packages/compass-collection/src/transform-schema-to-field-info.ts
Outdated
Show resolved
Hide resolved
packages/compass-collection/src/transform-schema-to-field-info.ts
Outdated
Show resolved
Hide resolved
/** | ||
* Helper to cast BSON objects to their specific types | ||
*/ | ||
function castBSONValue<T>(value: unknown): T { | ||
return value as T; | ||
} |
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.
This seems a bit backward to me. If you're moving away from instanceof
checks, I'd prefer leaning into type guards based on a union of expected BSON types. Actually surprised "bson" doesn't already have this union defined 🤔
I put together a TS playground to exemplify what I'm talking about.
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.
Well, they do expose the BSONValue
class, so you could make this
/** | |
* Helper to cast BSON objects to their specific types | |
*/ | |
function castBSONValue<T>(value: unknown): T { | |
return value as T; | |
} | |
/** | |
* Helper to cast BSON objects to their specific types | |
*/ | |
function castBSONValue<T extends BSONValue>(value: BSONValue): T { | |
return value as T; | |
} |
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.
The BSONValue
from typescript is the abstract super-class? It adds no constrains to the _bsontype
field beyond string
... seems much less usable than a union over the concrete implementations of that super-class.
The original and suggested code effectively use none of the type guarantees provided by "bson": There's nothing linking the switch-case statements to the particular BSONValue
- might as well implement it in JavaScript at that point 🤷
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.
Yes, that's correct, and I agree that it would be useful to have a union of all possible BSON values available somewhere 🙂
I think we effectively have that elsewhere in Compass, though
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.
we effectively have that elsewhere in Compass
That would be a good candidate for reuse, I'm not aware of such utility?
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.
Mentioned it below:
with
TypeCastMap
from the hadron-type-checker package.
It's not perfect but it should work here
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.
Went with the suggestion to use BSONValue
for now, which allowed me to remove that cast. Let me know what you think
(Edit: just refreshed this page and saw the new comments since)
/** | ||
* Helper to cast BSON objects to their specific types | ||
*/ | ||
function castBSONValue<T>(value: unknown): T { | ||
return value as T; | ||
} |
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.
Well, they do expose the BSONValue
class, so you could make this
/** | |
* Helper to cast BSON objects to their specific types | |
*/ | |
function castBSONValue<T>(value: unknown): T { | |
return value as T; | |
} | |
/** | |
* Helper to cast BSON objects to their specific types | |
*/ | |
function castBSONValue<T extends BSONValue>(value: BSONValue): T { | |
return value as T; | |
} |
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.
My specific concern is this type assertion and I've suggested better and more type-safe ways to express this.
Gotta disagree with this one. It's not a better suggestion. I think we're in alignment that it would be good to have the bson library expose a type that fully describes possible BSON types. That's a fair request, but it's not something we can handle in this PR on its own, it requires a change to the BSON library itself. If you currently want something that describes all possible BSON types, I'd go with |
Description
Followups addressing non-blocking comments from #7439
instanceof
Checklist
Types of changes