Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include link metadata for h5wasm and fix unresolved entities #1101

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributes were undefined for "other" entities.

It was not caught by TS due to the type assertion as UnresolvedEntity that allows missing/additionnal field in an object.

To avoid such issues in the future, we should perhaps use type declaration rather than type assertion. See this simple example

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, the extra variable is annoying, but if it detects this sort of errors, it is indeed worth it.

}

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;
};
axelboc marked this conversation as resolved.
Show resolved Hide resolved
}

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