Skip to content

Commit 5f166a3

Browse files
committed
feat: enhance UUID handling in Primitive class and add corresponding tests
Signed-off-by: Vladislav Polyakov <[email protected]>
1 parent f6a7ee5 commit 5f166a3

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

packages/value/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export type JSValue =
2828
export function fromYdb(value: Ydb.Value, type: Ydb.Type): Value {
2929
switch (type.type.case) {
3030
case 'typeId':
31-
return new Primitive({ value: value.value, high128: value.high128 }, new PrimitiveType(type.type.value))
31+
let pValue = new Primitive({ value: value.value }, new PrimitiveType(type.type.value))
32+
if (value.high128) {
33+
pValue.high128 = value.high128
34+
}
35+
36+
return pValue
3237
case 'listType':
3338
return new List(...value.items.map((v) => fromYdb(v, (type.type.value as unknown as Ydb.ListType).item!)))
3439
case 'tupleType':

packages/value/src/primitive.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as Ydb from '@ydbjs/api/value'
55
import { type GenericDateConstructor, formatISO9075 } from 'date-fns'
66

77
import { type Type, TypeKind } from './type.js'
8-
import { bigIntsFromUuid } from './uuid.js'
8+
import { bigIntsFromUuid, uuidFromBigInts } from './uuid.js'
99
import { type Value } from './value.js'
1010

1111
export class PrimitiveType implements Type {
@@ -51,7 +51,6 @@ export class Primitive implements Value<PrimitiveType> {
5151
this.type = type
5252
this.value = value?.value?.value
5353
this.#value = value
54-
this.high128 = value.high128
5554
}
5655

5756
encode(): Ydb.Value {
@@ -282,15 +281,21 @@ export class UuidType extends PrimitiveType {
282281
}
283282

284283
export class Uuid extends Primitive implements Value<UuidType> {
284+
low128: bigint = 0n
285285
override high128: bigint = 0n
286286

287287
constructor(value: string) {
288288
let { low128, high128 } = bigIntsFromUuid(value)
289289

290290
super({ value: { case: 'low128', value: low128 }, high128 }, new UuidType())
291291

292+
this.low128 = low128
292293
this.high128 = high128
293294
}
295+
296+
override toString(): string {
297+
return uuidFromBigInts(this.low128, this.high128)
298+
}
294299
}
295300

296301
export class DateType extends PrimitiveType {

packages/value/tests/primitive.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
Timestamp,
1212
Uint32,
1313
Uint64,
14+
Uuid,
1415
} from '../dist/esm/primitive.js'
1516

1617
test('Bool primitive', async (t) => {
@@ -144,3 +145,18 @@ test('Timestamp primitive', async (t) => {
144145
}
145146
`)
146147
})
148+
149+
test('UUID primitive', async (t) => {
150+
const uuid = new Uuid('00112233-4455-6677-8899-aabbccddeeff')
151+
152+
t.expect(uuid).toMatchInlineSnapshot(`
153+
Uuid {
154+
"high128": 18441921395520346504n,
155+
"low128": 7383445245961249331n,
156+
"type": UuidType {},
157+
"value": 7383445245961249331n,
158+
}
159+
`)
160+
161+
t.expect(uuid.toString()).toBe('00112233-4455-6677-8899-aabbccddeeff')
162+
})

0 commit comments

Comments
 (0)