Skip to content

Commit

Permalink
Add catalog parameter to makeReader
Browse files Browse the repository at this point in the history
This allows users to create a reader with a catalog

fixes #765
  • Loading branch information
Caleb ツ Everett committed Aug 26, 2023
1 parent a2e10f6 commit 3f5b1ed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ catalog.add(sharedSymbolTable);
// Create a reader with catalog
let bytes = writer.getBytes();
let reader = new BinaryReader(new BinarySpan(bytes), catalog);
let reader = makeReader(bytes, catalog);
```
## Contributing
Expand Down
8 changes: 4 additions & 4 deletions src/Ion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ export type ReaderBuffer = ReaderOctetBuffer | string;
* @param buf The Ion data to be used by the reader. Typically a string, UTF-8 encoded buffer (text), or raw
* binary buffer.
*/
export function makeReader(buf: ReaderBuffer): Reader {
export function makeReader(buf: ReaderBuffer, catalog?: Catalog): Reader {
if (typeof buf === "string") {
return new TextReader(new StringSpan(buf as string));
return new TextReader(new StringSpan(buf as string), catalog);
}
const bufArray = new Uint8Array(buf as ReaderOctetBuffer);
if (isBinary(bufArray)) {
return new BinaryReader(new BinarySpan(bufArray));
return new BinaryReader(new BinarySpan(bufArray), catalog);
} else {
return new TextReader(new StringSpan(decodeUtf8(bufArray)));
return new TextReader(new StringSpan(decodeUtf8(bufArray)), catalog);
}
}

Expand Down
33 changes: 33 additions & 0 deletions test/IonBinaryReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

import {assert} from 'chai';
import * as ion from '../src/Ion';
import { BinaryWriter } from '../src/IonBinaryWriter';
import { Writeable } from '../src/IonWriteable';
import { LocalSymbolTable } from '../src/IonLocalSymbolTable';
import { getSystemSymbolTable } from '../src/IonSystemSymbolTable';
import { Import } from '../src/IonImport';

describe('Binary Reader', () => {
it('timestamp', () => {
Expand Down Expand Up @@ -73,4 +78,32 @@ describe('Binary Reader', () => {
// [0xE0, 0x1, 0x0, 0xEA, 0xF, 0x21, 0x7, 0x31, 0x11, 0x85, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0xB6, 0x21, 0x1, 0x21, 0x2, 0x21, 0x3]|
assert.equal(binaryReader.position(), 22);
});

it('test catalog', () => {
const symbols = ['id', 'name'];

// Create a SharedSymbolTable with the desired strings
const sharedSymbolTable = new ion.SharedSymbolTable('foo', 1, symbols);
// Create a symbol table with shared table and system table.
const localSymbolTable = new LocalSymbolTable([
sharedSymbolTable,
getSystemSymbolTable(),
].reduceRight((parent, table) => new Import(parent, table), null as (null | Import)))
// dump the symbols as binary. The buffer should not define the symbols in the table.
const writer = new BinaryWriter(localSymbolTable, new Writeable());
symbols.forEach(symbol => writer.writeSymbol(symbol));
writer.close();
const buffer = writer.getBytes();

// Create a catalog with shared symbol table
let catalog = new ion.Catalog();
catalog.add(sharedSymbolTable);

// Reader with catalog should return correct symbol string values
let reader = ion.makeReader(buffer, catalog);
assert.deepEqual(ion.loadAll(reader), symbols.map(symbol => new ion.dom.Symbol(symbol)))

// Reader without catalog should error (really this is testing that our buffer references symbols from the table.
assert.throws(() => ion.loadAll(ion.makeReader(buffer)), "symbol is unresolvable")
})
});

0 comments on commit 3f5b1ed

Please sign in to comment.