Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split VLenType out of ArrayType #1748

Merged
merged 1 commit into from
Feb 3, 2025
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
7 changes: 5 additions & 2 deletions packages/app/src/providers/h5grove/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
strType,
timeType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import { describe, expect, it } from 'vitest';

Expand Down Expand Up @@ -94,15 +95,17 @@ describe('parseDType', () => {
).toStrictEqual(boolType(intType(true, 8)));
});

it('should convert array types', () => {
it('should convert vlen type', () => {
expect(
parseDType({
class: 9,
size: 1,
base: { class: 1, size: 4, order: 0 },
}),
).toStrictEqual(arrayType(floatType()));
).toStrictEqual(vlenType(floatType()));
});

it('should convert array type', () => {
expect(
parseDType({
class: 10,
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/providers/h5grove/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
strType,
timeType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import {
type BigIntTypedArrayConstructor,
Expand Down Expand Up @@ -214,7 +215,7 @@ export function parseDType(type: H5GroveType): DType {
}

if (h5tClass === H5T_CLASS.VLEN) {
return arrayType(parseDType(type.base));
return vlenType(parseDType(type.base));
}

// `H5GroveType` doesn't allow for any other `h5tClass` value at this point,
Expand Down
10 changes: 8 additions & 2 deletions packages/app/src/providers/hsds/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface HsdsShape {
export type HsdsType =
| HsdsNumericType
| HsdsStringType
| HsdsVLenType
| HsdsArrayType
| HsdsCompoundType
| HsdsEnumType;
Expand All @@ -110,10 +111,15 @@ export interface HsdsStringType {
length: number | 'H5T_VARIABLE';
}

export interface HsdsVLenType {
class: 'H5T_VLEN';
base: HsdsType;
}

export interface HsdsArrayType {
class: 'H5T_ARRAY' | 'H5T_VLEN';
class: 'H5T_ARRAY';
base: HsdsType;
dims?: number[];
dims: number[];
}

export interface HsdsCompoundType {
Expand Down
27 changes: 13 additions & 14 deletions packages/app/src/providers/hsds/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
intType,
strType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import { describe, expect, it } from 'vitest';

Expand All @@ -18,6 +19,7 @@ import {
type HsdsEnumType,
type HsdsStringType,
type HsdsType,
type HsdsVLenType,
} from './models';
import { convertHsdsType } from './utils';

Expand Down Expand Up @@ -83,30 +85,27 @@ describe('convertHsdsType', () => {
expect(convertHsdsType(beFloat.hsds)).toStrictEqual(beFloat.hdf5);
});

it('should convert the base of Array type', () => {
const arr: HsdsArrayType = {
class: 'H5T_ARRAY',
it('should convert the base of VLen type', () => {
const vlen: HsdsVLenType = {
class: 'H5T_VLEN',
base: leInt.hsds,
dims: [4, 5],
};

expect(convertHsdsType(arr)).toStrictEqual(arrayType(leInt.hdf5, [4, 5]));
expect(convertHsdsType(vlen)).toStrictEqual(vlenType(leInt.hdf5));
});

it('should convert the base of VLen type', () => {
const vlen: HsdsArrayType = {
class: 'H5T_VLEN',
it('should convert the base of Array type', () => {
const arr: HsdsArrayType = {
class: 'H5T_ARRAY',
base: leInt.hsds,
dims: [4, 5],
};

expect(convertHsdsType(vlen)).toStrictEqual(arrayType(leInt.hdf5));
expect(convertHsdsType(arr)).toStrictEqual(arrayType(leInt.hdf5, [4, 5]));
});

it('should convert the field types of Compound type', () => {
const vlen: HsdsArrayType = {
class: 'H5T_VLEN',
base: leInt.hsds,
};
const vlen: HsdsVLenType = { class: 'H5T_VLEN', base: leInt.hsds };
const compound: HsdsCompoundType = {
class: 'H5T_COMPOUND',
fields: [
Expand All @@ -117,7 +116,7 @@ describe('convertHsdsType', () => {
expect(convertHsdsType(compound)).toStrictEqual(
compoundType({
f1: beFloat.hdf5,
f2: arrayType(leInt.hdf5),
f2: vlenType(leInt.hdf5),
}),
);
});
Expand Down
14 changes: 6 additions & 8 deletions packages/app/src/providers/hsds/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
type CompoundType,
type Dataset,
type DType,
DTypeClass,
type Entity,
type EnumType,
type Group,
Expand All @@ -17,13 +16,15 @@ import {
type Shape,
} from '@h5web/shared/hdf5-models';
import {
arrayType,
compoundType,
cplxType,
enumOrBoolType,
floatType,
intType,
strType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';

import {
Expand Down Expand Up @@ -139,14 +140,11 @@ export function convertHsdsType(hsdsType: HsdsType): DType {
);
}

case 'H5T_ARRAY':
case 'H5T_VLEN':
return {
class:
hsdsType.class === 'H5T_ARRAY' ? DTypeClass.Array : DTypeClass.VLen,
base: convertHsdsType(hsdsType.base),
...(hsdsType.dims && { dims: hsdsType.dims }),
};
return vlenType(convertHsdsType(hsdsType.base));

case 'H5T_ARRAY':
return arrayType(convertHsdsType(hsdsType.base), hsdsType.dims);

case 'H5T_ENUM':
return convertHsdsEnumType(hsdsType);
Expand Down
5 changes: 3 additions & 2 deletions packages/app/src/providers/mock/mock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
printableCompoundType,
strType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import {
array,
Expand Down Expand Up @@ -87,9 +88,9 @@ export function makeMockFile(): GroupWithChildren {
],
}),
scalar('scalar_vlen', [1, 2, 3], {
type: arrayType(intType()),
type: vlenType(intType()),
attributes: [
scalarAttr('attr', [1, 2, 3], { type: arrayType(intType()) }),
scalarAttr('attr', [1, 2, 3], { type: vlenType(intType()) }),
],
}),
scalar('scalar_enum', 2, {
Expand Down
3 changes: 2 additions & 1 deletion packages/h5wasm/src/worker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
strType,
timeType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import {
type Dataset as H5WasmDataset,
Expand Down Expand Up @@ -279,7 +280,7 @@ function parseDType(metadata: Metadata): DType {
if (h5tClass === H5T_CLASS.VLEN) {
const { vlen_type } = metadata;
assertDefined(vlen_type);
return arrayType(parseDType(vlen_type));
return vlenType(parseDType(vlen_type));
}

if (h5tClass === H5T_CLASS.ARRAY) {
Expand Down
10 changes: 8 additions & 2 deletions packages/shared/src/hdf5-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export type DType =
| PrintableType
| CompoundType
| ArrayType
| VLenType
| TimeType
| BitfieldType
| OpaqueType
Expand Down Expand Up @@ -166,9 +167,14 @@ export interface CompoundType<T extends DType = DType> {
}

export interface ArrayType<T extends DType = DType> {
class: DTypeClass.Array | DTypeClass.VLen;
class: DTypeClass.Array;
base: T;
dims: number[];
}

export interface VLenType<T extends DType = DType> {
class: DTypeClass.VLen;
base: T;
dims?: number[];
}

export interface BooleanType {
Expand Down
13 changes: 7 additions & 6 deletions packages/shared/src/hdf5-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
type StringType,
type TimeType,
type UnknownType,
type VLenType,
} from './hdf5-models';

export function getChildEntity(
Expand Down Expand Up @@ -114,15 +115,15 @@ export function compoundOrCplxType<T extends DType>(
return compoundType(fields);
}

export function vlenType<T extends DType>(baseType: T): VLenType<T> {
return { class: DTypeClass.VLen, base: baseType };
}

export function arrayType<T extends DType>(
baseType: T,
dims?: number[],
dims: number[],
): ArrayType<T> {
return {
class: dims ? DTypeClass.Array : DTypeClass.VLen,
base: baseType,
...(dims && { dims }),
};
return { class: DTypeClass.Array, base: baseType, dims };
}

export function boolType(baseType: NumericType): BooleanType {
Expand Down