Skip to content

Commit 0cec645

Browse files
authored
Fix elementMap serialization
Expose tokens in a similar way as the GLV.
1 parent fb7f3d6 commit 0cec645

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

lib/datastax/graph/index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,15 @@ export namespace graph {
7171
function asTimestamp(value: Date): object;
7272

7373
function asUdt(value: object): object;
74+
75+
interface EnumValue {
76+
toString(): string
77+
}
78+
79+
namespace t {
80+
const id: EnumValue;
81+
const key: EnumValue;
82+
const label: EnumValue;
83+
const value: EnumValue;
84+
}
7485
}

lib/datastax/graph/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ const getCustomTypeSerializers = require('./custom-type-serializers');
2525
const { asInt, asDouble, asFloat, asTimestamp, asUdt, UdtGraphWrapper, GraphTypeWrapper} = require('./wrappers');
2626
const { Edge, Element, Path, Property, Vertex, VertexProperty } = require('./structure');
2727

28+
class EnumValue {
29+
constructor(typeName, elementName) {
30+
this.typeName = typeName;
31+
this.elementName = elementName;
32+
}
33+
34+
toString() {
35+
return this.elementName;
36+
}
37+
}
38+
39+
/**
40+
* Represents a collection of tokens for more concise Traversal definitions.
41+
*/
42+
const t = {
43+
id: new EnumValue('T', 'id'),
44+
key: new EnumValue('T', 'key'),
45+
label: new EnumValue('T', 'label'),
46+
value: new EnumValue('T', 'value'),
47+
};
48+
2849
module.exports = {
2950
Edge,
3051
Element,
@@ -41,5 +62,6 @@ module.exports = {
4162
getCustomTypeSerializers,
4263
GraphResultSet,
4364
GraphTypeWrapper,
65+
t,
4466
UdtGraphWrapper
4567
};

lib/datastax/graph/type-serializers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
// Replace dependencies to minimize code changes from Apache TinkerPop
3131
const t = {
3232
P: UnsupportedType, TextP: UnsupportedType, Traversal: UnsupportedType, Traverser: UnsupportedType,
33-
EnumValue: UnsupportedType, t: {}, direction: {}
33+
EnumValue: UnsupportedType, direction: {}
3434
};
3535
const ts = { TraversalStrategy: UnsupportedType };
3636
const Bytecode = UnsupportedType;
3737
const g = require('./index');
3838
const utils = { Long: UnsupportedType };
39+
t.t = g.t;
3940

4041
function UnsupportedType() { }
4142

test/integration/short/graph/graph-tests.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const { InetAddress, Uuid, Tuple } = types;
2828
const ExecutionProfile = require('../../../../lib/execution-profile').ExecutionProfile;
2929
const utils = require('../../../../lib/utils');
3030
const graphModule = require('../../../../lib/datastax/graph');
31-
const { asInt, asFloat, asUdt } = graphModule;
31+
const { asInt, asFloat, asUdt, t } = graphModule;
3232
const { graphProtocol } = require('../../../../lib/datastax/graph/options');
3333
const graphTestHelper = require('./graph-test-helper');
3434

@@ -948,6 +948,20 @@ vdescribe('dse-5.0', 'Client @SERVER_API', function () {
948948
assert.deepEqual(rs.toArray(), [value]);
949949
});
950950

951+
it('should support elementMap() tokens', async () => {
952+
const id = schemaCounter++;
953+
const insertTraversal = `g.addV('label_test').property('id', ${id}).property('prop_text', 'sample')`;
954+
await client.executeGraph(insertTraversal, null, { graphName });
955+
956+
const traversalText = `g.V().has('label_test', 'id', ${id}).elementMap()`;
957+
const rs = await client.executeGraph(traversalText, null, { graphName });
958+
const map = rs.first();
959+
assert.strictEqual(map.get('id'), id);
960+
assert.strictEqual(map.get('prop_text'), 'sample');
961+
assert.strictEqual(typeof map.get(t.id), 'string');
962+
assert.strictEqual(map.get(t.label), 'label_test');
963+
});
964+
951965
context('with complex types', () => {
952966
before(() => {
953967
const createSchemaQuery = `

test/unit/api-tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ describe('API', function () {
7474
assert.isObject(api.datastax.graph.getCustomTypeSerializers()['dse:Tuple']);
7575
});
7676

77+
it('should expose graph tokens', () => {
78+
[
79+
'id',
80+
'key',
81+
'label',
82+
'value'
83+
].forEach(name => {
84+
assert.isObject(api.datastax.graph.t[name]);
85+
assert.equal(api.datastax.graph.t[name].toString(), name);
86+
});
87+
88+
checkConstructor(api.datastax.graph, 'UdtGraphWrapper');
89+
checkConstructor(api.datastax.graph, 'GraphTypeWrapper');
90+
});
91+
7792
it('should expose cassandra driver modules', function () {
7893
assert.ok(api.errors);
7994
assert.ok(api.policies);

test/unit/typescript/graph-tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ async function myTest(client: Client): Promise<any> {
3535
await client.executeGraph('g.V()', cb);
3636
await client.executeGraph('g.V(id)', { id: 1}, cb);
3737
await client.executeGraph('g.V()', {}, { executionProfile: 'ep1' }, cb);
38+
39+
let tokenString: string;
40+
tokenString = datastax.graph.t.id.toString();
41+
tokenString = datastax.graph.t.label.toString();
3842
}

0 commit comments

Comments
 (0)