Skip to content

Commit

Permalink
feat(DataSource): simplified data structure
Browse files Browse the repository at this point in the history
DataSource objects now represent a single type, rather than the
bag-of-types done originally.
  • Loading branch information
floryst committed Aug 8, 2024
1 parent f59f77d commit af41f54
Show file tree
Hide file tree
Showing 26 changed files with 356 additions and 526 deletions.
157 changes: 72 additions & 85 deletions src/io/import/__tests__/dataSource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,101 +1,88 @@
import { describe, it } from 'vitest';
import { expect } from 'chai';
import { DataSource, serializeDataSource } from '@/src/io/import/dataSource';
import {
getDataSourceName,
isRemoteDataSource,
} from '@/src/io/import/dataSource';
import { Chunk } from '@/src/core/streaming/chunk';

describe('serializeDataSource', () => {
it('should remove FileSources', () => {
const input: DataSource = {
fileSrc: {
file: new File([], '1.dcm'),
fileType: 'application/dicom',
},
};
const output = serializeDataSource(input);
describe('isRemoteDatasource', () => {
it('should work', () => {
expect(isRemoteDataSource(undefined)).to.be.false;

expect(output).to.deep.equal({});
});
expect(
isRemoteDataSource({
type: 'file',
file: new File([], 'name'),
fileType: 'type',
})
).to.be.false;

it('should preserve archive status', () => {
const input: DataSource = {
fileSrc: {
file: new File([], '1.dcm'),
fileType: 'application/dicom',
},
archiveSrc: {
path: 'a/b/c',
},
parent: {
fileSrc: {
file: new File([], 'archive.zip'),
fileType: 'application/zip',
expect(
isRemoteDataSource({
type: 'file',
file: new File([], 'name'),
fileType: 'type',
parent: {
type: 'uri',
uri: 'http://',
name: 'name',
},
},
};
const output = serializeDataSource(input);

expect(output).to.deep.equal({
archiveSrc: {
path: 'a/b/c',
},
parent: {},
});
})
).to.be.true;
});
});

it('should preserve UriSource', () => {
const input: DataSource = {
uriSrc: {
uri: 'https://example.com/image.jpg',
name: 'image.jpg',
},
parent: {
uriSrc: {
uri: 's3://example/bucket',
name: '',
},
},
};
const output = serializeDataSource(input);
describe('getDataSourceName', () => {
it('should work', () => {
expect(
getDataSourceName({
type: 'file',
file: new File([], 'name'),
fileType: 'ft',
})
).to.equal('name');

expect(output).to.deep.equal(input);
});
expect(
getDataSourceName({
type: 'uri',
uri: 'http://',
name: 'name',
})
).to.equal('name');

it('should serialize remote archive members', () => {
const input: DataSource = {
fileSrc: {
file: new File([], '1.dcm'),
fileType: 'application/dicom',
},
archiveSrc: {
path: 'a/b/c',
},
parent: {
fileSrc: {
file: new File([], 'archive.zip'),
fileType: 'application/zip',
},
parent: {
uriSrc: {
uri: 'https://example.com/archive.zip',
name: 'archive.zip',
expect(
getDataSourceName({
type: 'collection',
sources: [
{
type: 'file',
file: new File([], 'name'),
fileType: 'ft',
},
},
},
};
const output = serializeDataSource(input);
],
})
).to.equal('name');

expect(output).to.deep.equal({
archiveSrc: {
path: 'a/b/c',
},
parent: {
// empty parent b/c archive FileSource cannot be serialized
expect(
getDataSourceName({
type: 'chunk',
chunk: {} as Chunk,
mime: 'mime',
})
).to.equal(null);

expect(
getDataSourceName({
type: 'chunk',
chunk: {} as Chunk,
mime: 'mime',
parent: {
uriSrc: {
uri: 'https://example.com/archive.zip',
name: 'archive.zip',
},
type: 'file',
file: new File([], 'name'),
fileType: 'ft',
},
},
});
})
).to.equal('name');
});
});
22 changes: 0 additions & 22 deletions src/io/import/__tests__/importDataSources.spec.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/io/import/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ export type ImportHandler = ChainHandler<
ImportContext
>;

export function isArchive(
ds: DataSource
): ds is DataSource & { fileSrc: FileSource } {
return !!ds.fileSrc && ARCHIVE_FILE_TYPES.has(ds.fileSrc.fileType);
export function isArchive(ds: DataSource): ds is FileSource {
return ds.type === 'file' && ARCHIVE_FILE_TYPES.has(ds.fileType);
}

export function isLoadableResult(
Expand Down
Loading

0 comments on commit af41f54

Please sign in to comment.