From 3f5b1ed7fab715061b59751abd8b23a10e032041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Caleb=20=E3=83=84=20Everett?= Date: Sat, 26 Aug 2023 14:33:26 -0700 Subject: [PATCH 1/2] Add `catalog` parameter to makeReader This allows users to create a reader with a catalog fixes #765 --- README.md | 2 +- src/Ion.ts | 8 ++++---- test/IonBinaryReader.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 13293e5d..9af6bd51 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Ion.ts b/src/Ion.ts index 09a5d286..8e6109c6 100644 --- a/src/Ion.ts +++ b/src/Ion.ts @@ -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); } } diff --git a/test/IonBinaryReader.ts b/test/IonBinaryReader.ts index 709a8048..9704516b 100644 --- a/test/IonBinaryReader.ts +++ b/test/IonBinaryReader.ts @@ -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', () => { @@ -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") + }) }); From bc7089cd8c26e5036a777c1b8c8c6a3da3c95225 Mon Sep 17 00:00:00 2001 From: Caleb Everett Date: Mon, 28 Aug 2023 15:32:49 -0700 Subject: [PATCH 2/2] Update src/Ion.ts Co-authored-by: Khushboo <68757952+desaikd@users.noreply.github.com> --- src/Ion.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Ion.ts b/src/Ion.ts index 8e6109c6..b0be7b5c 100644 --- a/src/Ion.ts +++ b/src/Ion.ts @@ -57,6 +57,7 @@ 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. */ export function makeReader(buf: ReaderBuffer, catalog?: Catalog): Reader { if (typeof buf === "string") {