Skip to content

Commit 05bc0ed

Browse files
author
Denis Gursky
authored
added uuid support (#91)
1 parent 1932f72 commit 05bc0ed

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

src/results/resultUtils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ export function convertValue<T extends RelTypedValue>(
270270
}
271271
});
272272
}
273+
case 'UUID':
274+
return toUuid(Array.from(value));
273275
case 'Unknown':
274276
return value && value.toJSON ? value.toJSON() : value;
275277
}
@@ -297,6 +299,7 @@ export function getDisplayValue(
297299

298300
switch (val.type) {
299301
case 'String':
302+
case 'UUID':
300303
return JSON.stringify(val.value);
301304
case 'Bool':
302305
return val.value ? 'true' : 'false';
@@ -410,6 +413,21 @@ function uint128ToBigInt(tuple: bigint[]) {
410413
return (BigInt.asUintN(64, tuple[1]) << BigInt(64)) | tuple[0];
411414
}
412415

416+
function toUuid(tuple: bigint[]) {
417+
const num = uint128ToBigInt(tuple);
418+
419+
const str = num.toString(16).padStart(32, '0');
420+
const parts = [
421+
str.slice(0, 8),
422+
str.slice(8, 12),
423+
str.slice(12, 16),
424+
str.slice(16, 20),
425+
str.slice(20),
426+
];
427+
428+
return parts.join('-');
429+
}
430+
413431
function mapPrimitiveValue(val: PrimitiveValue) {
414432
switch (val.value.oneofKind) {
415433
case 'stringVal':
@@ -482,6 +500,7 @@ function mapValueType(typeDef: Omit<ValueTypeValue, 'value'>): RelTypeDef {
482500
case 'Missing':
483501
case 'Hash':
484502
case 'AutoNumber':
503+
case 'UUID':
485504
return {
486505
type: standardValueType,
487506
};

src/results/tests.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,20 @@ export const standardTypeTests: Test[] = [
590590
values: [1n],
591591
displayValues: ['1'],
592592
},
593+
{
594+
name: 'UUID',
595+
query: `
596+
with rel:base use uuid_from_string
597+
def output = uuid_from_string["22b4a8a1-e548-4eeb-9270-60426d66a48e"]
598+
`,
599+
typeDefs: [
600+
{
601+
type: 'UUID',
602+
},
603+
],
604+
values: ['22b4a8a1-e548-4eeb-9270-60426d66a48e'],
605+
displayValues: ['"22b4a8a1-e548-4eeb-9270-60426d66a48e"'],
606+
},
593607
];
594608

595609
export const specializationTests: Test[] = [
@@ -1406,6 +1420,25 @@ export const specializationTests: Test[] = [
14061420
values: [1n],
14071421
displayValues: ['1'],
14081422
},
1423+
{
1424+
name: 'UUID',
1425+
query: `
1426+
with rel:base use uuid_from_string
1427+
def v = uuid_from_string["22b4a8a1-e548-4eeb-9270-60426d66a48e"]
1428+
def output = #(v)
1429+
`,
1430+
typeDefs: [
1431+
{
1432+
type: 'Constant',
1433+
value: {
1434+
type: 'UUID',
1435+
value: '22b4a8a1-e548-4eeb-9270-60426d66a48e',
1436+
},
1437+
},
1438+
],
1439+
values: ['22b4a8a1-e548-4eeb-9270-60426d66a48e'],
1440+
displayValues: ['"22b4a8a1-e548-4eeb-9270-60426d66a48e"'],
1441+
},
14091442
];
14101443

14111444
export const valueTypeTests: Test[] = [
@@ -2558,6 +2591,34 @@ export const valueTypeTests: Test[] = [
25582591
values: [[':MyType', 1n, 1n]],
25592592
displayValues: ['(:MyType, 1, 1)'],
25602593
},
2594+
{
2595+
name: 'UUID',
2596+
query: `
2597+
with rel:base use uuid_from_string, UUID
2598+
def uuid = uuid_from_string["22b4a8a1-e548-4eeb-9270-60426d66a48e"]
2599+
value type MyType = Int, UUID
2600+
def output = ^MyType[1, uuid]
2601+
`,
2602+
typeDefs: [
2603+
{
2604+
type: 'ValueType',
2605+
typeDefs: [
2606+
{
2607+
type: 'Constant',
2608+
value: { type: 'String', value: ':MyType' },
2609+
},
2610+
{
2611+
type: 'Int64',
2612+
},
2613+
{
2614+
type: 'UUID',
2615+
},
2616+
],
2617+
},
2618+
],
2619+
values: [[':MyType', 1n, '22b4a8a1-e548-4eeb-9270-60426d66a48e']],
2620+
displayValues: ['(:MyType, 1, "22b4a8a1-e548-4eeb-9270-60426d66a48e")'],
2621+
},
25612622
];
25622623

25632624
export const miscValueTypeTests: Test[] = [
@@ -4106,4 +4167,37 @@ export const valueTypeSpecializationTests: Test[] = [
41064167
values: [[':MyType', 1n, 1n]],
41074168
displayValues: ['(:MyType, 1, 1)'],
41084169
},
4170+
{
4171+
name: 'UUID',
4172+
query: `
4173+
with rel:base use uuid_from_string, UUID
4174+
def uuid = uuid_from_string["22b4a8a1-e548-4eeb-9270-60426d66a48e"]
4175+
value type MyType = UUID, Int
4176+
def v = ^MyType[uuid, 1]
4177+
def output = #(v)
4178+
`,
4179+
typeDefs: [
4180+
{
4181+
type: 'Constant',
4182+
value: {
4183+
type: 'ValueType',
4184+
typeDefs: [
4185+
{
4186+
type: 'Constant',
4187+
value: { type: 'String', value: ':MyType' },
4188+
},
4189+
{
4190+
type: 'UUID',
4191+
},
4192+
{
4193+
type: 'Int64',
4194+
},
4195+
],
4196+
value: [':MyType', '22b4a8a1-e548-4eeb-9270-60426d66a48e', 1n],
4197+
},
4198+
},
4199+
],
4200+
values: [[':MyType', '22b4a8a1-e548-4eeb-9270-60426d66a48e', 1n]],
4201+
displayValues: ['(:MyType, "22b4a8a1-e548-4eeb-9270-60426d66a48e", 1)'],
4202+
},
41094203
];

src/results/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export type RelBaseTypedValue =
6060
| Rational32Value
6161
| Rational64Value
6262
| Rational128Value
63-
| AutoNumber;
63+
| AutoNumber
64+
| UUID;
6465

6566
export type RelTypedValue = RelBaseTypedValue | ValueTypeValue | UnknownType;
6667

@@ -106,6 +107,7 @@ export type RelTypeDef =
106107
| Omit<Rational64Value, 'value'>
107108
| Omit<Rational128Value, 'value'>
108109
| Omit<AutoNumber, 'value'>
110+
| Omit<UUID, 'value'>
109111
| ConstantValue
110112
| Omit<ValueTypeValue, 'value'>
111113
| Omit<UnknownType, 'value'>;
@@ -345,6 +347,11 @@ export type AutoNumber = {
345347
value: bigint;
346348
};
347349

350+
export type UUID = {
351+
type: 'UUID';
352+
value: string;
353+
};
354+
348355
// TODO: should be removed with JSON based metadata implementation?
349356
export type UnknownType = {
350357
type: 'Unknown';

0 commit comments

Comments
 (0)