Skip to content

Commit

Permalink
refactor(symbol): rename Symbol to BSONSymbol
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This was conflicting with the ES6 Symbol type
  • Loading branch information
mbroadst committed Oct 23, 2018
1 parent b0fda90 commit 5d5b3d2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/bson.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Double = require('./double');
const Timestamp = require('./timestamp');
const ObjectId = require('./objectid');
const BSONRegExp = require('./regexp');
const Symbol = require('./symbol');
const BSONSymbol = require('./symbol');
const Int32 = require('./int_32');
const Code = require('./code');
const Decimal128 = require('./decimal128');
Expand Down Expand Up @@ -250,7 +250,7 @@ module.exports = {
// wrapped types
Code,
Map,
Symbol,
BSONSymbol,
DBRef,
Binary,
ObjectId,
Expand Down
4 changes: 2 additions & 2 deletions lib/parser/calculate_size.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Long = require('../long');
const Double = require('../double');
const Timestamp = require('../timestamp');
const ObjectId = require('../objectid');
const Symbol = require('../symbol');
const BSONSymbol = require('../symbol');
const BSONRegExp = require('../regexp');
const Code = require('../code');
const Decimal128 = require('../decimal128');
Expand Down Expand Up @@ -146,7 +146,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1)
);
}
} else if (value instanceof Symbol || value['_bsontype'] === 'Symbol') {
} else if (value instanceof BSONSymbol || value['_bsontype'] === 'Symbol') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
Buffer.byteLength(value.value, 'utf8') +
Expand Down
6 changes: 6 additions & 0 deletions lib/parser/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ function serializeBSONRegExp(buffer, key, value, index, isArray) {
}

function serializeMinMax(buffer, key, value, index, isArray) {
console.log({
value,
MinKey,
isMinKey: value instanceof MinKey
});

// Write the type of either min or max key
if (value === null) {
buffer[index++] = constants.BSON_DATA_NULL;
Expand Down
8 changes: 4 additions & 4 deletions lib/symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* A class representation of the BSON Symbol type.
*/
class Symbol {
class BSONSymbol {
/**
* Create a Symbol type
*
Expand Down Expand Up @@ -54,9 +54,9 @@ class Symbol {
* @ignore
*/
static fromExtendedJSON(doc) {
return new Symbol(doc.$symbol);
return new BSONSymbol(doc.$symbol);
}
}

Object.defineProperty(Symbol.prototype, '_bsontype', { value: 'Symbol' });
module.exports = Symbol;
Object.defineProperty(BSONSymbol.prototype, '_bsontype', { value: 'Symbol' });
module.exports = BSONSymbol;
2 changes: 2 additions & 0 deletions test/node/bson_corpus_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ describe('BSON Corpus', function() {
const dEJ = normalize(v.degenerate_extjson);
const roundTrippedDEJ = nativeToCEJSON(jsonToNative(dEJ));
expect(roundTrippedDEJ).to.equal(cEJ);

if (!v.lossy) {
expect(nativeToBson(jsonToNative(dEJ))).to.deep.equal(cB);
}
Expand All @@ -195,6 +196,7 @@ describe('BSON Corpus', function() {
let rEJ = normalize(v.relaxed_extjson);
// BSON -> native -> relaxed EJSON matches provided
expect(nativeToREJSON(nativeFromCB)).to.equal(rEJ);

// relaxed EJSON -> native -> relaxed EJSON unchanged
expect(nativeToREJSON(jsonToNative(rEJ))).to.equal(rEJ);
}
Expand Down
8 changes: 4 additions & 4 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Binary = BSON.Binary;
const Timestamp = BSON.Timestamp;
const Long = BSON.Long;
const ObjectId = BSON.ObjectId;
const Symbol = BSON.Symbol;
const BSONSymbol = BSON.BSONSymbol;
const DBRef = BSON.DBRef;
const Decimal128 = BSON.Decimal128;
const Int32 = BSON.Int32;
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('BSON', function() {
expect(_mongodb.Timestamp === Timestamp).to.be.ok;
expect(_mongodb.Code === Code).to.be.ok;
expect(_mongodb.DBRef === DBRef).to.be.ok;
expect(_mongodb.Symbol === Symbol).to.be.ok;
expect(_mongodb.BSONSymbol === BSONSymbol).to.be.ok;
expect(_mongodb.MinKey === MinKey).to.be.ok;
expect(_mongodb.MaxKey === MaxKey).to.be.ok;
expect(_mongodb.Double === Double).to.be.ok;
Expand Down Expand Up @@ -1377,10 +1377,10 @@ describe('BSON', function() {
* @ignore
*/
it('Should Correctly Serialize and Deserialize Symbol', function(done) {
if (Symbol != null) {
if (BSONSymbol != null) {
// symbols are deprecated, so upgrade to strings... so I'm not sure
// we really need this test anymore...
//var doc = { b: [new Symbol('test')] };
//var doc = { b: [new BSONSymbol('test')] };

var doc = { b: ['test'] };
var serialized_data = BSON.serialize(doc);
Expand Down
6 changes: 3 additions & 3 deletions test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const MaxKey = BSON.MaxKey;
const MinKey = BSON.MinKey;
const ObjectID = BSON.ObjectID;
const BSONRegExp = BSON.BSONRegExp;
const Symbol = BSON.Symbol;
const BSONSymbol = BSON.BSONSymbol;
const Timestamp = BSON.Timestamp;

describe('Extended JSON', function() {
Expand All @@ -43,7 +43,7 @@ describe('Extended JSON', function() {
minKey: new MinKey(),
objectId: ObjectID.createFromHexString('111111111111111111111111'),
regexp: new BSONRegExp('hello world', 'i'),
symbol: new Symbol('symbol'),
symbol: new BSONSymbol('symbol'),
timestamp: Timestamp.fromNumber(1000),
int32Number: 300,
doubleNumber: 200.2,
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('Extended JSON', function() {
minKey: new MinKey(),
objectID: ObjectID.createFromHexString('111111111111111111111111'),
bsonRegExp: new BSONRegExp('hello world', 'i'),
symbol: new Symbol('symbol'),
symbol: new BSONSymbol('symbol'),
timestamp: new Timestamp()
};

Expand Down

0 comments on commit 5d5b3d2

Please sign in to comment.