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

Add catalog parameter to makeReader #768

Merged
merged 3 commits into from
Sep 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
9 changes: 5 additions & 4 deletions src/Ion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ 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.
* @param catalog Optional catalog to be used by reader to resolve shared symbol table references.
*/
everett1992 marked this conversation as resolved.
Show resolved Hide resolved
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")
})
});