tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jorg...@apache.org
Subject [1/2] tinkerpop git commit: TINKERPOP-1943 Support GraphSON3 in Gremlin-JavaScript
Date Tue, 01 May 2018 18:24:48 GMT
Repository: tinkerpop
Updated Branches:
  refs/heads/tp33 7d8cb0694 -> 99845d4f1


TINKERPOP-1943 Support GraphSON3 in Gremlin-JavaScript


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/9357c9e5
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/9357c9e5
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/9357c9e5

Branch: refs/heads/tp33
Commit: 9357c9e567cea65522db2ca109f427c27a999c06
Parents: 8005cb3
Author: Jorge Bay Gondra <jorgebaygondra@gmail.com>
Authored: Wed Apr 25 14:22:25 2018 +0200
Committer: Jorge Bay Gondra <jorgebaygondra@gmail.com>
Committed: Wed Apr 25 14:22:25 2018 +0200

----------------------------------------------------------------------
 .../lib/driver/driver-remote-connection.js      |  2 +-
 .../lib/structure/io/graph-serializer.js        | 11 ++-
 .../lib/structure/io/type-serializers.js        | 81 ++++++++++++++++++++
 .../test/cucumber/feature-steps.js              |  4 +-
 .../gremlin-javascript/test/cucumber/world.js   | 10 +--
 5 files changed, 96 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/9357c9e5/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js
index d9e6000..ac148a3 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js
@@ -33,7 +33,7 @@ const responseStatusCode = {
   noContent: 204,
   partialContent: 206
 };
-const defaultMimeType = 'application/vnd.gremlin-v2.0+json';
+const defaultMimeType = 'application/vnd.gremlin-v3.0+json';
 
 class DriverRemoteConnection extends RemoteConnection {
   /**

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/9357c9e5/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/graph-serializer.js
----------------------------------------------------------------------
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 df05659..df64e41 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
@@ -159,8 +159,11 @@ const deserializers = {
   'g:Edge': typeSerializers.EdgeSerializer,
   'g:VertexProperty': typeSerializers.VertexPropertySerializer,
   'g:Property': typeSerializers.PropertySerializer,
-  'g:Path': typeSerializers.PathSerializer,
-  'g:T': typeSerializers.TSerializer
+  'g:Path': typeSerializers.Path3Serializer,
+  'g:T': typeSerializers.TSerializer,
+  'g:List': typeSerializers.ListSerializer,
+  'g:Set': typeSerializers.SetSerializer,
+  'g:Map': typeSerializers.MapSerializer
 };
 
 const serializers = [
@@ -172,7 +175,9 @@ const serializers = [
   typeSerializers.EnumSerializer,
   typeSerializers.VertexSerializer,
   typeSerializers.EdgeSerializer,
-  typeSerializers.LongSerializer
+  typeSerializers.LongSerializer,
+  typeSerializers.ListSerializer,
+  typeSerializers.MapSerializer
 ];
 
 module.exports = {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/9357c9e5/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
index 304888f..3b2b9e6 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
@@ -270,22 +270,103 @@ class PathSerializer extends TypeSerializer {
   }
 }
 
+class Path3Serializer extends TypeSerializer {
+  deserialize(obj) {
+    const value = obj[valueKey];
+    return new g.Path(this.reader.read(value['labels']), this.reader.read(value['objects']));
+  }
+}
+
 class TSerializer extends TypeSerializer {
   deserialize(obj) {
     return t.t[obj[valueKey]];
   }
 }
 
+class ArraySerializer extends TypeSerializer {
+  constructor(typeKey) {
+    super();
+    this.typeKey = typeKey;
+  }
+
+  deserialize(obj) {
+    const value = obj[valueKey];
+    if (!Array.isArray(value)) {
+      throw new Error('Expected Array, obtained: ' + value);
+    }
+    return value.map(x => this.reader.read(x));
+  }
+
+  /** @param {Array} item */
+  serialize(item) {
+    return {
+      [typeKey]: this.typeKey,
+      [valueKey]: item.map(x => this.writer.adaptObject(x))
+    };
+  }
+
+  canBeUsedFor(value) {
+    return Array.isArray(value);
+  }
+}
+
+class MapSerializer extends TypeSerializer {
+  deserialize(obj) {
+    const value = obj[valueKey];
+    if (!Array.isArray(value)) {
+      throw new Error('Expected Array, obtained: ' + value);
+    }
+    const result = new Map();
+    for (let i = 0; i < value.length; i += 2) {
+      result.set(this.reader.read(value[i]), this.reader.read(value[i + 1]));
+    }
+    return result;
+  }
+
+  /** @param {Map} map */
+  serialize(map) {
+    const arr = [];
+    map.forEach((v, k) => {
+      arr.push(this.writer.adaptObject(k));
+      arr.push(this.writer.adaptObject(v));
+    });
+    return {
+      [typeKey]: 'g:Map',
+      [valueKey]: arr
+    };
+  }
+
+  canBeUsedFor(value) {
+    return value instanceof Map;
+  }
+}
+
+class ListSerializer extends ArraySerializer {
+  constructor() {
+    super('g:List');
+  }
+}
+
+class SetSerializer extends ArraySerializer {
+  constructor() {
+    super('g:Set');
+  }
+}
+
 module.exports = {
   BytecodeSerializer,
   EdgeSerializer,
   EnumSerializer,
   LambdaSerializer,
+  ListSerializer,
   LongSerializer,
+  MapSerializer,
   NumberSerializer,
+  Path3Serializer,
   PathSerializer,
   PropertySerializer,
   PSerializer,
+  SetSerializer,
   TSerializer,
   TraverserSerializer,
   typeKey,

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/9357c9e5/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
index d774754..047856f 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
@@ -37,7 +37,7 @@ const t = traversalModule.t;
 
 // Determines whether the feature maps (m[]), are deserialized as objects (true) or maps
(false).
 // Use false for GraphSON3.
-const mapAsObject = true;
+const mapAsObject = false;
 
 const parsers = [
   [ 'd\\[([\\d.]+)\\]\\.[ilfdm]', toNumeric ],
@@ -233,7 +233,7 @@ function toNumeric(stringValue) {
 }
 
 function toVertex(name) {
-  return this.getData().vertices[name];
+  return this.getData().vertices.get(name);
 }
 
 function toVertexId(name) {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/9357c9e5/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js
index f531720..269e20a 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/world.js
@@ -111,16 +111,14 @@ function getEdges(connection) {
     .next()
     .then(it => {
       const edges = {};
-      Object.keys(it.value).map(key => {
-        edges[getEdgeKey(key)] = it.value[key];
+      it.value.forEach((v, k) => {
+        edges[getEdgeKey(k)] = v;
       });
       return edges;
     });
 }
 
 function getEdgeKey(key) {
-  const o = /o=(.+?)[,}]/.exec(key)[1];
-  const l = /l=(.+?)[,}]/.exec(key)[1];
-  const i = /i=(.+?)[,}]/.exec(key)[1];
-  return o + "-" + l + "->" + i;
+  // key is a map
+  return key.get('o') + "-" + key.get('l') + "->" + key.get('i');
 }
\ No newline at end of file


Mime
View raw message