Commit 20412a16 by James Cropcho Committed by GitHub

Merge pull request #124 from todvora/master

NumberLong data type recognition
parents 955fc90e 5ee817fc
...@@ -36,6 +36,7 @@ module.exports = { ...@@ -36,6 +36,7 @@ module.exports = {
"collection": false, "collection": false,
"DBQuery": false, "DBQuery": false,
"BinData": false, "BinData": false,
"NumberLong": false,
"tojson": false "tojson": false
}, },
......
import { Binary } from 'mongodb'; import { Binary } from 'mongodb';
import { Long } from 'mongodb';
import Tester from './utils/Tester.js'; import Tester from './utils/Tester.js';
const test = new Tester('test', 'users'); const test = new Tester('test', 'users');
...@@ -10,7 +11,8 @@ const crazyObject = { ...@@ -10,7 +11,8 @@ const crazyObject = {
'key_binData-generic': new Binary('1234'), // TODO: how to create other bin-data types? 'key_binData-generic': new Binary('1234'), // TODO: how to create other bin-data types?
key_array: [], key_array: [],
key_object: {}, key_object: {},
key_null: null key_null: null,
key_long: Long.fromString('4611686018427387904')
}; };
describe('Data type recognition', () => { describe('Data type recognition', () => {
...@@ -20,13 +22,16 @@ describe('Data type recognition', () => { ...@@ -20,13 +22,16 @@ describe('Data type recognition', () => {
it('should recognize all supported data types', async () => { it('should recognize all supported data types', async () => {
const results = await test.runJsonAnalysis({collection:'users'}, true); const results = await test.runJsonAnalysis({collection:'users'}, true);
results.validateResultsCount(10);
results.validate('_id', 1, 100.0, {ObjectId: 1}); results.validate('_id', 1, 100.0, {ObjectId: 1});
results.validate('key_string', 1, 100.0, {String: 1}); results.validate('key_string', 1, 100.0, {String: 1});
results.validate('key_boolean', 1, 100.0, {Boolean: 1});
results.validate('key_number', 1, 100.0, {Number: 1}); results.validate('key_number', 1, 100.0, {Number: 1});
results.validate('key_date', 1, 100.0, {Date: 1}); results.validate('key_date', 1, 100.0, {Date: 1});
results.validate('key_binData-generic', 1, 100.0, {'BinData-generic': 1}); results.validate('key_binData-generic', 1, 100.0, {'BinData-generic': 1});
results.validate('key_array', 1, 100.0, {Array: 1}); results.validate('key_array', 1, 100.0, {Array: 1});
results.validate('key_object', 1, 100.0, {Object: 1}); results.validate('key_object', 1, 100.0, {Object: 1});
results.validate('key_null', 1, 100.0, {null: 1}); // TODO: why has 'null' first letter lowercase, unlike all other types? results.validate('key_null', 1, 100.0, {null: 1}); // TODO: why has 'null' first letter lowercase, unlike all other types?
results.validate('key_long', 1, 100.0, {NumberLong: 1});
}); });
}); });
...@@ -18,6 +18,6 @@ export default class JsonValidator { ...@@ -18,6 +18,6 @@ export default class JsonValidator {
} }
validateResultsCount(count) { validateResultsCount(count) {
equal(this.results.length, count, 'Total count of results does not match expected count.'); equal(this.results.length, count, `Total count of results does not match expected count. Known keys are: [${this.results.map(item => item._id.key).join(',')}].`);
} }
} }
...@@ -85,10 +85,10 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -85,10 +85,10 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
read('logKeysContinuously', false); read('logKeysContinuously', false);
read('excludeSubkeys', []); read('excludeSubkeys', []);
read('arrayEscape', 'XX'); read('arrayEscape', 'XX');
//Translate excludeSubkeys to set like object... using an object for compatibility... //Translate excludeSubkeys to set like object... using an object for compatibility...
config.excludeSubkeys = config.excludeSubkeys.reduce(function (result, item) { result[item+'.'] = true; return result; }, {}); config.excludeSubkeys = config.excludeSubkeys.reduce(function (result, item) { result[item+'.'] = true; return result; }, {});
return config; return config;
}; };
...@@ -154,6 +154,8 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -154,6 +154,8 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
return 'null'; return 'null';
} else if (thing instanceof Date) { } else if (thing instanceof Date) {
return 'Date'; return 'Date';
} else if(thing instanceof NumberLong) {
return 'NumberLong';
} else if (thing instanceof ObjectId) { } else if (thing instanceof ObjectId) {
return 'ObjectId'; return 'ObjectId';
} else if (thing instanceof BinData) { } else if (thing instanceof BinData) {
...@@ -183,12 +185,13 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -183,12 +185,13 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
var isObject = typeof v === 'object'; var isObject = typeof v === 'object';
var specialObject = v instanceof Date || var specialObject = v instanceof Date ||
v instanceof ObjectId || v instanceof ObjectId ||
v instanceof BinData; v instanceof BinData ||
v instanceof NumberLong;
return !specialObject && (isArray || isObject); return !specialObject && (isArray || isObject);
} }
var arrayRegex = new RegExp('\\.' + config.arrayEscape + '\\d+' + config.arrayEscape + '\\.', 'g'); var arrayRegex = new RegExp('\\.' + config.arrayEscape + '\\d+' + config.arrayEscape + '\\.', 'g');
function serialize(document, parentKey, maxDepth) { function serialize(document, parentKey, maxDepth) {
if(Object.prototype.hasOwnProperty.call(excludeSubkeys, parentKey.replace(arrayRegex, '.'))) if(Object.prototype.hasOwnProperty.call(excludeSubkeys, parentKey.replace(arrayRegex, '.')))
return; return;
...@@ -199,7 +202,7 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -199,7 +202,7 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
} }
var value = document[key]; var value = document[key];
if(Array.isArray(document)) if(Array.isArray(document))
key = config.arrayEscape + key + config.arrayEscape; //translate unnamed object key from {_parent_name_}.{_index_} to {_parent_name_}.arrayEscape{_index_}arrayEscape. key = config.arrayEscape + key + config.arrayEscape; //translate unnamed object key from {_parent_name_}.{_index_} to {_parent_name_}.arrayEscape{_index_}arrayEscape.
result[parentKey+key] = value; result[parentKey+key] = value;
//it's an object, recurse...only if we haven't reached max depth //it's an object, recurse...only if we haven't reached max depth
if(isHash(value) && maxDepth > 1) { if(isHash(value) && maxDepth > 1) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment