You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ke...@apache.org on 2023/10/08 22:57:37 UTC

[tinkerpop] 01/03: update javascript to deserialize Sets

This is an automated email from the ASF dual-hosted git repository.

kenhuuu pushed a commit to branch gherkin-set-results
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit d629abb73c34eb46ca29f1033faa8c787ee65305
Author: Ken Hu <10...@users.noreply.github.com>
AuthorDate: Wed Oct 4 21:14:38 2023 -0700

    update javascript to deserialize Sets
---
 .../structure/io/binary/internals/SetSerializer.js | 106 +++++++++++++++++++++
 .../lib/structure/io/type-serializers.js           |   4 +
 .../test/cucumber/feature-steps.js                 |   9 +-
 3 files changed, 118 insertions(+), 1 deletion(-)

diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/binary/internals/SetSerializer.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/binary/internals/SetSerializer.js
new file mode 100644
index 0000000000..c129fc7abf
--- /dev/null
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/binary/internals/SetSerializer.js
@@ -0,0 +1,106 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+/**
+ * @author Igor Ostapenko
+ */
+'use strict';
+
+module.exports = class SetSerializer {
+  constructor(ioc, ID) {
+    this.ioc = ioc;
+    this.ID = ID;
+    this.ioc.serializers[ID] = this;
+  }
+
+  canBeUsedFor(value) {
+    return value instanceof Set;
+  }
+
+  serialize(item, fullyQualifiedFormat = true) {
+    throw new Error('serialize() method not implemented for SetSerializer');
+  }
+
+  deserialize(buffer, fullyQualifiedFormat = true) {
+    let len = 0;
+    let cursor = buffer;
+
+    try {
+      if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
+        throw new Error('buffer is missing');
+      }
+      if (buffer.length < 1) {
+        throw new Error('buffer is empty');
+      }
+
+      if (fullyQualifiedFormat) {
+        const type_code = cursor.readUInt8();
+        len++;
+        if (type_code !== this.ID) {
+          throw new Error('unexpected {type_code}');
+        }
+        cursor = cursor.slice(1);
+
+        if (cursor.length < 1) {
+          throw new Error('{value_flag} is missing');
+        }
+        const value_flag = cursor.readUInt8();
+        len++;
+        if (value_flag === 1) {
+          return { v: null, len };
+        }
+        if (value_flag !== 0) {
+          throw new Error('unexpected {value_flag}');
+        }
+        cursor = cursor.slice(1);
+      }
+
+      let length, length_len;
+      try {
+        ({ v: length, len: length_len } = this.ioc.intSerializer.deserialize(cursor, false));
+        len += length_len;
+      } catch (err) {
+        err.message = '{length}: ' + err.message;
+        throw err;
+      }
+      if (length < 0) {
+        throw new Error('{length} is less than zero');
+      }
+      cursor = cursor.slice(length_len);
+
+      const v = new Set();
+      for (let i = 0; i < length; i++) {
+        let value, value_len;
+        try {
+          ({ v: value, len: value_len } = this.ioc.anySerializer.deserialize(cursor));
+          len += value_len;
+        } catch (err) {
+          err.message = `{item_${i}}: ` + err.message;
+          throw err;
+        }
+        cursor = cursor.slice(value_len);
+        v.add(value);
+      }
+
+      return { v, len };
+    } catch (err) {
+      throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
+    }
+  }
+};
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 3eefbaad82..599e132dd0 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
@@ -474,6 +474,10 @@ class SetSerializer extends ArraySerializer {
   constructor() {
     super('g:Set');
   }
+
+  deserialize(obj) {
+    return new Set(super.deserialize(obj));
+  }
 }
 
 module.exports = {
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 8ce00869ad..f9f194ed49 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
@@ -54,7 +54,7 @@ const parsers = [
   [ 'vp\\[(.+)\\]', toVertexProperty ],
   [ 'p\\[(.+)\\]', toPath ],
   [ 'l\\[(.*)\\]', toArray ],
-  [ 's\\[(.*)\\]', toArray ],
+  [ 's\\[(.*)\\]', toSet ],
   [ 'm\\[(.+)\\]', toMap ],
   [ 'c\\[(.+)\\]', toLambda ],
   [ 't\\[(.+)\\]', toT ],
@@ -368,6 +368,13 @@ function toMap(stringMap) {
   return parseMapValue.call(this, JSON.parse(stringMap));
 }
 
+function toSet(stringSet) {
+  if (stringSet === '') {
+    return new Set();
+  }
+  return new Set(stringSet.split(',').map(x => parseValue.call(this, x)));
+}
+
 function parseMapValue(value) {
   if (value === null)
     return null;