From 2f951f44de56e439389a17b6cb90e7de66568e41 Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Wed, 4 Sep 2024 12:18:25 +0200 Subject: [PATCH] Assert compound values --- .../core/compound/MappedCompoundVis.tsx | 5 +++-- packages/app/src/vis-packs/core/matrix/utils.ts | 4 ++-- packages/shared/src/guards.ts | 8 +++++--- packages/shared/src/hdf5-models.ts | 14 +++++--------- packages/shared/src/hdf5-utils.ts | 16 +++++++--------- 5 files changed, 22 insertions(+), 25 deletions(-) diff --git a/packages/app/src/vis-packs/core/compound/MappedCompoundVis.tsx b/packages/app/src/vis-packs/core/compound/MappedCompoundVis.tsx index 7b9d0fa2e..12b9e67ae 100644 --- a/packages/app/src/vis-packs/core/compound/MappedCompoundVis.tsx +++ b/packages/app/src/vis-packs/core/compound/MappedCompoundVis.tsx @@ -1,8 +1,9 @@ import { MatrixVis } from '@h5web/lib'; import type { ArrayShape, + CompoundType, Dataset, - PrintableCompoundType, + PrintableType, ScalarShape, Value, } from '@h5web/shared/hdf5-models'; @@ -18,7 +19,7 @@ import { getCellWidth, getFormatter } from '../matrix/utils'; import { getSliceSelection } from '../utils'; interface Props { - dataset: Dataset; + dataset: Dataset>; value: Value; dimMapping: DimensionMapping; toolbarContainer: HTMLDivElement | undefined; diff --git a/packages/app/src/vis-packs/core/matrix/utils.ts b/packages/app/src/vis-packs/core/matrix/utils.ts index 03a2c44b5..c2f54a27a 100644 --- a/packages/app/src/vis-packs/core/matrix/utils.ts +++ b/packages/app/src/vis-packs/core/matrix/utils.ts @@ -8,8 +8,8 @@ import { import type { BooleanType, ComplexType, + CompoundType, NumericType, - PrintableCompoundType, PrintableType, } from '@h5web/shared/hdf5-models'; import { DTypeClass } from '@h5web/shared/hdf5-models'; @@ -69,7 +69,7 @@ export function getFormatter( } export function getCellWidth( - type: PrintableType | PrintableCompoundType, + type: PrintableType | CompoundType, ): number { if (type.class === DTypeClass.Compound) { return Math.max(...Object.values(type.fields).map(getCellWidth)); diff --git a/packages/shared/src/guards.ts b/packages/shared/src/guards.ts index d26ef51bb..dfbc4c737 100644 --- a/packages/shared/src/guards.ts +++ b/packages/shared/src/guards.ts @@ -17,7 +17,6 @@ import type { NumericLikeType, NumericType, Primitive, - PrintableCompoundType, PrintableType, ScalarShape, Shape, @@ -401,14 +400,14 @@ export function assertCompoundType( export function hasPrintableCompoundType( dataset: Dataset, -): dataset is Dataset { +): dataset is Dataset> { const { fields } = dataset.type; return Object.values(fields).every((f) => PRINTABLE_DTYPES.has(f.class)); } export function assertPrintableCompoundType( dataset: Dataset, -): asserts dataset is Dataset { +): asserts dataset is Dataset> { if (!hasPrintableCompoundType(dataset)) { throw new Error('Expected compound dataset to have printable types'); } @@ -435,6 +434,9 @@ function assertPrimitiveValue( assertComplex(value); } else if (isCompoundType(type)) { assertArray(value); + Object.values(type.fields).forEach((fieldType, index) => { + assertPrimitiveValue(fieldType, value[index]); + }); } } diff --git a/packages/shared/src/hdf5-models.ts b/packages/shared/src/hdf5-models.ts index 307f95b47..3d6980d88 100644 --- a/packages/shared/src/hdf5-models.ts +++ b/packages/shared/src/hdf5-models.ts @@ -157,15 +157,11 @@ export interface StringType { length?: number; } -export interface CompoundType< - F extends Record = Record, -> { +export interface CompoundType { class: DTypeClass.Compound; - fields: F; + fields: Record; } -export type PrintableCompoundType = CompoundType>; - export interface ArrayType { class: DTypeClass.Array | DTypeClass.VLen; base: T; @@ -211,13 +207,13 @@ export type Primitive = T extends NumericType | EnumType ? number | boolean // let providers choose : T extends ComplexType ? H5WebComplex - : T extends PrintableCompoundType - ? Primitive[] + : T extends CompoundType + ? Primitive[] : unknown; export type ArrayValue = | Primitive[] - | (T extends NumericType | BooleanType | EnumType ? TypedArray : never); + | (T extends NumericLikeType ? TypedArray : never); export type Value = D extends Dataset diff --git a/packages/shared/src/hdf5-utils.ts b/packages/shared/src/hdf5-utils.ts index d4dae41ae..ae16276f1 100644 --- a/packages/shared/src/hdf5-utils.ts +++ b/packages/shared/src/hdf5-utils.ts @@ -109,19 +109,17 @@ export function cplxType( return { class: DTypeClass.Complex, realType, imagType }; } -export function compoundType>( - fields: F, -): CompoundType { +export function compoundType( + fields: Record, +): CompoundType { return { class: DTypeClass.Compound, fields }; } -export const printableCompoundType = compoundType< - Record ->; +export const printableCompoundType = compoundType; -export function compoundOrCplxType>( - fields: F, -): CompoundType | ComplexType { +export function compoundOrCplxType( + fields: Record, +): CompoundType | ComplexType { const { r, i } = fields; if (r && isNumericType(r) && i && isNumericType(i)) { return cplxType(r, i);