Skip to content

Commit

Permalink
Merge pull request #1101 from silx-kit/h5wasm-link
Browse files Browse the repository at this point in the history
Include link metadata for h5wasm and fix unresolved entities
  • Loading branch information
loichuder authored May 9, 2022
2 parents bd6ca61 + 638efa8 commit e78c50d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 19 deletions.
31 changes: 19 additions & 12 deletions packages/app/src/providers/h5grove/h5grove-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
Group,
GroupWithChildren,
NumericType,
UnresolvedEntity,
} from '@h5web/shared';
import { hasScalarShape, buildEntityPath, EntityKind } from '@h5web/shared';
import { isString } from 'lodash';
Expand Down Expand Up @@ -145,30 +144,35 @@ export class H5GroveApi extends DataProviderApi {
path: string,
response: H5GroveEntityResponse
): Promise<Entity> {
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)) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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(
Expand Down
41 changes: 35 additions & 6 deletions packages/h5wasm/src/h5wasm-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,7 +19,9 @@ import {
assertH5WasmEntityWithAttrs,
convertMetadataToDType,
isH5WasmDataset,
isH5WasmExternalLink,
isH5WasmGroup,
isH5WasmSoftLink,
isHDF5,
convertSelectionToRanges,
} from './utils';
Expand Down Expand Up @@ -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[] {
Expand Down
19 changes: 18 additions & 1 deletion packages/h5wasm/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand Down

0 comments on commit e78c50d

Please sign in to comment.