Skip to content

Commit 65fecfa

Browse files
committed
Document how to work with HDF5 attributes
1 parent f2e5e6b commit 65fecfa

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

packages/app/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,3 +608,47 @@ valuesStore.prefetch(ordinatesDataset);
608608
const abscissas = useValue(abscissasDataset);
609609
const ordinates = useValue(ordinatesDataset);
610610
```
611+
612+
To work with HDF5 attributes, retrieve an entity object with `useEntity` or
613+
`useDatasets` and pass it to `findAttribute`. Then, you can check or assert its
614+
type and shape and retrieve its value with `getAttributeValue`:
615+
616+
```ts
617+
const { attrValuesStore } = useDataContext();
618+
const entity = useEntity('/arrays/twoD'); // ProvidedEntity
619+
620+
// If you just want to know whether the attribute is present
621+
const hasAttr = hasAttribute(entity, 'my_attr'); // boolean
622+
623+
// Otherwise, find it
624+
const attribute = findAttribute(entity, 'my_attr'); // Attribute | undefined
625+
626+
// If the attribute must be present and have the expected shape and type, use type assertions
627+
assertDefined(attribute);
628+
assertArrayShape(attribute);
629+
assertStringType(attribute); // now `Attribute & HasShape<ArrayShape> & HasType<StringType>`
630+
631+
// Otherwise, use type guards and an `if` block
632+
if (
633+
isDefined(attribute) &&
634+
hasArrayShape(attribute) &&
635+
hasStringType(attribute)
636+
) {
637+
const someStr = getAttributeValue(entity, attribute, attrValuesStore); // string
638+
someStr.startWith('foo'); // `someStr` is fully type-checked; no need to use `typeof`
639+
}
640+
```
641+
642+
With scalar string and numeric attributes, use `findScalarStrAttr` and
643+
`findScalarNumAttr` for convenience:
644+
645+
```ts
646+
const strAttr = findScalarStrAttr(entity, 'my_str_attr');
647+
const numAttr = findScalarNumAttr(entity, 'my_num_attr');
648+
649+
assertDefined(strAttr); // or `isDefined` + `if` block
650+
assertDefined(numAttr);
651+
652+
const str = getAttributeValue(entity, strAttr, attrValuesStore); // string
653+
const num = getAttributeValue(entity, numAttr, attrValuesStore); // number | bigint
654+
```

packages/app/src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ export {
1212
buildBasicAuthHeader,
1313
} from './providers/utils';
1414

15-
export { enableBigIntSerialization } from './utils';
15+
export {
16+
enableBigIntSerialization,
17+
hasAttribute,
18+
findAttribute,
19+
findScalarNumAttr,
20+
findScalarStrAttr,
21+
getAttributeValue,
22+
} from './utils';
23+
1624
export { getFeedbackMailto } from './breadcrumbs/utils';
1725
export { AbortError } from '@h5web/shared/react-suspense-fetch';
1826

@@ -181,13 +189,6 @@ export { default as DataProvider } from './providers/DataProvider';
181189
export { DataProviderApi } from './providers/api';
182190
export { getValueOrError } from './providers/utils';
183191
export { useValuesInCache } from './hooks';
184-
export {
185-
hasAttribute,
186-
findAttribute,
187-
findScalarNumAttr,
188-
findScalarStrAttr,
189-
getAttributeValue,
190-
} from './utils';
191192
export { arrayDef, scalarDef, parseShape } from '@h5web/shared/hdf5-utils';
192193

193194
export { default as ValueFetcher } from './vis-packs/core/ValueFetcher';

0 commit comments

Comments
 (0)