Skip to content

parse map data type #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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