Skip to content

Commit e75e833

Browse files
authored
Vector type stringer unifiction and isVector function (#1327)
1 parent ade8503 commit e75e833

File tree

9 files changed

+120
-10
lines changed

9 files changed

+120
-10
lines changed

packages/bolt-connection/src/bolt/bolt-protocol-v6x0.transformer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import v5x8 from './bolt-protocol-v5x8.transformer'
1919
import { TypeTransformer } from './transformer'
2020
import { structure } from '../packstream'
21-
import { Vector, newError } from 'neo4j-driver-core'
21+
import { Vector, isVector, newError } from 'neo4j-driver-core'
2222
const VECTOR = 0x56
2323
const FLOAT_32 = 0xc6
2424
const FLOAT_64 = 0xc1
@@ -39,7 +39,7 @@ const typeToTypeMarker = {
3939
function createVectorTransformer () {
4040
return new TypeTransformer({
4141
signature: VECTOR,
42-
isTypeInstance: object => object instanceof Vector,
42+
isTypeInstance: object => isVector(object),
4343
toStructure: vector => {
4444
const typeMarker = typeToTypeMarker[vector.getType()]
4545
if (typeMarker === undefined) {

packages/core/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ import * as json from './json'
102102
import resultTransformers, { ResultTransformer } from './result-transformers'
103103
import ClientCertificate, { clientCertificateProviders, ClientCertificateProvider, ClientCertificateProviders, RotatingClientCertificateProvider, resolveCertificateProvider } from './client-certificate'
104104
import * as internal from './internal' // todo: removed afterwards
105-
import Vector, { VectorType, vector } from './vector'
105+
import Vector, { VectorType, vector, isVector } from './vector'
106106

107107
/**
108108
* Object containing string constants representing predefined {@link Neo4jError} codes.
@@ -189,7 +189,9 @@ const forExport = {
189189
notificationFilterDisabledClassification,
190190
notificationFilterMinimumSeverityLevel,
191191
clientCertificateProviders,
192-
resolveCertificateProvider
192+
resolveCertificateProvider,
193+
isVector,
194+
vector
193195
}
194196

195197
export {
@@ -268,6 +270,7 @@ export {
268270
notificationFilterMinimumSeverityLevel,
269271
clientCertificateProviders,
270272
resolveCertificateProvider,
273+
isVector,
271274
Vector,
272275
vector
273276
}

packages/core/src/vector.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import { newError } from './error'
1919

20+
const VECTOR_IDENTIFIER_PROPERTY = '__isVector__'
21+
2022
type EnumRecord<T extends string | symbol> = { [key in T]: key }
2123

2224
export type VectorType = 'INT8' | 'INT16' | 'INT32' | 'INT64' | 'FLOAT32' | 'FLOAT64'
@@ -87,8 +89,38 @@ export default class Vector<K extends Float32Array | Float64Array | Int8Array |
8789
getType (): VectorType {
8890
return this._type
8991
}
92+
93+
toString (): string {
94+
return `vector([${this._typedArray.join(', ')}], ${this._typedArray.length}, ${getTypeString(this._type)})`
95+
}
96+
}
97+
98+
function getTypeString (type: VectorType): string {
99+
switch (type) {
100+
case 'INT8':
101+
return 'INTEGER8 NOT NULL'
102+
case 'INT16':
103+
return 'INTEGER16 NOT NULL'
104+
case 'INT32':
105+
return 'INTEGER32 NOT NULL'
106+
case 'INT64':
107+
return 'INTEGER NOT NULL'
108+
case 'FLOAT32':
109+
return 'FLOAT32 NOT NULL'
110+
case 'FLOAT64':
111+
return 'FLOAT NOT NULL'
112+
default:
113+
throw newError(`Cannot stringify vector with unsupported type. Got type: ${type as string}`)
114+
}
90115
}
91116

117+
Object.defineProperty(Vector.prototype, VECTOR_IDENTIFIER_PROPERTY, {
118+
value: true,
119+
enumerable: false,
120+
configurable: false,
121+
writable: false
122+
})
123+
92124
/**
93125
* Cast a TypedArray to a {@link Vector}
94126
* @access public
@@ -98,3 +130,12 @@ export default class Vector<K extends Float32Array | Float64Array | Int8Array |
98130
export function vector<K extends Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | BigInt64Array> (typedArray: K): Vector<K> {
99131
return new Vector(typedArray)
100132
}
133+
134+
/**
135+
* Test if given object is an instance of the {@link Vector} class.
136+
* @param {Object} obj the object to test.
137+
* @return {boolean} `true` if given object is a {@link Vector}, `false` otherwise.
138+
*/
139+
export function isVector<K extends Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | BigInt64Array> (obj: any): obj is Vector<K> {
140+
return obj != null && obj[VECTOR_IDENTIFIER_PROPERTY] === true
141+
}

packages/core/test/vector-type.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,20 @@ describe('Vector', () => {
3232
expect(vec.asTypedArray()).toEqual(typedArray)
3333
})
3434
})
35+
describe('.toString()', () => {
36+
it.each([
37+
['Int8Array', Int8Array.from([1]), 'vector([1], 1, INTEGER8 NOT NULL)'],
38+
['Int16Array', Int16Array.from([1, 2, 3]), 'vector([1, 2, 3], 3, INTEGER16 NOT NULL)'],
39+
['Int32Array', Int32Array.from([3]), 'vector([3], 1, INTEGER32 NOT NULL)'],
40+
['BigInt64Array', BigInt64Array.from([BigInt(0)]), 'vector([0], 1, INTEGER NOT NULL)'],
41+
['Float32Array', Float32Array.from([0.5, 1.875]), 'vector([0.5, 1.875], 2, FLOAT32 NOT NULL)'],
42+
['Float32Array with special values', Float32Array.from([-Infinity, Infinity, NaN, -NaN, 1.401298464324817e-45, 3.4028235e38]), 'vector([-Infinity, Infinity, NaN, NaN, 1.401298464324817e-45, 3.4028234663852886e+38], 6, FLOAT32 NOT NULL)'],
43+
['Float64Array', Float64Array.from([2]), 'vector([2], 1, FLOAT NOT NULL)'],
44+
['Float64Array with special values', Float64Array.from([-Infinity, Infinity, NaN, -NaN, 2.2250738585072014e-308, 1.7976931348623157e308]), 'vector([-Infinity, Infinity, NaN, NaN, 2.2250738585072014e-308, 1.7976931348623157e+308], 6, FLOAT NOT NULL)'],
45+
['Float64Array of huge size', Float64Array.from([...Array(10000).keys()]), `vector([${[...Array(10000).keys()].join(', ')}], 10000, FLOAT NOT NULL)`]
46+
])('should correctly stringify (%s)', (_, typedArray, expectedString) => {
47+
const vec = vector(typedArray)
48+
expect(vec.toString()).toEqual(expectedString)
49+
})
50+
})
3551
})

packages/neo4j-driver-deno/lib/bolt-connection/bolt/bolt-protocol-v6x0.transformer.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/neo4j-driver-deno/lib/core/index.ts

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/neo4j-driver-deno/lib/core/vector.ts

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/neo4j-driver/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import {
8080
staticAuthTokenManager,
8181
clientCertificateProviders,
8282
resolveCertificateProvider,
83+
isVector,
8384
Vector,
8485
VectorType,
8586
vector
@@ -410,6 +411,7 @@ const forExport = {
410411
notificationFilterDisabledCategory,
411412
notificationFilterMinimumSeverityLevel,
412413
clientCertificateProviders,
414+
isVector,
413415
vector
414416
}
415417

@@ -484,6 +486,7 @@ export {
484486
notificationFilterDisabledClassification,
485487
notificationFilterMinimumSeverityLevel,
486488
clientCertificateProviders,
489+
isVector,
487490
vector,
488491
Vector,
489492
VectorType

packages/neo4j-driver/types/index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ import {
9898
ClientCertificateProviders,
9999
RotatingClientCertificateProvider,
100100
clientCertificateProviders,
101-
types as coreTypes
101+
types as coreTypes,
102+
isVector
102103
} from 'neo4j-driver-core'
103104
import {
104105
AuthToken,
@@ -299,6 +300,7 @@ declare const forExport: {
299300
notificationFilterMinimumSeverityLevel: typeof notificationFilterMinimumSeverityLevel
300301
logging: typeof logging
301302
clientCertificateProviders: typeof clientCertificateProviders
303+
isVector: typeof isVector
302304
}
303305

304306
export {
@@ -379,7 +381,8 @@ export {
379381
notificationFilterDisabledClassification,
380382
notificationFilterMinimumSeverityLevel,
381383
logging,
382-
clientCertificateProviders
384+
clientCertificateProviders,
385+
isVector
383386
}
384387

385388
export type {

0 commit comments

Comments
 (0)