Skip to content

Commit 34808da

Browse files
committed
Assert array and vlen type values
1 parent 537eb48 commit 34808da

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

packages/app/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export {
148148
isComplexType,
149149
isPrintableType,
150150
isCompoundType,
151+
isArrayOrVlenType,
151152
hasType,
152153
hasStringType,
153154
hasIntegerType,

packages/shared/src/guards.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest';
33
import { assertScalarValue, assertValue } from './guards';
44
import {
55
arrayShape,
6+
arrayType,
67
boolType,
78
compoundType,
89
cplxType,
@@ -11,6 +12,7 @@ import {
1112
intType,
1213
scalarShape,
1314
strType,
15+
vlenType,
1416
} from './hdf5-utils';
1517
import { dataset } from './mock-utils';
1618

@@ -38,12 +40,21 @@ describe('assertScalarValue', () => {
3840
assertScalarValue([0, 0], cplxType(intType())),
3941
).not.toThrowError();
4042

43+
expect(() =>
44+
assertScalarValue([0, 0], arrayType(intType(), [2])),
45+
).not.toThrowError();
46+
47+
expect(() =>
48+
assertScalarValue([0], vlenType(floatType())),
49+
).not.toThrowError();
50+
4151
expect(() =>
4252
assertScalarValue(
43-
[0, ''],
53+
[0, '', [[]]],
4454
compoundType([
4555
['int', intType()],
4656
['str', strType()],
57+
['nested', compoundType([['vlen', vlenType(floatType())]])],
4758
]),
4859
),
4960
).not.toThrowError();

packages/shared/src/guards.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type Data, type NdArray, type TypedArray } from 'ndarray';
22

33
import {
44
type ArrayShape,
5+
type ArrayType,
56
type BooleanType,
67
type ComplexArray,
78
type ComplexType,
@@ -31,6 +32,7 @@ import {
3132
type ShapeClassMap,
3233
type StringType,
3334
type Value,
35+
type VLenType,
3436
} from './hdf5-models';
3537
import {
3638
type AnyNumArray,
@@ -637,6 +639,10 @@ export function assertPrintableCompoundType<O extends HasType<CompoundType>>(
637639
}
638640
}
639641

642+
export function isArrayOrVlenType(type: DType): type is ArrayType | VLenType {
643+
return type.class === DTypeClass.Array || type.class === DTypeClass.VLen;
644+
}
645+
640646
export function isComplexValue(
641647
type: DType,
642648
value: unknown,
@@ -665,6 +671,11 @@ export function assertScalarValue<T extends DType>(
665671
type.fields.values().forEach((fieldType, index) => {
666672
assertScalarValue(value[index], fieldType);
667673
});
674+
} else if (isArrayOrVlenType(type)) {
675+
assertArrayOrTypedArray(value);
676+
if (value.length > 0) {
677+
assertScalarValue(value[0], type.base);
678+
}
668679
}
669680
}
670681

0 commit comments

Comments
 (0)