This is an automated email from the ASF dual-hosted git repository. jorgebg pushed a commit to branch TINKERPOP-2330 in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
commit 57ba497f7de1fee8945e9c92b011c27739423c0d Author: Jorge Bay Gondra <jorgebaygon...@gmail.com> AuthorDate: Thu Jan 23 14:54:48 2020 +0100 TINKERPOP-2330 JavaScript GLV: Export GraphSON2 and 3 writers/readers --- .../main/javascript/gremlin-javascript/index.js | 5 +- .../gremlin-javascript/lib/driver/connection.js | 29 +++++--- .../lib/structure/io/graph-serializer.js | 84 ++++++++++++++++++---- .../gremlin-javascript/test/unit/client-test.js | 4 +- .../gremlin-javascript/test/unit/exports-test.js | 6 ++ 5 files changed, 99 insertions(+), 29 deletions(-) diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/index.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/index.js index c8fe547..5ef0eca 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/index.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/index.js @@ -80,10 +80,7 @@ module.exports = { AnonymousTraversalSource }, structure: { - io: { - GraphSONReader: gs.GraphSONReader, - GraphSONWriter: gs.GraphSONWriter - }, + io: gs, Edge: graph.Edge, Graph: graph.Graph, Path: graph.Path, diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js index 692dbe1..29be5dc 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/connection.js @@ -38,6 +38,7 @@ const responseStatusCode = { }; const defaultMimeType = 'application/vnd.gremlin-v3.0+json'; +const graphSON2MimeType = 'application/vnd.gremlin-v2.0+json'; const pingIntervalDelay = 60 * 1000; const pongTimeoutDelay = 30 * 1000; @@ -73,10 +74,16 @@ class Connection extends EventEmitter { this.url = url; this.options = options = options || {}; + /** + * Gets the MIME type. + * @type {String} + */ + this.mimeType = options.mimeType || defaultMimeType; + // A map containing the request id and the handler this._responseHandlers = {}; - this._reader = options.reader || new serializer.GraphSONReader(); - this._writer = options.writer || new serializer.GraphSONWriter(); + this._reader = options.reader || this._getDefaultReader(this.mimeType); + this._writer = options.writer || this._getDefaultWriter(this.mimeType); this._openPromise = null; this._openCallback = null; this._closePromise = null; @@ -84,12 +91,6 @@ class Connection extends EventEmitter { this._pingInterval = null; this._pongTimeout = null; - /** - * Gets the MIME type. - * @type {String} - */ - this.mimeType = options.mimeType || defaultMimeType; - this._header = String.fromCharCode(this.mimeType.length) + this.mimeType; this.isOpen = false; this.traversalSource = options.traversalSource || 'g'; @@ -169,6 +170,18 @@ class Connection extends EventEmitter { })); } + _getDefaultReader(mimeType) { + return mimeType === graphSON2MimeType + ? new serializer.GraphSON2Reader() + : new serializer.GraphSONReader(); + } + + _getDefaultWriter(mimeType) { + return mimeType === graphSON2MimeType + ? new serializer.GraphSON2Writer() + : new serializer.GraphSONWriter(); + } + _getRequest(id, bytecode, op, args, processor) { if (args) { args = this._adaptArgs(args, true); diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js index 4a7fdf2..fb461d9 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js @@ -25,24 +25,27 @@ const typeSerializers = require('./type-serializers'); /** - * GraphSON Writer + * GraphSON2 writer. */ -class GraphSONWriter { +class GraphSON2Writer { + /** * @param {Object} [options] - * @param {Object} options.serializers An object used as an associative array with GraphSON 2 type name as keys and + * @param {Object} [options.serializers] An object used as an associative array with GraphSON 2 type name as keys and * serializer instances as values, ie: { 'g:Int64': longSerializer }. * @constructor */ constructor(options) { this._options = options || {}; // Create instance of the default serializers - this._serializers = serializers.map(serializerConstructor => { + this._serializers = this.getDefaultSerializers().map(serializerConstructor => { const s = new serializerConstructor(); s.writer = this; return s; }); + const customSerializers = this._options.serializers || {}; + Object.keys(customSerializers).forEach(key => { const s = customSerializers[key]; if (!s.serialize) { @@ -54,6 +57,14 @@ class GraphSONWriter { }); } + /** + * Gets the default serializers to be used. + * @returns {Array} + */ + getDefaultSerializers() { + return graphSON2Serializers; + } + adaptObject(value) { let s; @@ -89,7 +100,19 @@ class GraphSONWriter { } } -class GraphSONReader { +/** + * GraphSON3 writer. + */ +class GraphSON3Writer extends GraphSON2Writer { + getDefaultSerializers() { + return graphSON3Serializers; + } +} + +/** + * GraphSON2 reader. + */ +class GraphSON2Reader { /** * GraphSON Reader * @param {Object} [options] @@ -100,12 +123,15 @@ class GraphSONReader { constructor(options) { this._options = options || {}; this._deserializers = {}; - Object.keys(deserializers).forEach(typeName => { - const serializerConstructor = deserializers[typeName]; + + const defaultDeserializers = this.getDefaultDeserializers(); + Object.keys(defaultDeserializers).forEach(typeName => { + const serializerConstructor = defaultDeserializers[typeName]; const s = new serializerConstructor(); s.reader = this; this._deserializers[typeName] = s; }); + if (this._options.serializers) { const customSerializers = this._options.serializers || {}; Object.keys(customSerializers).forEach(key => { @@ -119,6 +145,14 @@ class GraphSONReader { } } + /** + * Gets the default deserializers as an associative array. + * @returns {Object} + */ + getDefaultDeserializers() { + return graphSON2Deserializers; + } + read(obj) { if (obj === undefined) { return undefined; @@ -155,7 +189,16 @@ class GraphSONReader { } } -const deserializers = { +/** + * GraphSON3 reader. + */ +class GraphSON3Reader extends GraphSON2Reader { + getDefaultDeserializers() { + return graphSON3Deserializers; + } +} + +const graphSON2Deserializers = { 'g:Traverser': typeSerializers.TraverserSerializer, 'g:TraversalStrategy': typeSerializers.TraversalStrategySerializer, 'g:Int32': typeSerializers.NumberSerializer, @@ -168,13 +211,16 @@ const deserializers = { 'g:VertexProperty': typeSerializers.VertexPropertySerializer, 'g:Property': typeSerializers.PropertySerializer, 'g:Path': typeSerializers.Path3Serializer, - 'g:T': typeSerializers.TSerializer, + 'g:T': typeSerializers.TSerializer +}; + +const graphSON3Deserializers = Object.assign({}, graphSON2Deserializers, { 'g:List': typeSerializers.ListSerializer, 'g:Set': typeSerializers.SetSerializer, 'g:Map': typeSerializers.MapSerializer -}; +}); -const serializers = [ +const graphSON2Serializers = [ typeSerializers.NumberSerializer, typeSerializers.DateSerializer, typeSerializers.BytecodeSerializer, @@ -185,12 +231,20 @@ const serializers = [ typeSerializers.EnumSerializer, typeSerializers.VertexSerializer, typeSerializers.EdgeSerializer, - typeSerializers.LongSerializer, + typeSerializers.LongSerializer +]; + +const graphSON3Serializers = graphSON2Serializers.concat([ typeSerializers.ListSerializer, + typeSerializers.SetSerializer, typeSerializers.MapSerializer -]; +]); module.exports = { - GraphSONWriter, - GraphSONReader + GraphSON3Writer, + GraphSON3Reader, + GraphSON2Writer, + GraphSON2Reader, + GraphSONWriter: GraphSON3Writer, + GraphSONReader: GraphSON3Reader }; \ No newline at end of file diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/client-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/client-test.js index 360e3f0..b651e37 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/client-test.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/client-test.js @@ -36,7 +36,7 @@ describe('Client', function () { } }; - const customClient = new Client('localhost:8182', {traversalSource: 'g'}); + const customClient = new Client('ws://localhost:8182', {traversalSource: 'g'}); customClient._connection = connectionMock; customClient.submit(query) }); @@ -51,7 +51,7 @@ describe('Client', function () { } }; - const customClient = new Client('localhost:8182', {traversalSource: 'g', processor: customOpProcessor}); + const customClient = new Client('ws://localhost:8182', {traversalSource: 'g', processor: customOpProcessor}); customClient._connection = connectionMock; customClient.submit(query) }); diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/exports-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/exports-test.js index be46e93..4273604 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/exports-test.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/exports-test.js @@ -59,6 +59,12 @@ describe('API', function () { assert.ok(glvModule.structure.io); assert.strictEqual(typeof glvModule.structure.io.GraphSONReader, 'function'); assert.strictEqual(typeof glvModule.structure.io.GraphSONWriter, 'function'); + validateConstructor(glvModule.structure.io, 'GraphSON2Reader'); + validateConstructor(glvModule.structure.io, 'GraphSON2Writer'); + validateConstructor(glvModule.structure.io, 'GraphSON3Reader'); + validateConstructor(glvModule.structure.io, 'GraphSON3Writer'); + assert.strictEqual(glvModule.structure.io.GraphSONReader, glvModule.structure.io.GraphSON3Reader); + assert.strictEqual(glvModule.structure.io.GraphSONWriter, glvModule.structure.io.GraphSON3Writer); assert.strictEqual(typeof glvModule.structure.Edge, 'function'); assert.strictEqual(typeof glvModule.structure.Graph, 'function'); assert.strictEqual(typeof glvModule.structure.Path, 'function');