Skip to content

Commit

Permalink
Add flexibility some fields are added/deleted from/to end
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Jul 3, 2018
1 parent 92e465c commit f49f0f9
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 9 deletions.
20 changes: 19 additions & 1 deletion src/mapType.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var DataType = require("./common").DataType;
var chars = require("./common").chars;
var startsWithNimnChar = require("./common").startsWithNimnChar;


function MapType(fName){
this._name = fName;
this._type = DataType.MAP;
Expand Down Expand Up @@ -61,6 +60,9 @@ MapType.prototype._decode = function(v,i){
currentObj[ this._keys[ key_i ]._name ] = keyVal.value;
}
}
if(v[i] !== chars.objEnd){
i = skipUntilThisObjectEnds(v, i);
}
return {
index : i+1,
value: currentObj
Expand All @@ -70,4 +72,20 @@ MapType.prototype._decode = function(v,i){
}
};


var skipUntilThisObjectEnds = function(str, from,to){
to = to || str.length;
var count = 0;
for(;from < to; from++){
if(chars.objStart === str[ from ]) {
count ++;
}else if( chars.objEnd === str[ from ] && str[ from -1 ] !== "\\" ){
if(count === 0) return from;
else count --;
}
}
return from;
}


module.exports = MapType;
8 changes: 0 additions & 8 deletions src/nimn.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@

var char = require("./common").char;
var inRange = require("./common").inRange;
var startsWithNimnChar = require("./common").startsWithNimnChar;
var read = require("./common").read;
var sanitize = require("./common").sanitize;
var removeBackspace = require("./common").removeBackspace;

/* var decodeNimnChar = {};
decodeNimnChar[char(175)] = null;
decodeNimnChar[char(184)] = undefined;
Expand Down
218 changes: 218 additions & 0 deletions tests/flexibility_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
var parser = require("../src/nimn");
var chars = require("../src/common").chars;

describe("Nimn Decoder", function () {

it("should decode if extra fields are present in last", function () {

var schema = {
type : "list",
detail : {
type : "map",
detail : [{
name : "name",
type : "string"
},{
name : "age",
type : "number"
},{
name : "isHuman",
type : "boolean"
},{
name : "address",
type : "string"
},{
name : "hobbies",
type : "list",
detail : {
type : "string"
}
},{
name : "project",
type : "map",
detail: [{
name: "title",
type : "string"
},{
name: "description",
type : "string"
},{
name: "status",
type : "string"
}
]
}
]
}
}

var newSchema = parser.buildSchema(schema);

var jData = [{
"name" : "somename",
"isHuman" : true,
"age": 32,
"address" : "I'll not tell you",
hobbies : [
null
, "not reading "+ chars.missingPremitive +" book"
, "watching \\"+ chars.nilPremitive +" movie"
],
project : {
title : "nimn",
//description : "it is 80% smaller",
status : "rocking"
}
},{
},{
"name" : "somename",
"isHuman" : null,
"address" : "I'll not tell you",
hobbies : null,
project : null
}
];

var dataWithExtraFields = chars.arrStart
+ chars.objStart
+ "somename" + chars.boundaryChar
+ "32"
+ chars.yes // Order of the keys should be maintained as per schema not the data
+ "I'll not tell you"
+ chars.arrStart
+ chars.nilPremitive
+ "not reading \\"+ chars.missingPremitive +" book" + chars.boundaryChar
+ "watching \\\\"+ chars.nilPremitive +" movie"
+ chars.arrEnd
+ chars.objStart
+ "nimn" //No boundary char if next field is nimn char
+ chars.missingPremitive
+ "rocking" //last field can not have boundary char
+ chars.objEnd
+ "I'm extra data"
+ chars.objEnd
+ chars.emptyChar
+ chars.objStart
+ "somename"
+ chars.missingPremitive
+ chars.nilPremitive // Order of the keys should be maintained as per schema not the data
+ "I'll not tell you"
+ chars.nilChar
+ chars.nilChar
+ chars.objEnd
+ chars.arrEnd;

result = parser.parse(newSchema, dataWithExtraFields);
//console.log(JSON.stringify(result, null, 4));
expect(result).toEqual(jData );
});

it("should decode data encoded with different schema if extra fields are present in last", function () {

var oldStructure = { //has less fields : v1
type : "list",
detail : {
type : "map",
detail : [{
name : "name",
type : "string"
},{
name : "age",
type : "number"
},{
name : "isHuman",
type : "boolean"
},{
name : "address",
type : "string"
},{
name : "hobbies",
type : "list",
detail : {
type : "string"
}
}
]
}
}

var newStructure = {
type : "list",
detail : {
type : "map",
detail : [{
name : "name",
type : "string"
},{
name : "age",
type : "number"
},{
name : "isHuman",
type : "boolean"
},{
name : "address",
type : "string"
},{
name : "hobbies",
type : "list",
detail : {
type : "string"
}
},{
name : "project",
type : "map",
detail: [{
name: "title",
type : "string"
},{
name: "description",
type : "string"
},{
name: "status",
type : "string"
}
]
}
]
}
}

var oldSchema = parser.buildSchema(oldStructure);
var newSchema = parser.buildSchema(newStructure);

var jData = [{
"name" : "somename",
"isHuman" : true,
"age": 32,
"address" : "I'll not tell you",
hobbies : [
null
, "not reading "+ chars.missingPremitive +" book"
, "watching \\"+ chars.nilPremitive +" movie"
],
project : {
title : "nimn",
//description : "it is 80% smaller",
status : "rocking"
}
},{
},{
"name" : "somename",
"isHuman" : null,
"address" : "I'll not tell you",
hobbies : null,
project : null
}
];

var oldNimnData = parser.stringify(oldSchema, jData);
var newNimnData = parser.stringify(newSchema, jData);

parser.parse(oldSchema, oldNimnData);
parser.parse(newSchema, oldNimnData);

parser.parse(newSchema, newNimnData);
parser.parse(oldSchema, newNimnData);

});
});

0 comments on commit f49f0f9

Please sign in to comment.