Skip to content

Commit

Permalink
parse map data type
Browse files Browse the repository at this point in the history
  • Loading branch information
swilly22 committed Jan 22, 2021
1 parent 5a2d92f commit ab1da70
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/resultSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const ResultSetValueTypes = {
VALUE_EDGE: 7,
VALUE_NODE: 8,
VALUE_PATH: 9,
VALUE_MAP: 10,
};

/**
Expand Down Expand Up @@ -248,6 +249,22 @@ class ResultSet {
return new Path(nodes, edges);
}

/**
* Parse a raw map representation into Map object.
* @async
* @param {object[]} rawMap raw map representation
* @returns {Map} Map object.
*/
async parseMap(rawMap) {
let m = {};
for (var i = 0; i < rawMap.length; i+=2) {
var key = rawMap[i];
m[key] = await this.parseScalar(rawMap[i+1]);
}

return m;
}

/**
* Parse a raw value into its actual value.
* @async
Expand Down Expand Up @@ -291,6 +308,11 @@ class ResultSet {
case ResultSetValueTypes.VALUE_PATH:
scalar = await this.parsePath(value);
break;

case ResultSetValueTypes.VALUE_MAP:
scalar = await this.parseMap(value);
break;

case ResultSetValueTypes.VALUE_UNKNOWN:
console.log("Unknown scalar type\n");
break;
Expand Down
29 changes: 29 additions & 0 deletions test/redisGraphAPITest.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,35 @@ describe("RedisGraphAPI Test", () => {
assert.deepStrictEqual([nodeA, nodeB], record.get(0));
});

it("test map", async () => {
// return empty map
let resultSet = await api.query("RETURN {}");
assert.equal(resultSet.size(), 1);
assert.ok(resultSet.hasNext());

let record = resultSet.next();
assert.deepStrictEqual({}, record.get(0));
assert.ok(!resultSet.hasNext());

// return map with multiple types
await api.query("CREATE (:person{v:1})-[:R{v:2}]->(:person{v:3})")
resultSet = await api.query("MATCH (a)-[e]->(b) RETURN {a:'a', b:1, c:null, d:true, e:[1,2], f:{x:1}, src:a, edge:e, dest:b}");
assert.equal(resultSet.size(), 1);
assert.ok(resultSet.hasNext());

let src = new Node("person", { v: 1 });
src.setId(0);
let dest = new Node("person", { v: 3 });
dest.setId(1);
let edge = new Edge(0, "R", 1, {v: 2});
edge.setId(0);

let expected = {a:'a', b:1, c:null, d:true, e:[1,2], f:{x:1}, src:src, edge:edge, dest:dest};
record = resultSet.next();
assert.deepStrictEqual(expected, record.get(0));
assert.ok(!resultSet.hasNext());
});

it("test multi thread", async () => {
await api.query("CREATE (:person {name:'roi', age:34})");
var promises = [];
Expand Down

0 comments on commit ab1da70

Please sign in to comment.