Skip to content

Commit f30158f

Browse files
committed
deprecate float parsing - closes brianc#296
1 parent 6415450 commit f30158f

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

lib/types/binaryParsers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var deprecate = require('deprecate');
2+
13
var parseBits = function(data, bits, offset, invert, callback) {
24
offset = offset || 0;
35
invert = invert || false;
@@ -45,6 +47,12 @@ var parseBits = function(data, bits, offset, invert, callback) {
4547
};
4648

4749
var parseFloatFromBits = function(data, precisionBits, exponentBits) {
50+
deprecate('parsing and returning floats from PostgreSQL server is deprecated',
51+
'JavaScript has a hard time with floats and there is precision loss which can cause',
52+
'unexpected, hard to trace, potentially bad bugs in your program',
53+
'for more information see the following:',
54+
'https://github.com/brianc/node-postgres/pull/271',
55+
'in node-postgres v1.0.0 all floats & decimals will be returned as strings');
4856
var bias = Math.pow(2, exponentBits - 1) - 1;
4957
var sign = parseBits(data, 1);
5058
var exponent = parseBits(data, exponentBits, 1);

lib/types/textParsers.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var deprecate = require('deprecate');
2+
13
var arrayParser = require(__dirname + "/arrayParser.js");
24

35
//parses PostgreSQL server formatted date strings into javascript date objects
@@ -76,6 +78,12 @@ var parseIntegerArray = function(val) {
7678
};
7779

7880
var parseFloatArray = function(val) {
81+
deprecate('parsing and returning floats from PostgreSQL server is deprecated',
82+
'JavaScript has a hard time with floats and there is precision loss which can cause',
83+
'unexpected, hard to trace, potentially bad bugs in your program',
84+
'for more information see the following:',
85+
'https://github.com/brianc/node-postgres/pull/271',
86+
'in node-postgres v1.0.0 all floats & decimals will be returned as strings');
7987
if(!val) { return null; }
8088
var p = arrayParser.create(val, function(entry){
8189
if(entry !== null) {
@@ -162,24 +170,27 @@ var parseInteger = function(val) {
162170
return parseInt(val, 10);
163171
};
164172

173+
var parseFloatAndWarn = function(val) {
174+
deprecate('parsing and returning floats from PostgreSQL server is deprecated',
175+
'JavaScript has a hard time with floats and there is precision loss which can cause',
176+
'unexpected, hard to trace, potentially bad bugs in your program',
177+
'for more information see the following:',
178+
'https://github.com/brianc/node-postgres/pull/271',
179+
'in node-postgres v1.0.0 all floats & decimals will be returned as strings');
180+
return parseFloat(val);
181+
};
182+
165183
var init = function(register) {
166184
register(20, parseInteger);
167185
register(21, parseInteger);
168186
register(23, parseInteger);
169187
register(26, parseInteger);
170188
//TODO remove for v1.0
171-
register(1700, function(val){
172-
if(val.length > maxLen) {
173-
console.warn(
174-
'WARNING: value %s is longer than max supported numeric value in ' +
175-
'javascript. Possible data loss', val);
176-
}
177-
return parseFloat(val);
178-
});
189+
register(1700, parseFloatAndWarn);
179190
//TODO remove for v1.0
180-
register(700, parseFloat);
191+
register(700, parseFloatAndWarn);
181192
//TODO remove for v1.0
182-
register(701, parseFloat);
193+
register(701, parseFloatAndWarn);
183194
register(16, parseBool);
184195
register(1082, parseDate); // date
185196
register(1114, parseDate); // timestamp without timezone

test/test-helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var BufferList = require(__dirname+'/buffer-list')
77

88
var Connection = require(__dirname + '/../lib/connection');
99

10+
require(__dirname + '/../lib').defaults.hideDeprecationWarnings = true;
11+
1012
Client = require(__dirname + '/../lib').Client;
1113

1214
process.on('uncaughtException', function(d) {

0 commit comments

Comments
 (0)