From f30158f7c4b1d030b50b9b6a3d0732bcc335206d Mon Sep 17 00:00:00 2001 From: brianc Date: Thu, 7 Mar 2013 09:54:01 -0600 Subject: [PATCH] deprecate float parsing - closes #296 --- lib/types/binaryParsers.js | 8 ++++++++ lib/types/textParsers.js | 31 +++++++++++++++++++++---------- test/test-helper.js | 2 ++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/lib/types/binaryParsers.js b/lib/types/binaryParsers.js index 7f0a89c6..b1bfdd3a 100644 --- a/lib/types/binaryParsers.js +++ b/lib/types/binaryParsers.js @@ -1,3 +1,5 @@ +var deprecate = require('deprecate'); + var parseBits = function(data, bits, offset, invert, callback) { offset = offset || 0; invert = invert || false; @@ -45,6 +47,12 @@ var parseBits = function(data, bits, offset, invert, callback) { }; var parseFloatFromBits = function(data, precisionBits, exponentBits) { + deprecate('parsing and returning floats from PostgreSQL server is deprecated', + 'JavaScript has a hard time with floats and there is precision loss which can cause', + 'unexpected, hard to trace, potentially bad bugs in your program', + 'for more information see the following:', + 'https://github.com/brianc/node-postgres/pull/271', + 'in node-postgres v1.0.0 all floats & decimals will be returned as strings'); var bias = Math.pow(2, exponentBits - 1) - 1; var sign = parseBits(data, 1); var exponent = parseBits(data, exponentBits, 1); diff --git a/lib/types/textParsers.js b/lib/types/textParsers.js index e5d2a747..00ceecb1 100644 --- a/lib/types/textParsers.js +++ b/lib/types/textParsers.js @@ -1,3 +1,5 @@ +var deprecate = require('deprecate'); + var arrayParser = require(__dirname + "/arrayParser.js"); //parses PostgreSQL server formatted date strings into javascript date objects @@ -76,6 +78,12 @@ var parseIntegerArray = function(val) { }; var parseFloatArray = function(val) { + deprecate('parsing and returning floats from PostgreSQL server is deprecated', + 'JavaScript has a hard time with floats and there is precision loss which can cause', + 'unexpected, hard to trace, potentially bad bugs in your program', + 'for more information see the following:', + 'https://github.com/brianc/node-postgres/pull/271', + 'in node-postgres v1.0.0 all floats & decimals will be returned as strings'); if(!val) { return null; } var p = arrayParser.create(val, function(entry){ if(entry !== null) { @@ -162,24 +170,27 @@ var parseInteger = function(val) { return parseInt(val, 10); }; +var parseFloatAndWarn = function(val) { + deprecate('parsing and returning floats from PostgreSQL server is deprecated', + 'JavaScript has a hard time with floats and there is precision loss which can cause', + 'unexpected, hard to trace, potentially bad bugs in your program', + 'for more information see the following:', + 'https://github.com/brianc/node-postgres/pull/271', + 'in node-postgres v1.0.0 all floats & decimals will be returned as strings'); + return parseFloat(val); +}; + var init = function(register) { register(20, parseInteger); register(21, parseInteger); register(23, parseInteger); register(26, parseInteger); //TODO remove for v1.0 - register(1700, function(val){ - if(val.length > maxLen) { - console.warn( - 'WARNING: value %s is longer than max supported numeric value in ' + - 'javascript. Possible data loss', val); - } - return parseFloat(val); - }); + register(1700, parseFloatAndWarn); //TODO remove for v1.0 - register(700, parseFloat); + register(700, parseFloatAndWarn); //TODO remove for v1.0 - register(701, parseFloat); + register(701, parseFloatAndWarn); register(16, parseBool); register(1082, parseDate); // date register(1114, parseDate); // timestamp without timezone diff --git a/test/test-helper.js b/test/test-helper.js index 4ad7b7b0..398bc861 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -7,6 +7,8 @@ var BufferList = require(__dirname+'/buffer-list') var Connection = require(__dirname + '/../lib/connection'); +require(__dirname + '/../lib').defaults.hideDeprecationWarnings = true; + Client = require(__dirname + '/../lib').Client; process.on('uncaughtException', function(d) {