Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/results/resultUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ export function convertValue<T extends RelTypedValue>(
}
});
}
case 'UUID':
return toUuid(Array.from(value));
case 'Unknown':
return value && value.toJSON ? value.toJSON() : value;
}
Expand Down Expand Up @@ -297,6 +299,7 @@ export function getDisplayValue(

switch (val.type) {
case 'String':
case 'UUID':
return JSON.stringify(val.value);
case 'Bool':
return val.value ? 'true' : 'false';
Expand Down Expand Up @@ -410,6 +413,21 @@ function uint128ToBigInt(tuple: bigint[]) {
return (BigInt.asUintN(64, tuple[1]) << BigInt(64)) | tuple[0];
}

function toUuid(tuple: bigint[]) {
const num = uint128ToBigInt(tuple);

const str = num.toString(16).padStart(32, '0');
const parts = [
str.slice(0, 8),
str.slice(8, 12),
str.slice(12, 16),
str.slice(16, 20),
str.slice(20),
];

return parts.join('-');
}

function mapPrimitiveValue(val: PrimitiveValue) {
switch (val.value.oneofKind) {
case 'stringVal':
Expand Down Expand Up @@ -482,6 +500,7 @@ function mapValueType(typeDef: Omit<ValueTypeValue, 'value'>): RelTypeDef {
case 'Missing':
case 'Hash':
case 'AutoNumber':
case 'UUID':
return {
type: standardValueType,
};
Expand Down
94 changes: 94 additions & 0 deletions src/results/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,20 @@ export const standardTypeTests: Test[] = [
values: [1n],
displayValues: ['1'],
},
{
name: 'UUID',
query: `
with rel:base use uuid_from_string
def output = uuid_from_string["22b4a8a1-e548-4eeb-9270-60426d66a48e"]
`,
typeDefs: [
{
type: 'UUID',
},
],
values: ['22b4a8a1-e548-4eeb-9270-60426d66a48e'],
displayValues: ['"22b4a8a1-e548-4eeb-9270-60426d66a48e"'],
},
];

export const specializationTests: Test[] = [
Expand Down Expand Up @@ -1406,6 +1420,25 @@ export const specializationTests: Test[] = [
values: [1n],
displayValues: ['1'],
},
{
name: 'UUID',
query: `
with rel:base use uuid_from_string
def v = uuid_from_string["22b4a8a1-e548-4eeb-9270-60426d66a48e"]
def output = #(v)
`,
typeDefs: [
{
type: 'Constant',
value: {
type: 'UUID',
value: '22b4a8a1-e548-4eeb-9270-60426d66a48e',
},
},
],
values: ['22b4a8a1-e548-4eeb-9270-60426d66a48e'],
displayValues: ['"22b4a8a1-e548-4eeb-9270-60426d66a48e"'],
},
];

export const valueTypeTests: Test[] = [
Expand Down Expand Up @@ -2558,6 +2591,34 @@ export const valueTypeTests: Test[] = [
values: [[':MyType', 1n, 1n]],
displayValues: ['(:MyType, 1, 1)'],
},
{
name: 'UUID',
query: `
with rel:base use uuid_from_string, UUID
def uuid = uuid_from_string["22b4a8a1-e548-4eeb-9270-60426d66a48e"]
value type MyType = Int, UUID
def output = ^MyType[1, uuid]
`,
typeDefs: [
{
type: 'ValueType',
typeDefs: [
{
type: 'Constant',
value: { type: 'String', value: ':MyType' },
},
{
type: 'Int64',
},
{
type: 'UUID',
},
],
},
],
values: [[':MyType', 1n, '22b4a8a1-e548-4eeb-9270-60426d66a48e']],
displayValues: ['(:MyType, 1, "22b4a8a1-e548-4eeb-9270-60426d66a48e")'],
},
];

export const miscValueTypeTests: Test[] = [
Expand Down Expand Up @@ -4106,4 +4167,37 @@ export const valueTypeSpecializationTests: Test[] = [
values: [[':MyType', 1n, 1n]],
displayValues: ['(:MyType, 1, 1)'],
},
{
name: 'UUID',
query: `
with rel:base use uuid_from_string, UUID
def uuid = uuid_from_string["22b4a8a1-e548-4eeb-9270-60426d66a48e"]
value type MyType = UUID, Int
def v = ^MyType[uuid, 1]
def output = #(v)
`,
typeDefs: [
{
type: 'Constant',
value: {
type: 'ValueType',
typeDefs: [
{
type: 'Constant',
value: { type: 'String', value: ':MyType' },
},
{
type: 'UUID',
},
{
type: 'Int64',
},
],
value: [':MyType', '22b4a8a1-e548-4eeb-9270-60426d66a48e', 1n],
},
},
],
values: [[':MyType', '22b4a8a1-e548-4eeb-9270-60426d66a48e', 1n]],
displayValues: ['(:MyType, "22b4a8a1-e548-4eeb-9270-60426d66a48e", 1)'],
},
];
9 changes: 8 additions & 1 deletion src/results/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export type RelBaseTypedValue =
| Rational32Value
| Rational64Value
| Rational128Value
| AutoNumber;
| AutoNumber
| UUID;

export type RelTypedValue = RelBaseTypedValue | ValueTypeValue | UnknownType;

Expand Down Expand Up @@ -106,6 +107,7 @@ export type RelTypeDef =
| Omit<Rational64Value, 'value'>
| Omit<Rational128Value, 'value'>
| Omit<AutoNumber, 'value'>
| Omit<UUID, 'value'>
| ConstantValue
| Omit<ValueTypeValue, 'value'>
| Omit<UnknownType, 'value'>;
Expand Down Expand Up @@ -345,6 +347,11 @@ export type AutoNumber = {
value: bigint;
};

export type UUID = {
type: 'UUID';
value: string;
};

// TODO: should be removed with JSON based metadata implementation?
export type UnknownType = {
type: 'Unknown';
Expand Down