Skip to content

Commit

Permalink
feat: utility to parse vertex properties and handle cardinality
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed May 9, 2024
1 parent 5540244 commit b5598f3
Show file tree
Hide file tree
Showing 6 changed files with 3,182 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gremlin-javascript/examples/browser/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -729,14 +729,14 @@ __metadata:

"gremlin@file:../../src/main/javascript/gremlin-javascript::locator=browser%40workspace%3A.":
version: 4.0.0-alpha1
resolution: "gremlin@file:../../src/main/javascript/gremlin-javascript#../../src/main/javascript/gremlin-javascript::hash=83bc8e&locator=browser%40workspace%3A."
resolution: "gremlin@file:../../src/main/javascript/gremlin-javascript#../../src/main/javascript/gremlin-javascript::hash=fd5a82&locator=browser%40workspace%3A."
dependencies:
buffer: "npm:^6.0.3"
eventemitter3: "npm:^5.0.1"
readable-stream: "npm:^4.5.2"
uuid: "npm:^9.0.1"
ws: "npm:^8.16.0"
checksum: 10c0/13986e0b78a84a421e5542a7bb4ba8cca3fb7a12cb7fcce1426a40233f434a4ce86315aa943ce6818b101126a0167b8fa8e0a516dea0e98576fd68a22eef5650
checksum: 10c0/4ac483e068ae230130d3ca60718d3fac7031f00f2aba193f2bffffd444064281db5336ae55b16243d9427ff8ef60baad792c908ec3a41c21972f9dffcc85534f
languageName: node
linkType: hard

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ export const structure = {
Vertex: graph.Vertex,
VertexProperty: graph.VertexProperty,
toLong: utils.toLong,
parseVertex: utils.parseVertex,
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import * as uuid from 'uuid';
import type { Vertex, VertexProperties } from './structure/graph.js';

const gremlinVersion = '4.0.0-SNAPSHOT'; // DO NOT MODIFY - Configured automatically by Maven Replacer Plugin

Expand Down Expand Up @@ -119,3 +120,35 @@ export const DeferredPromise = <T>() => {

return Object.assign(promise, { resolve, reject });
};

export const parseVertex = <
TVertex extends Vertex,
TCardinality extends { [P in keyof NonNullable<TVertex['properties']>]?: 'single' | 'list' | 'set' | undefined },
>(
vertex: TVertex,
cardinality: TCardinality = {} as TCardinality,
): {
[P in keyof NonNullable<TVertex['properties']>]: (typeof cardinality)[P] extends 'single'
? NonNullable<TVertex['properties']>[P][0]['value']
: (typeof cardinality)[P] extends 'list'
? Array<NonNullable<TVertex['properties']>[P][0]['value']>
: (typeof cardinality)[P] extends 'set'
? Set<NonNullable<TVertex['properties']>[P][0]['value']>
: NonNullable<TVertex['properties']>[P][0]['value'];
} =>
Object.fromEntries(
Object.entries<VertexProperties>(vertex.properties as any).map(([key, value]): any => [
key,
(() => {
switch (cardinality[key]) {
case 'list':
return value.map((x) => x.value);
case 'set':
return new Set(value.map((x) => x.value));
case 'single':
default:
return value[0].value;
}
})(),
]),
) as any;
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"@cucumber/cucumber": "^10.3.1",
"@knighted/duel": "^1.0.7",
"@tsconfig/node18": "^18.2.2",
"@types/chai": "^4.3.16",
"@types/mocha": "^10.0.6",
"@types/readable-stream": "^4.0.10",
"@types/uuid": "^9.0.8",
"@types/ws": "^8.5.10",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { Vertex, VertexProperties, VertexProperty } from '../../lib/structure/graph.js';
import { parseVertex } from '../../lib/utils.js';

describe('Vertex parser', () => {
it('should parse vertex properties with default cardinality', () => {
type PetVertex = Vertex<'pet', { name: VertexProperties<'name', string> }>;

const vertex: PetVertex = new Vertex(0, 'pet', {
name: [new VertexProperty(0, 'name', 'Pal')],
});

const parsedVertex = parseVertex(vertex);

expect(parsedVertex.name).to.equals('Fred');
});

it('should parse vertex properties with different cardinality', () => {
type PersonVertex = Vertex<
'person',
{
name: VertexProperties<'name', string>;
roles: VertexProperties<'role', string>;
weapons: VertexProperties<'weapon', string>;
}
>;

const vertex: PersonVertex = new Vertex(0, 'person', {
name: [new VertexProperty(0, 'name', 'Fred')],
roles: [new VertexProperty(0, 'role', 'hunter'), new VertexProperty(0, 'role', 'gatherer')],
weapons: [new VertexProperty(0, 'weapon', 'spear'), new VertexProperty(0, 'weapon', 'spear')],
});

const parsedVertex = parseVertex(vertex, { name: 'single', roles: 'set', weapons: 'list' });

expect(parsedVertex.name).to.equals('Fred');
expect(parsedVertex.roles).to.be.an.instanceOf(Set).to.have.all.keys(['hunter', 'gatherer']);
expect(parsedVertex.weapons).to.be.an.instanceOf(Array).to.includes.members(['spear', 'spear']);
});
});

0 comments on commit b5598f3

Please sign in to comment.