Skip to content

Commit 8613324

Browse files
author
Denis Gursky
authored
Added SHA1 value type support (#92)
* added autonumber support * added uuid * changed switch * added tests * added sha1 * fixed tests * fixed sha1 * removed empty line * updated README
1 parent 05bc0ed commit 8613324

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ their corresponding JavaScript equivalents. Full mapping:
112112
| Bool | boolean |
113113
| FilePos | bigint |
114114
| Missing | null |
115+
| AutoNumber | bigint |
116+
| UUID | string |
117+
| SHA1 | string |
115118

116119
## Support
117120

src/results/resultUtils.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ export function convertValue<T extends RelTypedValue>(
272272
}
273273
case 'UUID':
274274
return toUuid(Array.from(value));
275+
case 'SHA1': {
276+
const val = value.toArray ? value.toArray() : value;
277+
278+
return toSha1(Array.from(val[0]), val[1]);
279+
}
275280
case 'Unknown':
276281
return value && value.toJSON ? value.toJSON() : value;
277282
}
@@ -300,6 +305,7 @@ export function getDisplayValue(
300305
switch (val.type) {
301306
case 'String':
302307
case 'UUID':
308+
case 'SHA1':
303309
return JSON.stringify(val.value);
304310
case 'Bool':
305311
return val.value ? 'true' : 'false';
@@ -415,7 +421,6 @@ function uint128ToBigInt(tuple: bigint[]) {
415421

416422
function toUuid(tuple: bigint[]) {
417423
const num = uint128ToBigInt(tuple);
418-
419424
const str = num.toString(16).padStart(32, '0');
420425
const parts = [
421426
str.slice(0, 8),
@@ -428,6 +433,13 @@ function toUuid(tuple: bigint[]) {
428433
return parts.join('-');
429434
}
430435

436+
function toSha1(a: bigint[], b: Number) {
437+
return (
438+
uint128ToBigInt(a).toString(16).padStart(32, '0') +
439+
b.toString(16).padStart(8, '0')
440+
);
441+
}
442+
431443
function mapPrimitiveValue(val: PrimitiveValue) {
432444
switch (val.value.oneofKind) {
433445
case 'stringVal':
@@ -501,6 +513,7 @@ function mapValueType(typeDef: Omit<ValueTypeValue, 'value'>): RelTypeDef {
501513
case 'Hash':
502514
case 'AutoNumber':
503515
case 'UUID':
516+
case 'SHA1':
504517
return {
505518
type: standardValueType,
506519
};
@@ -592,7 +605,8 @@ function unflattenConstantValue(typeDef: RelTypeDef, value: PrimitiveValue[]) {
592605
case 'Rational32':
593606
case 'Rational64':
594607
case 'Rational128':
595-
// Rationals take 2 values
608+
case 'SHA1':
609+
// These types take 2 values
596610
result.push(values.splice(0, 2));
597611
break;
598612
default: {

src/results/tests.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,20 @@ export const standardTypeTests: Test[] = [
604604
values: ['22b4a8a1-e548-4eeb-9270-60426d66a48e'],
605605
displayValues: ['"22b4a8a1-e548-4eeb-9270-60426d66a48e"'],
606606
},
607+
{
608+
name: 'SHA1',
609+
query: `
610+
with rel:base use ^SHA1
611+
def output = ^SHA1[0x0d7d4a744fd92effd1ed88e48ac8231e, 0x7f7e9e6c]
612+
`,
613+
typeDefs: [
614+
{
615+
type: 'SHA1',
616+
},
617+
],
618+
values: ['0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c'],
619+
displayValues: ['"0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c"'],
620+
},
607621
];
608622

609623
export const specializationTests: Test[] = [
@@ -1439,6 +1453,25 @@ export const specializationTests: Test[] = [
14391453
values: ['22b4a8a1-e548-4eeb-9270-60426d66a48e'],
14401454
displayValues: ['"22b4a8a1-e548-4eeb-9270-60426d66a48e"'],
14411455
},
1456+
{
1457+
name: 'SHA1',
1458+
query: `
1459+
with rel:base use ^SHA1
1460+
def v = ^SHA1[0x0d7d4a744fd92effd1ed88e48ac8231e, 0x7f7e9e6c]
1461+
def output = #(v)
1462+
`,
1463+
typeDefs: [
1464+
{
1465+
type: 'Constant',
1466+
value: {
1467+
type: 'SHA1',
1468+
value: '0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c',
1469+
},
1470+
},
1471+
],
1472+
values: ['0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c'],
1473+
displayValues: ['"0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c"'],
1474+
},
14421475
];
14431476

14441477
export const valueTypeTests: Test[] = [
@@ -2619,6 +2652,34 @@ export const valueTypeTests: Test[] = [
26192652
values: [[':MyType', 1n, '22b4a8a1-e548-4eeb-9270-60426d66a48e']],
26202653
displayValues: ['(:MyType, 1, "22b4a8a1-e548-4eeb-9270-60426d66a48e")'],
26212654
},
2655+
{
2656+
name: 'SHA1',
2657+
query: `
2658+
with rel:base use ^SHA1, SHA1
2659+
def sha1 = ^SHA1[0x0d7d4a744fd92effd1ed88e48ac8231e, 0x7f7e9e6c]
2660+
value type MyType = Int, SHA1
2661+
def output = ^MyType[1, sha1]
2662+
`,
2663+
typeDefs: [
2664+
{
2665+
type: 'ValueType',
2666+
typeDefs: [
2667+
{
2668+
type: 'Constant',
2669+
value: { type: 'String', value: ':MyType' },
2670+
},
2671+
{
2672+
type: 'Int64',
2673+
},
2674+
{
2675+
type: 'SHA1',
2676+
},
2677+
],
2678+
},
2679+
],
2680+
values: [[':MyType', 1n, '0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c']],
2681+
displayValues: ['(:MyType, 1, "0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c")'],
2682+
},
26222683
];
26232684

26242685
export const miscValueTypeTests: Test[] = [
@@ -4200,4 +4261,37 @@ export const valueTypeSpecializationTests: Test[] = [
42004261
values: [[':MyType', '22b4a8a1-e548-4eeb-9270-60426d66a48e', 1n]],
42014262
displayValues: ['(:MyType, "22b4a8a1-e548-4eeb-9270-60426d66a48e", 1)'],
42024263
},
4264+
{
4265+
name: 'SHA1',
4266+
query: `
4267+
with rel:base use ^SHA1, SHA1
4268+
def sha1 = ^SHA1[0x0d7d4a744fd92effd1ed88e48ac8231e, 0x7f7e9e6c]
4269+
value type MyType = SHA1, Int
4270+
def v = ^MyType[sha1, 1]
4271+
def output = #(v)
4272+
`,
4273+
typeDefs: [
4274+
{
4275+
type: 'Constant',
4276+
value: {
4277+
type: 'ValueType',
4278+
typeDefs: [
4279+
{
4280+
type: 'Constant',
4281+
value: { type: 'String', value: ':MyType' },
4282+
},
4283+
{
4284+
type: 'SHA1',
4285+
},
4286+
{
4287+
type: 'Int64',
4288+
},
4289+
],
4290+
value: [':MyType', '0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c', 1n],
4291+
},
4292+
},
4293+
],
4294+
values: [[':MyType', '0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c', 1n]],
4295+
displayValues: ['(:MyType, "0d7d4a744fd92effd1ed88e48ac8231e7f7e9e6c", 1)'],
4296+
},
42034297
];

src/results/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export type RelBaseTypedValue =
6161
| Rational64Value
6262
| Rational128Value
6363
| AutoNumber
64-
| UUID;
64+
| UUID
65+
| SHA1;
6566

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

@@ -108,6 +109,7 @@ export type RelTypeDef =
108109
| Omit<Rational128Value, 'value'>
109110
| Omit<AutoNumber, 'value'>
110111
| Omit<UUID, 'value'>
112+
| Omit<SHA1, 'value'>
111113
| ConstantValue
112114
| Omit<ValueTypeValue, 'value'>
113115
| Omit<UnknownType, 'value'>;
@@ -352,6 +354,11 @@ export type UUID = {
352354
value: string;
353355
};
354356

357+
export type SHA1 = {
358+
type: 'SHA1';
359+
value: string;
360+
};
361+
355362
// TODO: should be removed with JSON based metadata implementation?
356363
export type UnknownType = {
357364
type: 'Unknown';

0 commit comments

Comments
 (0)