Skip to content

Commit

Permalink
Added serialization for decimal values
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jun 29, 2018
1 parent d2527db commit 1461f57
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
17 changes: 17 additions & 0 deletions lib/client/network/protocol37/operation.js
Expand Up @@ -178,6 +178,23 @@ Operation.prototype.writeInt = function(data) {
return this;
};

/**
* Parse data to 4 bytes which represents integer value.
*
* @param {Mixed} data The data to write.
* @return {Buffer} The buffer containing the data.
*/
Operation.prototype.writeDouble = function(data) {
this.writeOps.push([
constants.BYTES_LONG,
function(buffer, offset) {
buffer.fill(0, offset, offset + constants.BYTES_LONG);
buffer.writeDoubleBE(data, offset);
}
]);
return this;
};

/**
* Parse data to 8 bytes which represents a long value.
*
Expand Down
39 changes: 19 additions & 20 deletions lib/client/network/protocol37/serializer-binary.js
@@ -1,11 +1,10 @@
/*jshint esversion: 6 */
"use strict";

const Operation = require('./operation');

const RID = require('../../../recordid');
const RawExpression = require('../../../db/statement').RawExpression;
const Operation = require("./operation");

const RID = require("../../../recordid");
const RawExpression = require("../../../db/statement").RawExpression;

function serializeDocument(doc, op) {
let operation = op || new Operation();
Expand All @@ -17,39 +16,43 @@ function serializeDocument(doc, op) {
exports.serializeDocument = serializeDocument;

function writeObject(operation, doc) {
let fields = Object.keys(doc).filter((f) => {
let fields = Object.keys(doc).filter(f => {
return f !== "@class" && f !== "@rid";
});
operation.writeVarint(fields.length);

fields.forEach((f) => {
fields.forEach(f => {
operation.writeVarintString(f);
writeValue(operation, doc[f]);
});
}

function writeValue(op, value) {

switch (typeof value) {
case 'string' :
case "string":
op.writeByte(7);
op.writeVarintString(value);
break;
case 'object':
case "object":
if (Array.isArray(value)) {
handleArray(op, value);
} else {
handleObject(op, value);
}
break;
case 'function':
case "function":
// TODO Handle Function?
break;
case 'number' :
op.writeByte(3);
op.writeVarint(value);
case "number":
if (Number.isInteger(value)) {
op.writeByte(3);
op.writeVarint(value);
}else {
op.writeByte(5);
op.writeDouble(value);
}
break;
case 'boolean' :
case "boolean":
op.writeByte(0);
op.writeBoolean(value);
break;
Expand All @@ -63,7 +66,6 @@ function writeLink(op, link) {
op.writeVarint(link.position);
}


function handleObject(op, value) {
if (value instanceof RawExpression) {
writeValue(op, value.toString());
Expand All @@ -84,24 +86,22 @@ function handleObject(op, value) {
}
}


function handleEmbeddedCollection(op, value) {
op.writeByte(10);
op.writeVarint(value.length);
value.forEach((val) => {
value.forEach(val => {
writeValue(op, val);
});
}

function handleLinkList(op, value) {
op.writeByte(14);
op.writeVarint(value.length);
value.forEach((val) => {
value.forEach(val => {
writeLink(op, val);
});
}
function handleArray(op, value) {

let handler = handleEmbeddedCollection;
if (value.length > 0) {
if (value[0] instanceof RID) {
Expand All @@ -110,4 +110,3 @@ function handleArray(op, value) {
}
handler(op, value);
}

0 comments on commit 1461f57

Please sign in to comment.