From 638efa84c45fa2dadc219520e28fb9b345600a1a Mon Sep 17 00:00:00 2001 From: Loic Huder Date: Thu, 5 May 2022 14:45:59 +0200 Subject: [PATCH] Include link metadata for h5wasm and fix unresolved entities --- .../app/src/providers/h5grove/h5grove-api.ts | 31 ++++++++------ packages/h5wasm/src/h5wasm-api.ts | 41 ++++++++++++++++--- packages/h5wasm/src/utils.ts | 19 ++++++++- 3 files changed, 72 insertions(+), 19 deletions(-) diff --git a/packages/app/src/providers/h5grove/h5grove-api.ts b/packages/app/src/providers/h5grove/h5grove-api.ts index 2802fbeb4..e537152cc 100644 --- a/packages/app/src/providers/h5grove/h5grove-api.ts +++ b/packages/app/src/providers/h5grove/h5grove-api.ts @@ -7,7 +7,6 @@ import type { Group, GroupWithChildren, NumericType, - UnresolvedEntity, } from '@h5web/shared'; import { hasScalarShape, buildEntityPath, EntityKind } from '@h5web/shared'; import { isString } from 'lodash'; @@ -145,30 +144,35 @@ export class H5GroveApi extends DataProviderApi { path: string, response: H5GroveEntityResponse ): Promise { - const { name, type: kind } = response; + const { name } = response; - const baseEntity = { name, path, kind }; + const baseEntity = { name, path }; if (isGroupResponse(response)) { const { children, attributes: attrsMetadata } = response; const attributes = await this.processAttrsMetadata(path, attrsMetadata); + const baseGroup: Group = { + ...baseEntity, + kind: EntityKind.Group, + attributes, + }; if (!children) { /* `/meta` stops at one nesting level * (i.e. children of child groups are not returned) */ - return { ...baseEntity, attributes } as Group; + return baseGroup; } - return { - ...baseEntity, - attributes, + const groupWithChildren: GroupWithChildren = { + ...baseGroup, // Fetch attribute values of any child groups in parallel children: await Promise.all( children.map((child) => this.processEntityResponse(buildEntityPath(path, child.name), child) ) ), - } as GroupWithChildren; + }; + return groupWithChildren; } if (isDatasetResponse(response)) { @@ -180,16 +184,18 @@ export class H5GroveApi extends DataProviderApi { filters, } = response; const attributes = await this.processAttrsMetadata(path, attrsMetadata); - - return { + const dataset: Dataset = { ...baseEntity, attributes, + kind: EntityKind.Dataset, shape, type: convertDtype(dtype), rawType: dtype, ...(chunks && { chunks }), ...(filters && { filters }), - } as Dataset; + }; + + return dataset; } if (isSoftLinkResponse(response)) { @@ -221,8 +227,9 @@ export class H5GroveApi extends DataProviderApi { // Treat 'other' entities as unresolved return { ...baseEntity, + attributes: [], kind: EntityKind.Unresolved, - } as UnresolvedEntity; + }; } private async processAttrsMetadata( diff --git a/packages/h5wasm/src/h5wasm-api.ts b/packages/h5wasm/src/h5wasm-api.ts index 6dc3cf740..91de707c4 100644 --- a/packages/h5wasm/src/h5wasm-api.ts +++ b/packages/h5wasm/src/h5wasm-api.ts @@ -8,7 +8,6 @@ import type { Group, GroupWithChildren, Shape, - UnresolvedEntity, } from '@h5web/shared'; import { assertNonNull, buildEntityPath, EntityKind } from '@h5web/shared'; import type { Attribute as H5WasmAttribute } from 'h5wasm'; @@ -20,7 +19,9 @@ import { assertH5WasmEntityWithAttrs, convertMetadataToDType, isH5WasmDataset, + isH5WasmExternalLink, isH5WasmGroup, + isH5WasmSoftLink, isHDF5, convertSelectionToRanges, } from './utils'; @@ -123,27 +124,55 @@ export class H5WasmApi extends ProviderApi { const childPath = buildEntityPath(path, name); return this.processH5WasmEntity(name, childPath, h5wChild, true); }); + const groupWithChildren: GroupWithChildren = { ...baseGroup, children }; - return { ...baseGroup, children } as GroupWithChildren; + return groupWithChildren; } if (isH5WasmDataset(h5wEntity)) { const { shape, metadata, dtype } = h5wEntity; - - return { + const dataset: Dataset = { ...baseEntity, kind: EntityKind.Dataset, attributes: this.processH5WasmAttrs(h5wEntity.attrs), shape, type: convertMetadataToDType(metadata), rawType: dtype, - } as Dataset; + }; + + return dataset; + } + + if (isH5WasmSoftLink(h5wEntity)) { + return { + ...baseEntity, + attributes: [], + kind: EntityKind.Unresolved, + link: { + class: 'Soft', + path: h5wEntity.target, + }, + }; + } + + if (isH5WasmExternalLink(h5wEntity)) { + return { + ...baseEntity, + attributes: [], + kind: EntityKind.Unresolved, + link: { + class: 'External', + path: h5wEntity.obj_path, + file: h5wEntity.filename, + }, + }; } return { ...baseEntity, + attributes: [], kind: EntityKind.Unresolved, - } as UnresolvedEntity; + }; } private processH5WasmAttrs(h5wAttrs: H5WasmAttributes): Attribute[] { diff --git a/packages/h5wasm/src/utils.ts b/packages/h5wasm/src/utils.ts index 069b9a301..05c1d7f3e 100644 --- a/packages/h5wasm/src/utils.ts +++ b/packages/h5wasm/src/utils.ts @@ -1,6 +1,11 @@ import type { DType } from '@h5web/shared'; import { DTypeClass, Endianness } from '@h5web/shared'; -import { Dataset as H5WasmDataset, Group as H5WasmGroup } from 'h5wasm'; +import { + BrokenSoftLink as H5WasmSoftLink, + Dataset as H5WasmDataset, + ExternalLink as H5WasmExternalLink, + Group as H5WasmGroup, +} from 'h5wasm'; import type { Metadata } from 'h5wasm/src/hdf5_util_helpers'; import type { H5WasmEntity } from './models'; @@ -22,6 +27,18 @@ export function isH5WasmDataset(entity: H5WasmEntity): entity is H5WasmDataset { return entity instanceof H5WasmDataset; } +export function isH5WasmSoftLink( + entity: H5WasmEntity +): entity is H5WasmSoftLink { + return entity instanceof H5WasmSoftLink; +} + +export function isH5WasmExternalLink( + entity: H5WasmEntity +): entity is H5WasmExternalLink { + return entity instanceof H5WasmExternalLink; +} + export function assertH5WasmDataset( entity: H5WasmEntity ): asserts entity is H5WasmDataset {