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

Refactor providers to notably remove unneeded type guards #1555

Merged
merged 6 commits into from
Jan 29, 2024
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
125 changes: 2 additions & 123 deletions packages/app/src/providers/h5grove/h5grove-api.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
import { hasNumericType, hasScalarShape } from '@h5web/shared/guards';
import type {
ArrayShape,
Attribute,
AttributeValues,
ChildEntity,
Dataset,
Entity,
Group,
ProvidedEntity,
Value,
} from '@h5web/shared/hdf5-models';
import { EntityKind } from '@h5web/shared/hdf5-models';
import { buildEntityPath } from '@h5web/shared/hdf5-utils';
import type { AxiosRequestConfig } from 'axios';

import { DataProviderApi } from '../api';
import type { ExportFormat, ExportURL, ValuesStoreParams } from '../models';
import { handleAxiosError, typedArrayFromDType } from '../utils';
import type {
H5GroveAttribute,
H5GroveAttrValuesResponse,
H5GroveDataResponse,
H5GroveEntityResponse,
H5GrovePathsResponse,
} from './models';
import {
convertH5GroveDtype,
hasErrorMessage,
isDatasetResponse,
isExternalLinkResponse,
isGroupResponse,
isSoftLinkResponse,
} from './utils';
import { hasErrorMessage, parseEntity } from './utils';

export class H5GroveApi extends DataProviderApi {
/* API compatible with [email protected] */
Expand All @@ -46,7 +33,7 @@ export class H5GroveApi extends DataProviderApi {

public async getEntity(path: string): Promise<ProvidedEntity> {
const response = await this.fetchEntity(path);
return this.processEntityResponse(path, response);
return parseEntity(path, response);
}

public async getValue(
Expand Down Expand Up @@ -173,112 +160,4 @@ export class H5GroveApi extends DataProviderApi {

return data;
}

private async processEntityResponse(
path: string,
response: H5GroveEntityResponse,
isChild: true,
): Promise<ChildEntity>;

private async processEntityResponse(
path: string,
response: H5GroveEntityResponse,
isChild?: false,
): Promise<ProvidedEntity>;

private async processEntityResponse(
path: string,
response: H5GroveEntityResponse,
isChild = false,
): Promise<ProvidedEntity | ChildEntity> {
const { name } = response;
const baseEntity = { name, path };

if (isGroupResponse(response)) {
const { children = [], attributes: attrsMetadata } = response;
const attributes = await this.processAttrsMetadata(attrsMetadata);
const baseGroup: Group = {
...baseEntity,
kind: EntityKind.Group,
attributes,
};

if (isChild) {
return baseGroup;
}

return {
...baseGroup,
// Fetch attribute values of any child groups in parallel
children: await Promise.all(
children.map((child) => {
const childPath = buildEntityPath(path, child.name);
return this.processEntityResponse(childPath, child, true);
}),
),
};
}

if (isDatasetResponse(response)) {
const {
attributes: attrsMetadata,
dtype,
shape,
chunks,
filters,
} = response;
const attributes = await this.processAttrsMetadata(attrsMetadata);
return {
...baseEntity,
attributes,
kind: EntityKind.Dataset,
shape,
type: convertH5GroveDtype(dtype),
rawType: dtype,
...(chunks && { chunks }),
...(filters && { filters }),
};
}

if (isSoftLinkResponse(response)) {
const { target_path } = response;
return {
...baseEntity,
attributes: [],
kind: EntityKind.Unresolved,
link: { class: 'Soft', path: target_path },
};
}

if (isExternalLinkResponse(response)) {
const { target_file, target_path } = response;
return {
...baseEntity,
kind: EntityKind.Unresolved,
attributes: [],
link: {
class: 'External',
file: target_file,
path: target_path,
},
};
}

// Treat 'other' entities as unresolved
return {
...baseEntity,
attributes: [],
kind: EntityKind.Unresolved,
};
}

private async processAttrsMetadata(
attrsMetadata: H5GroveAttribute[],
): Promise<Attribute[]> {
return attrsMetadata.map<Attribute>(({ name, dtype, shape }) => ({
name,
shape,
type: convertH5GroveDtype(dtype),
}));
}
}
51 changes: 24 additions & 27 deletions packages/app/src/providers/h5grove/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,52 @@ import type {
Filter,
} from '@h5web/shared/hdf5-models';

export interface H5GroveEntityResponse {
export type H5GroveEntityResponse = H5GroveEntity;
export type H5GroveDataResponse = unknown;
export type H5GroveAttrValuesResponse = AttributeValues;
export type H5GrovePathsResponse = string[];

export type H5GroveEntity =
| H5GroveGroup
| H5GroveDataset
| H5GroveSoftLink
| H5GroveExternalLink;

export interface H5GroveBaseEntity {
name: string;
type:
| EntityKind.Dataset
| EntityKind.Group
| 'external_link'
| 'soft_link'
| 'other';
type: string;
}

export type H5GroveDtype =
| string
| {
[k: string]: H5GroveDtype;
};
export interface H5GroveGroup extends H5GroveBaseEntity {
type: EntityKind.Group;
children?: H5GroveEntity[];
attributes: H5GroveAttribute[];
}

export interface H5GroveDatasetResponse extends H5GroveEntityResponse {
export interface H5GroveDataset extends H5GroveBaseEntity {
type: EntityKind.Dataset;
dtype: H5GroveDtype;
shape: number[];
attributes: H5GroveAttribute[];
dtype: H5GroveDtype;
chunks: number[] | null;
filters: Filter[] | null;
}

export interface H5GroveGroupResponse extends H5GroveEntityResponse {
type: EntityKind.Group;
children?: H5GroveEntityResponse[];
attributes: H5GroveAttribute[];
}

export interface H5GroveSoftLinkResponse extends H5GroveEntityResponse {
export interface H5GroveSoftLink extends H5GroveBaseEntity {
type: 'soft_link';
target_path: string;
}

export interface H5GroveExternalLinkResponse extends H5GroveEntityResponse {
export interface H5GroveExternalLink extends H5GroveBaseEntity {
type: 'external_link';
target_file: string;
target_path: string;
}

export interface H5GroveAttribute {
dtype: H5GroveDtype;
name: string;
shape: number[];
dtype: H5GroveDtype;
}

export type H5GroveAttrValuesResponse = AttributeValues;
export type H5GroveDataResponse = unknown;

export type H5GrovePathsResponse = string[];
export type H5GroveDtype = string | { [k: string]: H5GroveDtype };
36 changes: 13 additions & 23 deletions packages/app/src/providers/h5grove/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,43 @@ import {
} from '@h5web/shared/hdf5-utils';
import { describe, expect, it } from 'vitest';

import { convertH5GroveDtype } from './utils';
import { parseDType } from './utils';

describe('convertH5GroveDtype', () => {
describe('parseDType', () => {
it('should convert integer dtypes', () => {
expect(convertH5GroveDtype('<i4')).toStrictEqual(
intType(32, Endianness.LE),
);
expect(convertH5GroveDtype('>u8')).toStrictEqual(
uintType(64, Endianness.BE),
);
expect(parseDType('<i4')).toStrictEqual(intType(32, Endianness.LE));
expect(parseDType('>u8')).toStrictEqual(uintType(64, Endianness.BE));
});

it('should convert float dtypes', () => {
expect(convertH5GroveDtype('<f4')).toStrictEqual(
floatType(32, Endianness.LE),
);
expect(convertH5GroveDtype('>f8')).toStrictEqual(
floatType(64, Endianness.BE),
);
expect(parseDType('<f4')).toStrictEqual(floatType(32, Endianness.LE));
expect(parseDType('>f8')).toStrictEqual(floatType(64, Endianness.BE));
});

it('should convert complex dtypes', () => {
expect(convertH5GroveDtype('<c8')).toStrictEqual(
expect(parseDType('<c8')).toStrictEqual(
cplxType(floatType(32, Endianness.LE), floatType(32, Endianness.LE)),
);
});

it('should convert bytes string dtypes', () => {
expect(convertH5GroveDtype('|S6')).toStrictEqual(strType('ASCII', 6));
expect(parseDType('|S6')).toStrictEqual(strType('ASCII', 6));
});

it('should interpret objects as strings', () => {
expect(convertH5GroveDtype('|O')).toStrictEqual(strType('UTF-8'));
expect(parseDType('|O')).toStrictEqual(strType('UTF-8'));
});

it('should interpret |b1 as booleans', () => {
expect(convertH5GroveDtype('|b1')).toStrictEqual(boolType());
expect(parseDType('|b1')).toStrictEqual(boolType());
});

it('should handle "not applicable" endianness symbol', () => {
expect(convertH5GroveDtype('|f8')).toStrictEqual(floatType(64));
expect(parseDType('|f8')).toStrictEqual(floatType(64));
});

it('should convert compound dtype', () => {
expect(
convertH5GroveDtype({ country: '|S10', population: '<i4' }),
).toStrictEqual(
expect(parseDType({ country: '|S10', population: '<i4' })).toStrictEqual(
compoundType({
country: strType('ASCII', 10),
population: intType(32, Endianness.LE),
Expand All @@ -66,6 +56,6 @@ describe('convertH5GroveDtype', () => {
});

it('should handle unknown type', () => {
expect(convertH5GroveDtype('>notAType')).toStrictEqual(unknownType());
expect(parseDType('>notAType')).toStrictEqual(unknownType());
});
});
Loading
Loading