Skip to content

Commit

Permalink
Support json metadata in all providers
Browse files Browse the repository at this point in the history
  • Loading branch information
twojtasz committed Oct 30, 2019
1 parent 1a23c38 commit 35db013
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
5 changes: 5 additions & 0 deletions modules/io/src/io/fs-source.js
Expand Up @@ -30,5 +30,10 @@ export class FileSource {
return undefined;
}

existsSync(name) {
const path = this.path.join(this.root, name);
return this.fs.existsSync(path);
}

close() {}
}
4 changes: 4 additions & 0 deletions modules/io/src/io/memory-source-sink.js
Expand Up @@ -34,6 +34,10 @@ export class MemorySourceSink {
}
}

existsSync(name) {
return this.data.has(name);
}

close() {}

entries() {
Expand Down
7 changes: 6 additions & 1 deletion modules/io/src/providers/xviz-base-provider.js
Expand Up @@ -64,7 +64,12 @@ export class XVIZBaseProvider {
const {startTime, endTime} = this.reader.timeRange();
this.metadata = this._readMetadata();

if (this.metadata && Number.isFinite(startTime) && Number.isFinite(endTime)) {
if (
this.metadata &&
Number.isFinite(startTime) &&
Number.isFinite(endTime) &&
this.reader.checkMessage(0) // verify the first message exists
) {
this._valid = true;
}

Expand Down
21 changes: 16 additions & 5 deletions modules/io/src/readers/xviz-base-reader.js
Expand Up @@ -31,7 +31,11 @@ export class XVIZBaseReader {

readMetadata() {
if (this.source) {
return this.source.readSync(this._xvizMessage(1));
let data = this.source.readSync(this._xvizMessage(1));
if (!data) {
data = this.source.readSync(this._xvizMessage(1, {forceJson: true}));
}
return data;
}

return undefined;
Expand All @@ -46,6 +50,14 @@ export class XVIZBaseReader {
return undefined;
}

checkMessage(messageIndex) {
if (this.source) {
return this.source.existsSync(this._xvizMessage(2 + messageIndex));
}

return false;
}

timeRange() {
if (this.index) {
const {startTime, endTime} = this.index;
Expand Down Expand Up @@ -112,10 +124,9 @@ export class XVIZBaseReader {
}

// Support various formatted message names
_xvizMessage(index) {
if (index === 0) {
// index file is always json
return `0-frame.json`;
_xvizMessage(index, {forceJson = false} = {}) {
if (index === 0 || forceJson) {
return `${index}-frame.json`;
}

return `${index}${this.suffix}`;
Expand Down
15 changes: 15 additions & 0 deletions test/modules/io/readers/xviz-binary-reader.spec.js
Expand Up @@ -58,6 +58,21 @@ test('XVIZBinaryReader#readMetadata', t => {
t.end();
});

test('XVIZBinaryReader#readMetadata as json', t => {
const source = new MemorySourceSink();
const binBuilder = new XVIZBinaryReader(source);

const testData = {
version: '2.0.0'
};

source.writeSync('1-frame.json', testData);
const result = binBuilder.readMetadata();

t.deepEquals(result, testData, 'readMetadata works with json');
t.end();
});

test('XVIZBinaryReader#readMessage', t => {
const source = new MemorySourceSink();
const binBuilder = new XVIZBinaryReader(source);
Expand Down

0 comments on commit 35db013

Please sign in to comment.