Skip to content

Commit

Permalink
support for nulls, infinity and guid
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwittig committed Aug 29, 2015
1 parent 104bdb4 commit c58528c
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 32 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ con.close(function() {
});
```

## Types

Q has many more [data types](http://code.kx.com/wiki/Reference/Datatypes) than JavaScript. Therefore you need to know how types are converted.

### From q to JavaScript (deserialization)

| q type | JavaScript type | Null | +Infinity | -Infinity |
| ------ | --------------- | ---- | --------- | --------- |
| boolean | [Boolean](https://developer.mozilla.org/docs/Glossary/Boolean) | | | |
| guid | [String](https://developer.mozilla.org/docs/Glossary/String) | [Null](https://developer.mozilla.org/docs/Glossary/String) | | |
| byte | [Number](https://developer.mozilla.org/docs/Glossary/Number) | | | |
| short | [Number](https://developer.mozilla.org/docs/Glossary/Number) | [Null](https://developer.mozilla.org/docs/Glossary/String) | [Infinity](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Infinity) | -[Infinity](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Infinity) |
| int | [Number](https://developer.mozilla.org/docs/Glossary/Number) | [Null](https://developer.mozilla.org/docs/Glossary/String) | [Infinity](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Infinity) | -[Infinity](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Infinity) |
| long | [Number](https://developer.mozilla.org/docs/Glossary/Number) | [Null](https://developer.mozilla.org/docs/Glossary/String) | [Infinity](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Infinity) | -[Infinity](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Infinity) |

### From JavaScript to q (serialization)

TODO

## API

### connect(params, cb)
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Connection.prototype.auth = function(auth, cb) {
b = new Buffer(n + 2),
self = this;
b.write(auth, 0, n, "ascii"); // auth (username:password)
b.writeUInt8(0x1, n); // capability byte (compression, timestamp, timespan) http://code.kx.com/wiki/Reference/ipcprotocol#Handshake
b.writeUInt8(0x3, n); // capability byte (compression, timestamp, timespan) http://code.kx.com/wiki/Reference/ipcprotocol#Handshake
b.writeUInt8(0x0, n+1); // zero terminated
this.socket.write(b);
this.socket.once("data", function(buffer) {
Expand Down
129 changes: 109 additions & 20 deletions itest/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,22 @@ describe("types", function() {
});
});
});
/* TODO type describe("guid", function() {
});*/
describe("guid", function() {
it("null", function(done) {
con.k("0Ng", function(err, res) {
if (err) { throw err; }
assert.equal(res, null, "res");
done();
});
});
it("random", function(done) {
con.k("rand 0Ng", function(err, res) {
if (err) { throw err; }
assert.string(res, "res");
done();
});
});
});
describe("byte", function() {
it("255", function(done) {
con.k("0xff", function(err, res) {
Expand All @@ -61,13 +74,27 @@ describe("types", function() {
});
});
describe("short", function() {
/* TODO null it("null", function(done) {
it("null", function(done) {
con.k("0Nh", function(err, res) {
if (err) { throw err; }
assert.equal(res, null, "res");
done();
});
});*/
});
it("+infinity", function(done) {
con.k("0Wh", function(err, res) {
if (err) { throw err; }
assert.equal(res, Infinity, "res");
done();
});
});
it("-infinity", function(done) {
con.k("-0Wh", function(err, res) {
if (err) { throw err; }
assert.equal(res, -Infinity, "res");
done();
});
});
it("1", function(done) {
con.k("1h", function(err, res) {
if (err) { throw err; }
Expand All @@ -91,13 +118,27 @@ describe("types", function() {
});
});
describe("int", function() {
/* TODO null it("null", function(done) {
it("null", function(done) {
con.k("0Ni", function(err, res) {
if (err) { throw err; }
assert.equal(res, null, "res");
done();
});
});*/
});
it("+infinity", function(done) {
con.k("0Wi", function(err, res) {
if (err) { throw err; }
assert.equal(res, Infinity, "res");
done();
});
});
it("-infinity", function(done) {
con.k("-0Wi", function(err, res) {
if (err) { throw err; }
assert.equal(res, -Infinity, "res");
done();
});
});
it("1", function(done) {
con.k("1i", function(err, res) {
if (err) { throw err; }
Expand All @@ -121,13 +162,27 @@ describe("types", function() {
});
});
describe("long", function() {
/* TODO null it("null", function(done) {
it("null", function(done) {
con.k("0Nj", function(err, res) {
if (err) { throw err; }
assert.equal(res, null, "res");
done();
});
});*/
});
it("+infinity", function(done) {
con.k("0Wj", function(err, res) {
if (err) { throw err; }
assert.equal(res, Infinity, "res");
done();
});
});
it("-infinity", function(done) {
con.k("-0Wj", function(err, res) {
if (err) { throw err; }
assert.equal(res, -Infinity, "res");
done();
});
});
it("1", function(done) {
con.k("1j", function(err, res) {
if (err) { throw err; }
Expand All @@ -151,13 +206,27 @@ describe("types", function() {
});
});
describe("real", function() {
/* TODO null it("null", function(done) {
it("null", function(done) {
con.k("0Ne", function(err, res) {
if (err) { throw err; }
assert.equal(res, null, "res");
done();
});
});*/
});
it("+infinity", function(done) {
con.k("0We", function(err, res) {
if (err) { throw err; }
assert.equal(res, Infinity, "res");
done();
});
});
it("-infinity", function(done) {
con.k("-0We", function(err, res) {
if (err) { throw err; }
assert.equal(res, -Infinity, "res");
done();
});
});
it("1", function(done) {
con.k("1.0e", function(err, res) {
if (err) { throw err; }
Expand All @@ -181,13 +250,27 @@ describe("types", function() {
});
});
describe("float", function() {
/* TODO null it("null", function(done) {
it("null", function(done) {
con.k("0Nf", function(err, res) {
if (err) { throw err; }
assert.equal(res, null, "res");
done();
});
});*/
});
it("+infinity", function(done) {
con.k("0Wf", function(err, res) {
if (err) { throw err; }
assert.equal(res, Infinity, "res");
done();
});
});
it("-infinity", function(done) {
con.k("-0Wf", function(err, res) {
if (err) { throw err; }
assert.equal(res, -Infinity, "res");
done();
});
});
it("1", function(done) {
con.k("1f", function(err, res) {
if (err) { throw err; }
Expand All @@ -211,13 +294,13 @@ describe("types", function() {
});
});
describe("char", function() {
/* TODO null it("null", function(done) {
it("null", function(done) {
con.k('" "', function(err, res) {
if (err) { throw err; }
assert.equal(res, null, "res");
done();
});
});*/
});
it("a", function(done) {
con.k('"a"', function(err, res) {
if (err) { throw err; }
Expand All @@ -226,23 +309,29 @@ describe("types", function() {
});
});
});
/* TODO type describe("symbol", function() {
describe("symbol", function() {
it("null", function(done) {
con.k("`", function(err, res) {
con.k("s: `; s", function(err, res) {
if (err) { throw err; }
assert.equal(res, null, "res");
done();
});
});
it("`a", function(done) {
con.k("`a", function(err, res) {
console.log("res", [err, res]);
con.k("s: `a; s", function(err, res) {
if (err) { throw err; }
assert.equal(res, "a", "res");
done();
});
});
});*/
it("`abc", function(done) {
con.k("s: `abc; s", function(err, res) {
if (err) { throw err; }
assert.equal(res, "abc", "res");
done();
});
});
});
describe("timestamp", function() {

});
Expand Down
68 changes: 57 additions & 11 deletions lib/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ function deserialize(b, nanos2date) {
return rInt8() === 1;
}
function rChar() {
return String.fromCharCode(rInt8());
var val = rInt8();
if (val === 32) {
return null;
} else {
return String.fromCharCode(val);
}
}
function rInt8() {
var val = b.readInt8(pos);
Expand All @@ -86,43 +91,84 @@ function deserialize(b, nanos2date) {
return val;
}
function rGuid() {
var x = "0123456789abcdef",
s = "";
var x = "0123456789abcdef", s = "";
for (var i = 0; i < 16; i++) {
var c = rUInt8();
s += i == 4 || i === 6 || i === 8 || i === 10 ? "-" : "";
s += x[c >> 4];
s += x[c & 15];
}
return s;
if (s === "00000000-0000-0000-0000-000000000000") {
return null;
} else {
return s;
}
}
function rInt16() {
var h = rInt(2);
return h === -32768 ? NaN : h === -32767 ? -Infinity : h === 32767 ? Infinity : h;
if (h === -32768) {
return null;
} else if (h === 32767) {
return Infinity;
} else if (h === -32767) {
return -Infinity;
} else {
return h;
}
}
function rInt32() {
var i = rInt(4);
return i === -2147483648 ? NaN : i === -2147483647 ? -Infinity : i === 2147483647 ? Infinity : i;
if (i === -2147483648) {
return null;
} else if (i === 2147483647) {
return Infinity;
} else if (i === -2147483647) {
return -Infinity;
} else {
return i;
}
}
function rInt64() { // closest number to 64 bit int...
var y = rInt(4);
var x = rInt(4);
return x * j2p32 + (y >= 0 ? y : j2p32 + y);
if (x === -2147483648 && y === 0) {
return null;
} else if (x === 2147483647 && y === -1) {
return Infinity;
} else if (x === -2147483648 && y === 1) {
return -Infinity;
} else {
return x * j2p32 + (y >= 0 ? y : j2p32 + y);
}
}
function rFloat32() {
var val = b.readFloatLE(pos);
pos += 4;
return val;
if (Number.isNaN(val)) {
return null;
} else {
return val;
}
}
function rFloat64() {
var val = b.readDoubleLE(pos);
pos += 8;
return val;
if (Number.isNaN(val)) {
return null;
} else {
return val;
}
}
function rSymbol() {
var i = pos, c, s = "";
for (; (c = rInt8()) !== 0; s += String.fromCharCode(c));
return s;
while ((c = rInt8()) !== 0) {
s += String.fromCharCode(c);
}
if (s === "") {
return null;
} else {
return s;
}
}
function rTimestamp() {
if (nanos2date === false) {
Expand Down

0 comments on commit c58528c

Please sign in to comment.