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

chore: expand inspect support of BSON types NODE-2875 #413

Merged

Conversation

rose-m
Copy link
Contributor

@rose-m rose-m commented Nov 16, 2020

Description

Adds support for the NodeJS inspect function to more BSON data types. This is an upstream change proposed by the mongosh team.

A custom inspect function is added for the following data types:

  • Binary
  • Code
  • DBRef
  • Decimal128
  • Int32
  • Long
  • MinKey
  • MaxKey
  • BSONSymbol
  • Timestamp

src/binary.ts Outdated
// with a non-standard length.
// eslint-disable-next-line no-fallthrough
default:
return `BinData(${this.sub_type}, "${asBuffer.toString('base64')}")`;
Copy link
Contributor

@addaleax addaleax Nov 16, 2020

Choose a reason for hiding this comment

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

@rose-m These are also shell types, so I think at least this one would probably be best changed as well (and, because the different names aren’t enough confusing, the arguments work pretty differently … BinData() in the shell takes subtype + base64 string in that order, Binary in BSON takes a “““binary””” string or buffer + subtype, i.e. in reverse order)… I think ideally this line could be something like

Suggested change
return `BinData(${this.sub_type}, "${asBuffer.toString('base64')}")`;
return `Binary(Buffer.from("${asBuffer.toString('base64')}", "base64"), ${this.sub_type})`;

but it sure isn’t pretty…

(edit: if you want to know why I triple-quoted “binary”: https://twitter.com/addaleax/status/1189711048682680320)

Copy link
Member

@mbroadst mbroadst left a comment

Choose a reason for hiding this comment

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

Really just one small nit, but otherwise LGTM!

src/code.ts Outdated Show resolved Hide resolved
const util = require('util');
const BSONSymbol = BSON.BSONSymbol;

describe('MinKey tests', function () {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
describe('MinKey tests', function () {
describe('BSONSymbol tests', function () {

Copy link
Member

Choose a reason for hiding this comment

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

oops, missed this one. Actually, now that we're here does it make sense to just add all of these tests in bson_tests instead? I'm imagining a future maintainer forgetting to change one of these in the future because they have to change ~10 files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I wasn't sure how to handle that and then went with the individual files. I could implement a test case like https://github.com/mongodb/js-bson/blob/master/test/node/bson_test.js#L1310 which would inspect an object with all the different types and directly compare that instead of individual test cases?

Copy link
Member

@mbroadst mbroadst Nov 17, 2020

Choose a reason for hiding this comment

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

I like the idea of checking the behavior in one place, it looks like maybe there's a build failure related to formatting of the string. Maybe its easier to test each type in isolation, rather than inspecting an object with each type? We've had spurious build failures in the past due to minor fluctuations in formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah alright now I see - sometimes it has line breaks, sometimes it doesn't. Will fix asap and split it up into individual cases to circumvent that.

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've now added individual cases per type in one block in bson_tests - let's hope these now work in evergreen 😄

@rose-m rose-m force-pushed the NODE-2875-expand-bson-type-inspect-support branch 2 times, most recently from 6bb1855 to 2110bae Compare November 17, 2020 08:58
@rose-m rose-m requested a review from mbroadst November 17, 2020 08:59
@rose-m rose-m force-pushed the NODE-2875-expand-bson-type-inspect-support branch from 2110bae to b309c22 Compare November 17, 2020 17:15
Copy link
Contributor

@emadum emadum left a comment

Choose a reason for hiding this comment

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

LGTM 👍

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.

Thanks for contributing this!
A few patches to the tests and it looks good to me

test/node/bson_test.js Outdated Show resolved Hide resolved
test/node/bson_test.js Outdated Show resolved Hide resolved
test/node/bson_test.js Outdated Show resolved Hide resolved
test/node/bson_test.js Outdated Show resolved Hide resolved
@rose-m rose-m requested a review from nbbeeken November 18, 2020 07:22
Copy link
Member

@mbroadst mbroadst 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 merged commit 50e4529 into mongodb:master Nov 18, 2020
@rose-m rose-m deleted the NODE-2875-expand-bson-type-inspect-support branch November 19, 2020 07:32
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.

6 participants