Skip to content

Commit

Permalink
Normalise H5T enums and use them to initialise types
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Jun 4, 2024
1 parent b65b073 commit 3f8bf62
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 149 deletions.
20 changes: 10 additions & 10 deletions packages/app/src/providers/h5grove/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Endianness } from '@h5web/shared/hdf5-models';
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import {
arrayType,
bitfieldType,
Expand All @@ -23,35 +23,35 @@ import { parseDType } from './utils';
describe('parseDType', () => {
it('should convert integer types', () => {
expect(parseDType({ class: 0, size: 1, order: 0, sign: 1 })).toStrictEqual(
intType(8, Endianness.LE),
intType(8, H5T_ORDER.LE),
);
expect(parseDType({ class: 0, size: 8, order: 1, sign: 0 })).toStrictEqual(
uintType(64, Endianness.BE),
uintType(64, H5T_ORDER.BE),
);
});

it('should convert float types', () => {
expect(parseDType({ class: 1, size: 4, order: 0 })).toStrictEqual(
floatType(32, Endianness.LE),
floatType(32, H5T_ORDER.LE),
);
expect(parseDType({ class: 1, size: 8, order: 1 })).toStrictEqual(
floatType(64, Endianness.BE),
floatType(64, H5T_ORDER.BE),
);
});

it('should convert string types', () => {
expect(
parseDType({ class: 3, size: 6, cset: 0, vlen: false }),
).toStrictEqual(strType('ASCII', 6));
).toStrictEqual(strType(H5T_CSET.ASCII, 6));
expect(
parseDType({ class: 3, size: 6, cset: 0, vlen: true }),
).toStrictEqual(strType('ASCII'));
).toStrictEqual(strType(H5T_CSET.ASCII));
expect(
parseDType({ class: 3, size: 6, cset: 1, vlen: false }),
).toStrictEqual(strType('UTF-8', 6));
).toStrictEqual(strType(H5T_CSET.UTF8, 6));
expect(
parseDType({ class: 3, size: 6, cset: 1, vlen: true }),
).toStrictEqual(strType('UTF-8'));
).toStrictEqual(strType(H5T_CSET.UTF8));
});

it('should convert compound and complex types', () => {
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('parseDType', () => {
it('should convert other types', () => {
expect(parseDType({ class: 2, size: 1 })).toStrictEqual(timeType());
expect(parseDType({ class: 4, size: 1, order: 0 })).toStrictEqual(
bitfieldType(Endianness.LE),
bitfieldType(H5T_ORDER.LE),
);
expect(parseDType({ class: 5, size: 1, tag: 'foo' })).toStrictEqual(
opaqueType('foo'),
Expand Down
42 changes: 16 additions & 26 deletions packages/app/src/providers/h5grove/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { isNumericType } from '@h5web/shared/guards';
import { H5T_CLASS } from '@h5web/shared/h5t';
import type {
Attribute,
ChildEntity,
DType,
Group,
ProvidedEntity,
} from '@h5web/shared/hdf5-models';
import {
DTypeClass,
EntityKind,
H5TClass,
H5TSign,
} from '@h5web/shared/hdf5-models';
import { DTypeClass, EntityKind } from '@h5web/shared/hdf5-models';
import {
arrayType,
bitfieldType,
Expand All @@ -24,8 +20,6 @@ import {
referenceType,
strType,
timeType,
toCharSet,
toEndianness,
unknownType,
} from '@h5web/shared/hdf5-utils';
import type { TypedArrayConstructor } from '@h5web/shared/vis-models';
Expand Down Expand Up @@ -142,36 +136,32 @@ export function hasErrorMessage(error: unknown): error is { message: string } {
export function parseDType(type: H5GroveType): DType {
const { class: h5tClass, size } = type;

if (h5tClass === H5TClass.Integer) {
return intOrUintType(
type.sign === H5TSign.Signed,
size * 8,
toEndianness(type.order),
);
if (h5tClass === H5T_CLASS.INTEGER) {
return intOrUintType(type.sign, size * 8, type.order);
}

if (h5tClass === H5TClass.Float) {
return floatType(size * 8, toEndianness(type.order));
if (h5tClass === H5T_CLASS.FLOAT) {
return floatType(size * 8, type.order);
}

if (h5tClass === H5TClass.Time) {
if (h5tClass === H5T_CLASS.TIME) {
return timeType();
}

if (h5tClass === H5TClass.String) {
if (h5tClass === H5T_CLASS.STRING) {
const { cset, vlen } = type;
return strType(toCharSet(cset), vlen ? undefined : size);
return strType(cset, vlen ? undefined : size);
}

if (h5tClass === H5TClass.Bitfield) {
if (h5tClass === H5T_CLASS.BITFIELD) {
return bitfieldType();
}

if (h5tClass === H5TClass.Opaque) {
if (h5tClass === H5T_CLASS.OPAQUE) {
return opaqueType(type.tag);
}

if (h5tClass === H5TClass.Compound) {
if (h5tClass === H5T_CLASS.COMPOUND) {
return compoundOrCplxType(
Object.fromEntries(
Object.entries(type.members).map(([mName, mType]) => [
Expand All @@ -182,11 +172,11 @@ export function parseDType(type: H5GroveType): DType {
);
}

if (h5tClass === H5TClass.Reference) {
if (h5tClass === H5T_CLASS.REFERENCE) {
return referenceType();
}

if (h5tClass === H5TClass.Enum) {
if (h5tClass === H5T_CLASS.ENUM) {
const base = parseDType(type.base);
if (!isNumericType(base)) {
throw new Error('Expected enum type to have numeric base type');
Expand All @@ -195,11 +185,11 @@ export function parseDType(type: H5GroveType): DType {
return enumOrBoolType(base, type.members);
}

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

if (h5tClass === H5TClass.Array) {
if (h5tClass === H5T_CLASS.ARRAY) {
return arrayType(parseDType(type.base), type.dims);
}

Expand Down
16 changes: 9 additions & 7 deletions packages/app/src/providers/hsds/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import type { DType } from '@h5web/shared/hdf5-models';
import { Endianness } from '@h5web/shared/hdf5-models';
import {
arrayType,
boolType,
Expand Down Expand Up @@ -29,22 +29,22 @@ interface TestType {

const leInt = {
hsds: { class: 'H5T_INTEGER', base: 'H5T_STD_I8LE' },
hdf5: intType(8, Endianness.LE),
hdf5: intType(8, H5T_ORDER.LE),
} satisfies TestType;

const beUint = {
hsds: { class: 'H5T_INTEGER', base: 'H5T_STD_U64BE' },
hdf5: uintType(64, Endianness.BE),
hdf5: uintType(64, H5T_ORDER.BE),
} satisfies TestType;

const leFloat = {
hsds: { class: 'H5T_FLOAT', base: 'H5T_IEEE_F32LE' },
hdf5: floatType(32, Endianness.LE),
hdf5: floatType(32, H5T_ORDER.LE),
} satisfies TestType;

const beFloat = {
hsds: { class: 'H5T_FLOAT', base: 'H5T_IEEE_F64BE' },
hdf5: floatType(64, Endianness.BE),
hdf5: floatType(64, H5T_ORDER.BE),
} satisfies TestType;

describe('convertHsdsType', () => {
Expand All @@ -55,7 +55,9 @@ describe('convertHsdsType', () => {
length: 25,
};

expect(convertHsdsType(asciiStr)).toStrictEqual(strType('ASCII', 25));
expect(convertHsdsType(asciiStr)).toStrictEqual(
strType(H5T_CSET.ASCII, 25),
);
});

it('should convert variable-length UTF-8 string type', () => {
Expand All @@ -65,7 +67,7 @@ describe('convertHsdsType', () => {
length: 'H5T_VARIABLE',
};

expect(convertHsdsType(unicodeStr)).toStrictEqual(strType('UTF-8'));
expect(convertHsdsType(unicodeStr)).toStrictEqual(strType(H5T_CSET.UTF8));
});

it('should convert integer types', () => {
Expand Down
23 changes: 14 additions & 9 deletions packages/app/src/providers/hsds/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertArray, isGroup } from '@h5web/shared/guards';
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import type {
ArrayShape,
Attribute,
Expand All @@ -14,7 +15,7 @@ import type {
ScalarShape,
Shape,
} from '@h5web/shared/hdf5-models';
import { DTypeClass, Endianness } from '@h5web/shared/hdf5-models';
import { DTypeClass } from '@h5web/shared/hdf5-models';
import {
boolType,
compoundType,
Expand Down Expand Up @@ -81,19 +82,19 @@ function convertHsdsNumericType(hsdsType: HsdsNumericType): NumericType {
throw new Error(`Unrecognized base ${base}`);
}

const [, sign, sizeStr, endiannessAbbr] = matches;
const [, sign, sizeStr, h5tOrderStr] = matches;
const size = Number.parseInt(sizeStr, 10);
const endianness = Endianness[endiannessAbbr as 'BE' | 'LE'];
const h5tOrder = H5T_ORDER[h5tOrderStr as keyof typeof H5T_ORDER];

if (hsdsClass === 'H5T_FLOAT') {
return floatType(size, endianness);
return floatType(size, h5tOrder);
}

if (sign === 'U') {
return uintType(size, endianness);
return uintType(size, h5tOrder);
}

return intType(size, endianness);
return intType(size, h5tOrder);
}

function convertHsdsCompoundType(
Expand Down Expand Up @@ -136,11 +137,15 @@ export function convertHsdsType(hsdsType: HsdsType): DType {
case 'H5T_COMPOUND':
return convertHsdsCompoundType(hsdsType);

case 'H5T_STRING':
case 'H5T_STRING': {
const { charSet, length } = hsdsType;
return strType(
hsdsType.charSet.endsWith('ASCII') ? 'ASCII' : 'UTF-8',
hsdsType.length === 'H5T_VARIABLE' ? undefined : hsdsType.length,
H5T_CSET[
charSet.slice(charSet.lastIndexOf('_') + 1) as keyof typeof H5T_CSET
],
length === 'H5T_VARIABLE' ? undefined : length,
);
}

case 'H5T_ARRAY':
case 'H5T_VLEN':
Expand Down
11 changes: 9 additions & 2 deletions packages/app/src/providers/mock/mock-file.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { H5T_CSET } from '@h5web/shared/h5t';
import type { GroupWithChildren } from '@h5web/shared/hdf5-models';
import {
arrayType,
Expand Down Expand Up @@ -60,10 +61,16 @@ export function makeMockFile(): GroupWithChildren {
attributes: [scalarAttr('attr', cplx(1, 5))],
}),
scalar('scalar_compound', ['foo', 2], {
type: compoundType({ str: strType('ASCII', 3), int: intType(8) }),
type: compoundType({
str: strType(H5T_CSET.ASCII, 3),
int: intType(8),
}),
attributes: [
scalarAttr('attr', ['foo', 2], {
type: compoundType({ str: strType('UTF-8'), int: intType(8) }),
type: compoundType({
str: strType(H5T_CSET.UTF8),
int: intType(8),
}),
}),
],
}),
Expand Down
Loading

0 comments on commit 3f8bf62

Please sign in to comment.