Skip to content

Commit

Permalink
fix: make inspection result of BSON types evaluable (#416)
Browse files Browse the repository at this point in the history
Make sure that evaluating the inspection result of a BSON type
returns a result that can be evaluated to give an object equivalent
to the original.

NODE-2947
  • Loading branch information
addaleax authored Dec 4, 2020
1 parent b707f65 commit 616665f
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export class Binary {

inspect(): string {
const asBuffer = this.value(true);
return `Binary("${asBuffer.toString('hex')}", ${this.sub_type})`;
return `new Binary(Buffer.from("${asBuffer.toString('hex')}", "hex"), ${this.sub_type})`;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export class Code {

inspect(): string {
const codeJson = this.toJSON();
return `Code("${codeJson.code}"${codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
return `new Code("${codeJson.code}"${
codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''
})`;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/db_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ export class DBRef {
// NOTE: if OID is an ObjectId class it will just print the oid string.
const oid =
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
return `DBRef("${this.namespace}", "${oid}"${this.db ? `, "${this.db}"` : ''})`;
return `new DBRef("${this.namespace}", new ObjectId("${oid}")${
this.db ? `, "${this.db}"` : ''
})`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/decimal128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ export class Decimal128 {
}

inspect(): string {
return `Decimal128("${this.toString()}")`;
return `Decimal128.fromString("${this.toString()}")`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/double.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Double {

inspect(): string {
const eJSON = this.toExtendedJSON() as DoubleExtended;
return `Double(${eJSON.$numberDouble})`;
return `new Double(${eJSON.$numberDouble})`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/int_32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Int32 {
}

inspect(): string {
return `Int32(${this.valueOf()})`;
return `new Int32(${this.valueOf()})`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/long.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ export class Long {
}

inspect(): string {
return `Long("${this.toString()}")`;
return `new Long("${this.toString()}")`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/max_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class MaxKey {
}

inspect(): string {
return 'MaxKey()';
return 'new MaxKey()';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/min_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class MinKey {
}

inspect(): string {
return 'MinKey()';
return 'new MinKey()';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/objectid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export class ObjectId {
}

inspect(): string {
return `ObjectId("${this.toHexString()}")`;
return `new ObjectId("${this.toHexString()}")`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class BSONSymbol {

/** @internal */
inspect(): string {
return `BSONSymbol("${this.value}")`;
return `new BSONSymbol("${this.value}")`;
}

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion src/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ export class Timestamp extends LongWithoutOverridesClass {
}

inspect(): string {
return `Timestamp(${this.getLowBits().toString()}, ${this.getHighBits().toString()})`;
return `new Timestamp(${this.getLowBits().toString()}, ${this.getHighBits().toString()})`;
}
}
24 changes: 13 additions & 11 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2276,23 +2276,25 @@ describe('BSON', function () {
*/
it('Binary', function () {
const binary = new Binary(Buffer.from('0123456789abcdef0123456789abcdef', 'hex'), 4);
expect(inspect(binary)).to.equal('Binary("0123456789abcdef0123456789abcdef", 4)');
expect(inspect(binary)).to.equal(
'new Binary(Buffer.from("0123456789abcdef0123456789abcdef", "hex"), 4)'
);
});

/**
* @ignore
*/
it('BSONSymbol', function () {
const symbol = new BSONSymbol('sym');
expect(inspect(symbol)).to.equal('BSONSymbol("sym")');
expect(inspect(symbol)).to.equal('new BSONSymbol("sym")');
});

/**
* @ignore
*/
it('Code', function () {
const code = new Code('this.a > i', { i: 1 });
expect(inspect(code)).to.equal('Code("this.a > i", {"i":1})');
expect(inspect(code)).to.equal('new Code("this.a > i", {"i":1})');
});

/**
Expand All @@ -2302,7 +2304,7 @@ describe('BSON', function () {
const oid = new ObjectId('deadbeefdeadbeefdeadbeef');
const dbref = new DBRef('namespace', oid, 'integration_tests_');
expect(inspect(dbref)).to.equal(
'DBRef("namespace", "deadbeefdeadbeefdeadbeef", "integration_tests_")'
'new DBRef("namespace", new ObjectId("deadbeefdeadbeefdeadbeef"), "integration_tests_")'
);
});

Expand All @@ -2311,55 +2313,55 @@ describe('BSON', function () {
*/
it('Decimal128', function () {
const dec = Decimal128.fromString('1.42');
expect(inspect(dec)).to.equal('Decimal128("1.42")');
expect(inspect(dec)).to.equal('Decimal128.fromString("1.42")');
});

/**
* @ignore
*/
it('Double', function () {
const double = new Double(-42.42);
expect(inspect(double)).to.equal('Double(-42.42)');
expect(inspect(double)).to.equal('new Double(-42.42)');
});

/**
* @ignore
*/
it('Int32', function () {
const int = new Int32(42);
expect(inspect(int)).to.equal('Int32(42)');
expect(inspect(int)).to.equal('new Int32(42)');
});

/**
* @ignore
*/
it('Long', function () {
const long = Long.fromString('42');
expect(inspect(long)).to.equal('Long("42")');
expect(inspect(long)).to.equal('new Long("42")');
});

/**
* @ignore
*/
it('MaxKey', function () {
const maxKey = new MaxKey();
expect(inspect(maxKey)).to.equal('MaxKey()');
expect(inspect(maxKey)).to.equal('new MaxKey()');
});

/**
* @ignore
*/
it('MinKey', function () {
const minKey = new MinKey();
expect(inspect(minKey)).to.equal('MinKey()');
expect(inspect(minKey)).to.equal('new MinKey()');
});

/**
* @ignore
*/
it('Timestamp', function () {
const timestamp = new Timestamp(1, 100);
expect(inspect(timestamp)).to.equal('Timestamp(1, 100)');
expect(inspect(timestamp)).to.equal('new Timestamp(1, 100)');
});
});
});
2 changes: 1 addition & 1 deletion test/node/object_id_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('ObjectId', function () {
it('should correctly allow for node.js inspect to work with ObjectId', function (done) {
var a = 'AAAAAAAAAAAAAAAAAAAAAAAA';
var b = new ObjectId(a);
expect(util.inspect(b)).to.equal('ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa")');
expect(util.inspect(b)).to.equal('new ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa")');

done();
});
Expand Down

0 comments on commit 616665f

Please sign in to comment.