Skip to content

Commit c519a47

Browse files
committed
Add support for sparse javascript objects
For objects, silently drop undefined fields. For arrays, convert undefined entries to null. (Supporting true undefined values in arrays would require protocol changes, so null seems good enough for now) Related to neo4j#91
1 parent bdd39fb commit c519a47

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/v1/internal/packstream.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,27 @@ class Packer {
9696
} else if (x instanceof Array) {
9797
this.packListHeader(x.length);
9898
for(let i = 0; i < x.length; i++) {
99-
this.pack(x[i]);
99+
this.pack(x[i] === undefined ? null : x[i]);
100100
}
101101
} else if (x instanceof Structure) {
102102
this.packStruct( x.signature, x.fields );
103103
} else if (typeof(x) == "object") {
104104
let keys = Object.keys(x);
105-
this.packMapHeader(keys.length);
105+
106+
let count = 0;
107+
for(let i = 0; i < keys.length; i++) {
108+
if (x[keys[i]] !== undefined) {
109+
count++;
110+
}
111+
}
112+
113+
this.packMapHeader(count);
106114
for(let i = 0; i < keys.length; i++) {
107115
let key = keys[i];
108-
this.packString(key);
109-
this.pack(x[key]);
116+
if (x[key] !== undefined) {
117+
this.packString(key);
118+
this.pack(x[key]);
119+
}
110120
}
111121
} else {
112122
throw newError("Cannot pack this value: " + x);

test/v1/types.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe('string values', function() {
4949

5050
describe('list values', function() {
5151
it('should support empty lists ', testVal( [] ) );
52+
it('should support sparse lists ', testVal( [ undefined, 4 ], [ null, 4 ] ) );
5253
it('should support float lists ', testVal( [ 1,2,3 ] ) );
5354
it('should support boolean lists ', testVal( [ true, false ] ) );
5455
it('should support string lists ', testVal( [ "", "hello!" ] ) );
@@ -59,6 +60,7 @@ describe('list values', function() {
5960
describe('map values', function() {
6061
it('should support empty maps ', testVal( {} ) );
6162
it('should support basic maps ', testVal( {a:1, b:{}, c:[], d:{e:1}} ) );
63+
it('should support sparse maps ', testVal( {foo: undefined, bar: null}, {bar: null} ) );
6264
});
6365

6466
describe('node values', function() {
@@ -131,14 +133,14 @@ describe('path values', function() {
131133
});
132134
});
133135

134-
function testVal( val ) {
136+
function testVal( val, expected ) {
135137
return function( done ) {
136138
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
137139
var session = driver.session();
138140

139141
session.run("RETURN {val} as v", {val: val})
140142
.then( function( result ) {
141-
expect( result.records[0].get('v') ).toEqual( val );
143+
expect( result.records[0].get('v') ).toEqual( expected || val );
142144
driver.close();
143145
done();
144146
}).catch(function(err) { console.log(err); });

0 commit comments

Comments
 (0)